Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lecture 9
1
Topics Covered in Lecture 8
2
Role of Lexical Analyzer
Errors generated by Lexical Analyzer
Tokens
Lexemes
Patterns
3
Lexical Analyzer
Part 2
4
Specification of Tokens
5
Learn by Example:
letter → A | B | C | … | Z | a | b | …
|z
digit → 0 | 1 | 2 | … | 9
6
Another Example:
Let the grammar fragment be:
if expr then stmt
expr → term relop term
What are the patterns (Regular Expressions) for the following
tokens?
Terminals Set of strings
If → if
then → then
else → else
relop → < | <= | > | >= | = | <>
(Reference: Exp 3.6, Page 98)
7
Learn by Doing
Answer
tab {A,…,Z,a,...,z}* | {A,…,Z,a,....,z}*bat
8
Token Recognition
Tokens can be recognized using a Transition diagram
Depicts sequence of actions a lexical analyzer take, when
called by the parser to get next token
Used to keep track of info about characters during scanning
of input
Example: Token to be specified >= and >
10
11
Learn by Doing
Identifiers in Java
position
Sal123
ab
x
Specification of token identifier
identifier → letter ( letter | digit )*
Recognition of token identifier ?
12
Learn by Doing
13
Terminologies : Automata &
Language Theory
Finite State Automata (FSA)
A recognizer that takes an input string and determines whether
it’s a valid string of the language.
14
Representing NFA
2) Transition Tables:
More suitable for representation within a
computer
16
Learn by Example
17
Learn by Example – NFA
construction
(a (b*c))
18
Learn by Example – NFA
construction
Step 2: (a (b*c)) | (a (b | c+)?)
(a (b | c+)?)
19
Learn by Example – NFA
construction
Step 3:(a (b*c)) | (a (b | c+)?)
20
Working of NFA
Learn by Example: OR
Input: ababb move(0, a) = 0
1.move(0, a) = 1 move(0, b) = 0
2.move(1, b) = 2 move(0, a) = 1
3.move(2, a) = ? (undefined) move(1, b) = 2
move(2, b) = 3
REJECT !
ACCEPT !
21
The NFA Problem
Two problems
– Valid input may not be accepted
– Non-deterministic behavior from run
to run…
Solution ?
22
The DFA Saves The Day
No epsilon transitions
23
NFA-DFA comparison (???)
24
How does this all fit together ?
Point to Remember
Both NFA and DFA can be used to
recognize tokens, but DFA are faster and
more optimizable than NFA
25
Lets Revise!
26
Tokens can be specified by using regular
expressions
Tokens can be recognized through
transition diagrams generated by regular
expressions
A transition diagram may be NFA or DFA
but DFA is preferable because of its
speed and optimization
27
Home Work
28