Professional Documents
Culture Documents
08, 2016
1 Introduction
2 Roles
3 Implementation
source program
lexical analyzer
syntax analyzer
front end
semantic analyzer
code optimizer
back end
code generator
target program
Like a classification
I am a student
Finite Automata
Deterministic Finite Automata
Nondeterministic Finite Automata
Regular Expressions
q3 ...
q2 qn
q1 q0
Finite Control
a a
b
start q0 q1
Input: abaabb
Definition
Deterministic Finite Automaton(DFA) is a 5-tuple
M =(K,Σ,δ,s,F) where
K = a finite set of state
Σ = alphabet
s ∈ K = the initial state
F ⊆ K = the set of final states
δ = a transition function from K ×Σ to K
M =(K,Σ,δ,s,F)
where K = {q0 , q1 } Σ = {a,b} s=q0 F={q1 }
and δ
K Σ δ(K , Σ)
q0 a q0
q0 b q1
q1 a q1
q1 b q0
a a
b
start q0 q1
start q0 q1
b
a b
q2
a
start q0 q1
∈
b
a
q2
RE Language
0 => { 0 }
01 => { 01 }
0|1 => {0,1}
0(0|1) => {00,01}
(0|1)(0|1) => {00,01,10,11}
0* => {∈,0,00,000,0000,...}
(0|1)* => {∈,0,1,00,01,10,11,000,001,...}
(i|I)(f|F)
Keyword if of language Pascal
if
IF
If
iF
E(0|1|2|3|4|5|6|7|8|9)*
An E followed by a (possibly empty) sequence of digits
E123
E9
E
a b
start ab
a
∈ ∈
start a|b
∈ b ∈
∈
a
start a*
∈ ∈
∈
/∗∗
∗ Filename : H e l l o . g4
∗/
l e x e r grammar H e l l o ;
/ / match any d i g i t s
INT : [0 −9]+;
/ / Hexadecimal number
HEX: 0 [ Xx][0 −9A−Fa−f ] + ;
/ / match lower−case i d e n t i f i e r s
ID : [ a−z ] + ;
/ / s k i p spaces , tabs , n e w l i n e s
WS : [ \ t \ r \ n ] + −> s k i p ;
Dr. Nguyen Hua Phung Lexical Analysis 30 / 33
Lexical Analyzer
1 0 . 0 e 2 0 . . . Input Tape
Look ahead
r3 ...
Token
r2 rn
with longest prefix match
r1 r0
Lexical Analyzer