Chapter Two (3) (Autosaved)
Chapter Two (3) (Autosaved)
• the main task of the lexical analyzer is to read the input characters of the source
program, group them into lexemes, and produce as output a sequence of tokens.
• In some cases, information regarding the kind of identifier may be read from the
symbol table by the lexical analyzer to assist it in determining the proper token
it must pass to the parser.
The interaction b/n the lexical analyzer and the parser
A. Scanning: It consists of simple processes that do not require the tokenization of the
input such as deletion of comments, and compaction of consecutive white space
characters into one.
B. Lexical analysis: This is the more complex portion where the scanner produces a
sequence of tokens as output. produces tokens from the output of the scanner.
• A token can look like anything useful for processing an input text stream or text
file.
LEXEME:
• The lexical analyzer collects information about tokens into their associated
attributes. The attributes influence the translation of tokens.
5. Panic mode recovery: Deletion of consecutive characters from the token until
error is resolved.
SPECIFICATION OF TOKENS
There are 3 specifications of Tokens:
1. Strings
2. Language
3. Regular expression
Strings and Languages
An alphabet or character class is a finite set of symbols.
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.
Operations on strings
The following string-related terms are commonly used:
1. A prefix of string S is any string obtained by removing zero or more
symbols from the end of strings. For example, ban is a prefix for banana.
2. A suffix of string S is any string obtained by removing zero or more
symbols from the beginning of S. For example, nana is a suffix for
banana.
3. A substring of S is obtained by deleting any prefix or any suffix
forms. For example, nan is a substring of banana.
4. The proper prefixes, suffixes, and substrings of a string S are those
prefixes, suffixes, and substrings.
Con,….
5. 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
The following example shows the operations on languages:
Let L={0,1} and S={a, b, c}
1. Union : L U S={0,1, a, b, c}
2. Concatenation : L.S={0a,1a,0b,1b,0c,1c}
3. Kleene closure : L*={ ε,0,1,00….}
4. Positive closure: L+={0,1,00….}
Regular Expressions
• Regular expressions are used for algebraically representing certain sets of strings.
• Here are the rules that define the regular expressions over some alphabet Σ and the languages that
those expressions denote:
1. ε is a regular expression, and L(ε) is { ε }, that is, the language whose sole member is the empty
string.
2. 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.
3. Suppose r and s are regular expressions denoting the languages L(r) and
L(s). Then,
• If two regular expressions r and s denote the same regular set, we say they are
equivalent and write r = s.
• There are several algebraic laws for regular expressions that can be used to
manipulate into equivalent forms.
dl → r 1 d2 → r2 ……… dn → rn
letter → A | B | …. | Z | a | b | …. | z |
digit → 0 | 1 | …. | 9
- 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 notation [abc] where a, b and c are alphabet symbols denotes the regular
expression a | b | c.
-We can describe identifiers as being strings generated by the regular expression,
[A–Za–z][A–Za–z0–9]*
RECOGNITION OF TOKENS
• Consider the following grammar fragment:
stmt |ε
term |term
term → id |num
• where the terminals if , then, else, relop, id and num generate sets of strings given
by the following regular definitions:
• If → If
• Then → Then
• Else → Else
• relop → <|<=|=|<>|>|>=
• Id → letter(letter|digit)*
• Num → digit+ (.digit+)?(E(+|-)?digit+)?
• For this language fragment the lexical analyzer will recognize the keywords if,
then, else, as well as the lexemes denoted by relop, id, and num. To simplify
matters, we assume keywords are reserved; that is, they cannot be used as
identifiers.
Transition diagrams
q0 –starting state
fn –final state
Deterministic Finite Automata
DFA is a special case of a NFA in which
i) no state has an ε-transition.
ii) there is at most one transition from each state on any input.
DFA has five tuples denoted by
M = {Qd, Ʃ, δ, q0, fd}
Qd – finite set of states
Ʃ– finite set of input symbols
δ – transition function that maps state-symbol pairs to set of states
q0– starting state
fd–final state
Construction of DFA from regular expression
The following steps are involved in the construction of DFA from regular
expression: