SPECIFICATION OF TOKENS - Unit 1
SPECIFICATION OF TOKENS - Unit 1
SPECIFICATION OF TOKENS - Unit 1
UNIT-1
SPECIFICATION OF TOKENS
TEAM MEMBERS:
ANJALI.B 61072111103
DHINOOVIKA.D 61072111107
KAVIYA.S 61072211909
SPECIFICATION OF TOKENS
Regular expressions are an important notations for specifying
lexeme patterns. While they can not express all possible patterns,
they are very effective in specifying those types of patterns that we
actually need for tokens.
There are 5 specifications of tokens
Strings and languages.
Operations on languages .
Regular expression.
Regular definition.
Extension of regular expressions.
STRINGS AND LANGUAGES
An alphabet or character class is a finite set of symbols.
EXAMPLES of symbols are letters, digits and punctuation.
A string over an alphabet is a finite sequence of symbols drawn from that alphabet.
A language is any countable set of strings over some fixed alphabet.
In language theory, the terms "sentence" and "word" are often used as synonyms for
"string." The length of a string s, usually written |s|, is the number of occurrences of
symbols in s. For example, banana is a string of length six. The empty string, denoted ε,
is the string of length zero.
Operations on strings
A prefix of string s is any string obtained by removing zero or more symbols from
the end of string s. For example, ban, banana and ℇ are prefixes of banana.
A suffix of string s is any string obtained by removing zero or more symbols from
the beginning of s. For example, nana, banana and ℇ are suffixes of banana.
A substring of s is obtained by deleting any prefix and any suffix from s. For
example, banana ,nan and ℇ are substrings of banana.
The proper prefixes, suffixes, and substrings of a string s are those prefixes,
suffixes, and substrings, respectively of s that are not ε or not equal to s itself.
A subsequence of s is any string formed by deleting zero or more not necessarily
consecutive positions of s. For example, baan is a subsequence of banana.
Operations on languages:
The following are the operations that can be applied to languages:
1. Union
2. Concatenation
3. Kleene closure
4. Positive closure
VARIOUS OPERATIONS AND THEIR
DEFINITIONS
OPERATIONS DEFINITIONS
Union of two languages L L U M = {s | s is in L or s is
and M . in M}
Concatenation of two LM = {st | s is in L and t is in
languages L and M . M}
The Kleene Closure of a L* = Zero or more
language L . occurrence of language L.
Positive closure of a L+ = One or more occurrence of
language L. language L
The following example shows the operations on strings: Let L={0,1}
and S={a,b,c}
Union :L U S={0,1,a,b,c}
Concatenation :L.S={0a,1a,0b,1b,0c,1c}
Kleene closure :L*={ ℇ, 0,1,00,…….}
Positive Closure :L+={0,1,00,……..}
REGULAR EXPRESSIONS
Each regular expression r denotes a language L(r). Here are the rules that define
the regular expressions over some alphabet Σ and the languages that those
expressions denote:
BASIS: There are two rules that form the basis
ε is a regular expression, and L(ε) is { ε }, that is, the language whose sole
member is the empty string.
If ‘a’ is a symbol in Σ, then ‘a’ is a regular expression, and L(a) = {a}, that is, the
language with one string, of length one, with ‘a’ in its one position.
INDUCTION: There are four parts to the induction whereby larger
regular expressions are built from smaller ones. Suppose r and s are
regular expressions denoting the languages L(r) and L(s).
Then, (r)|(s) is a regular expression denoting the language L(r) U
L(s).
(r)(s) is a regular expression denoting the language L(r)L(s).
(r)* is a regular expression denoting (L(r))*.
(r) is a regular expression denoting L(r).
letter → A | B | …. | Z | a | b | …. | z |
digit → 0 | 1 | …. | 9
id → letter ( letter | digit ) *
EXTENSIONS OF REGULAR EXPRESSION
1. One or more instances (+):
The unary postfix operator + means “ one or more instances of” .
If r is a regular expression that denotes the language L(r), then ( r ) + is a regular
expression that denotes the language (L (r ))+.
Thus the regular expression a+ denotes the set of all strings of one or more a’s.
The operator + has the same precedence and associativity as the operator*.
2. Zero or one instance ( ?):
The unary postfix operator ? means “zero or one instance of”.
The notation r? is a shorthand for r | ε.
If ‘r’ is a regular expression, then ( r )? is a regular expression that denotes the language.
3. Character Classes:
The notation [abc] where a, b and c are alphabet symbols denotes the
regular expression a | b | c.
Character class such as [a – z] denotes the regular expression a | b | c |
d | ….|z.
We can describe identifiers as being strings generated by the regular
expression, [A–Za–z][A– Za–z0–9]*.