Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lexical Analyzer
Part Two
Lecture Three
Parts of a Compiler
Source Front End ‘Middle End’ Back End Target
chars IR IR
Scan Select Instructions
tokens IR
Semantics Emit
IR IR
Machine Code
chars IR IR
Scan Select Instructions
tokens IR
Semantics Emit
IR IR
Machine Code
5
Finite Automata
• States are nodes, transitions are directed labeled edges,
some states are marked as final, one state is marked as
starting.
•
a
The mapping of an FA can be represented in a transition table
0
a
1
b
2
b
3 Input Input
State
a b
b
S = {0,1,2,3} (0,a) = {0,1} 0 {0, 1} {0}
= {a,b} (0,b) = {0} 1 {2}
s0 = 0 (1,b) = {2}
F = {3} 2 {3}
(2,b) = {3}
*3
6
DFA Example (2)
Accepting Strings Ending in “ing”.
Not i or g
Not i or n i
Not i
S0 i in ing
i n g
i i
Not i
CS510 Spring 2015 Shahira Azazy 7
DFA Example (3)
Rejecting Strings containing 11
0 0,1
1 1
S0 S1 S2
𝜀 NFA
S0 S1
a
NFA
S0 S1
a S2
9
Finite Automata(Example)
1) Design FSM that accept RE=[a-z]+|[0-9]+ S0
a-z
S1
a-z
whitespace
or comments
end of input
1 Accept EOF
(
2 Accept LPAREN
)
3 Accept RPAREN
;
4 Accept SEMI
Scanner DFA Example – Part 2
! =
5 6 Accept NEQ
< =
8 9 Accept LEQ
[0-9] [0-9]
11
[a-zA-Z] [a-zA-Z0-9_]
13
• 1- Using Doubly
Nested Case Analysis
Implementation • 2- Using the
Transition Table
(Table Driven)
16
RE to NFA
• Theconstruction of an NFA using a regular expression. The -
transitions are used to “glue together” the machines of each piece of a
regular expression to form a machine that corresponds to the whole
expression
17
RE to NFA(Continued)
NFA for: ab
a ε b
a
ε ε
b ε
ε
ε a ε
NFA for: a*
ε
* Jim Hogg :2014 Washington university
18
RE to NFA(Example-1)
• NFA for a(b|c)*
b
a
c
19
RE to NFA(Example-2)
• NFA for abc*
a b
c
20