Lexical Analysis: Dr. Nguyen Hua Phung
Lexical Analysis: Dr. Nguyen Hua Phung
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
Lexemes Tokens
result IDENT
= ASSIGN_OP
oldsum IDENT
- SUBSTRACT_OP
value IDENT
/ DIV_OP
100 INT_LIT
; SEMICOLON
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
(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*
Integer:
Hexadecimal number:
Fixed-point number:
Floating point number:
String:
/∗∗
∗ 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 29 / 38
Lexical Analyzer
1 0 . 0 e 2 0 . . . Input Tape
Look ahead
r3 ...
Token
r2 rn
with longest prefix match
r1 r0
Lexical Analyzer
import scala.util.matching.Regex
val pat = new Regex("[0-9]+")
val pattern = "[a-z][a-z]*".r
val str = "123 abc 456"
pat.findFirstIn(str)
pattern.findFirstIn(str)
Library scala.util.parsing.combinator.Parsers.Parser
Construction new Parser[T]
new Parser[Token]
new Parser[Any]
Method ~ p1 ~ p2: must match p1 followed by p2
| p1 | p2: must match either p1 or p2, with prefer-
ence given to p1
? p1.? : may match p1 or not
* p1.*: matches any number of repetitions of p1
^^ p1 ^^ f: combine for function application
^^^ p1 ^^^ T: changes a successful result into the
specified value
... ...