03 Syntaxanalysis 2 2012 2013
03 Syntaxanalysis 2 2012 2013
Indeed, First(↵) is the same for all RHS ↵ of the productions for
Exp et Exp2
This is a consequence of left-recursion.
Syntax analysis 146
Left-recursion
Recursive productions are productions defined in terms of
themselves. Examples: A ! Ab ou A ! bA.
When the recursive nonterminal is at the left (resp. right), the
production is said to be left-recursive (resp. right-recursive).
Left-recursive productions can be rewritten with right-recursive
productions
Example:
0
N ! 1N
N ! N↵1
..
.. .
. 0
N ! nN
N ! N↵m 0
N ! ↵1 N 0
N ! 1
, ..
.. .
.
N0 ! ↵m N 0
N ! n
N0 ! ✏
Exp ! Exp2Exp 0
0
Exp ! Exp + Exp2 Exp ! +Exp2Exp 0
Exp ! Exp Exp2 Exp 0 ! Exp2Exp 0
Exp ! Exp2 Exp 0 ! ✏
Exp2 ! Exp2 ⇤ Exp3 Exp2 ! Exp3Exp20
0
Exp2 ! Exp2/Exp3 Exp2 ! ⇤Exp3Exp20
,
Exp2 ! Exp3 Exp20 ! /Exp3Exp20
Exp3 ! num Exp20 ! ✏
Exp3 ! (Exp) Exp3 ! num
Exp3 ! (Exp)
Note
I The resulting grammar is ambiguous and the parsing table will
contain two rules for M[ElseStat, else]
(because else 2 Follow (ElseStat) and else 2 First(else Stat))
I Ambiguity can be solved in this case by letting
M[ElseStat, else] = {ElseStat ! else Stat}.
Syntax analysis 149
Hidden left-factors and hidden left recursion
Sometimes, left-factors or left recursion are hidden
Examples:
I The following grammar:
A ! da|acB
B ! abB|daA|Af
⇤
has two overlapping productions: B ! daA and B ) daf .
I The following grammar:
S ! Tu|wx
T ! Sq|vvS
⇤
has left recursion on T (T ) Tuq)
Solution: expand the production rules by substitution to make
left-recursion or left factors visible and then eliminate them
1. Introduction
2. Context-free grammar
3. Top-down parsing
4. Bottom-up parsing
Shift/reduce parsing
LR parsers
Operator precedence parsing
Using ambiguous grammars
A bottom-up parser creates the parse tree starting from the leaves
towards the root
It tries to convert the program into the start symbol
Most common form of bottom-up parsing: shift-reduce parsing
Bottum-up parsing of
int + (int + int + int)
Grammar:
One View of a Bottom-Up Parse
S
S ! E$ S → E$
E→T E
E ! T E→E+T
T
T → int
E ! E + T T → (E)
E
T ! int E
T ! (E ) E E
T T T T
(Keith Schwarz)
In the previous example, all the handles were to the far right end of
the left area (not inside)
This is convenient because we then never need to shift from the left
to the right and thus could process the input from left-to-right in
one pass.
1. Introduction
2. Context-free grammar
3. Top-down parsing
4. Bottom-up parsing
Shift/reduce parsing
LR parsers
Operator precedence parsing
Using ambiguous grammars
(s0 X1 s1 . . . Xm sm , ai ai+1 . . . an $)
X1 . . . Xm ai . . . an
(s0 X1 s1 . . . Xm sm , ai ai+1 . . . an $)
3. T ! T ⇤ 5)
F F → (E) 4 s5 s4 8 2 3
6) F → id 5 r6 r6 r6 r6
4. T ! F 6 s5 s4 9 3
7 s5 s4 10
5. F ! (E ) 8 s6 s11
6. F ! id 9 r1 s7 r1 r1
10 r3 r3 r3 r3
11 r5 r5 r5 r5
34
There are several ways of building the parsing tables, among which:
I LR(0): no lookahead, works for only very few grammars
I SLR: the simplest one with one symbol lookahead. Works with less
grammars than the next ones
I LR(1): very powerful but generate potentially very large tables
I LALR(1): tradeo↵ between the other approaches in terms of power
and simplicity
I LR(k), k> 1: exploit more lookahead symbols
Main idea of all methods: build a DFA whose states keep track of
where we are in the parsing
E0 ! E Closure({E 0 ! .E }) = {E 0 ! .E ,
E !E +T E ! .E + T
E !T E ! .T
T !T ⇤F
T ! .T ⇤ F
T !F
F ! (E ) T ! .F
F ! id F ! .(E )
F ! . id }
Syntax analysis 178
Goto
Goto(I , X )
Set J to the empty set
for any itemSA ! ↵.X in I
J = J {A ! ↵X . }
return closure(J)
Example:
E0 ! E I0 = {E 0 ! .E ,
goto(I0 , E ) = {E 0 ! E ., E ! E . + T }
E !E +T E ! .E + T
goto(I0 , T ) = {E ! T ., T ! T . ⇤ F }
E !T E ! .T goto(I0 , F ) = {T ! F .}
T !T ⇤F
T ! .T ⇤ F goto(I0 ,0 (0 ) = Closure({F ! (.E )})
T !F
T ! .F = {F ! (.E )} [ (I0 \ {E 0 ! E })
F ! (E )
goto(I0 , id) = {F ! id.}
F ! id F ! .(E )
F ! . id }
C = {closure({S 0 ! .S})}
repeat
for each item set I in C
for each item A ! ↵.X in I
C = C [ Goto(I , X )
until C did not change in this iteration
return C
Collect all sets of items reachable from the initial state by one or
several applications of goto.
Item sets in C are the states of a DFA, goto is its transition
function
s of a LR-parser F ! . id !!EET++! F ! . id + *
Example: parsing table I1 : for 0
EE 0! the
! .TE !
T . expression.T ⇤EF+ .Tgrammar I6 :I9 E: E .T
TT
2)
⇤ FT
. E. → F ! 0 . ids5
0 I : E
E! !
! .T E
E. + I. :
⇤6 FT E ! Example I 6 : E ! E 1.+ .T
E !
II6 !
: .T
:E ET.⇤
!+⇤! TET I : EI !
: E E
! +E + T T . .
! T+→
I0 : E ! .E , I6 : E ! E + .TT 1 TT T FF 9 ⇤.T F .9 1 s6
t us assume E the ! .E parserExample Example
+ Tis in configuration T ! .T I⇤2 F : T E!
E !T T
.FE ..! + T.F T ! .T ⇤ F T ! .T ⇤ F10
T !IExample:
T: E ! I11!
T ! :T F!
TT ⇤!
3)
.T
4)F (E
Example
parsing
T*F
).FFT table
.T ⇤→ !T T !2 .T⇤. ⇤FFr2 s7
(SLR) Parsing
I : E ! Tables
F T !
. for
.(E
T !) Expression
.F Grammar 102.
.F .F I10 : T ! 3 forT ⇤ Fthe . r4 ex
r4
E ! .T T ! .F 2 F T ! ! .(ET .) ⇤ F I : F ! T (E! ). .F
(s X s . . . Xm sm , ai ai+1 . . . aT n $) I : E ! E + .T F ! F+!
.(E
11 ) I
.(ET)⇤ 5)F F10→ (E)I11 : T !
6 : : EF !T4 E(E⇤+ F.T.
s5).
T ! .T0 ⇤1 F1 F ! .(EI)30 : FT E 0!
accept
!!F
!.F.E
Tid.!. ⇤ Action
, .Fid Example
F6F !Table
T
.(E )
! . id ! .T ⇤ F
IGoto
6 : E
F
! E3.
Table
! . id
F ⇤!
T.F! F ! .(E )
F . id 6) IF11→:id F ! (E T !
!
5 .T
). ⇤ F r6 r6
Example
nitially, the T state ! .F
F ! .(E )
is (s0 , a1 a2 . .1). FanE! → .E+T
$), idIII143 ::: IaEF
where T
9E1 .:!
!
0state
!
. .E Fid..is+
E
a(.En !
.E +
I9) the
:T EE *input
Example
+
! TE T(! .F
+. )T . $ I9 E1: ETT2 !
T !F .T
I9 ! !
E4.+ T
.F
: TE. ⇤!
T . !FF ! . id (SLR) T Parsing
! 6 .F s5 Table
I4 T: .EE F! ! E.E :EE(E +!TE. + T .
0 s5
(.E )T s4 3
I9 : 2)E E! →E T + ! .T.+ + TT .! FT !. ⇤.(E ) T F !7 .(Es5)
ord) 1 T I!
T E F⇤!
s6: T F . id F+ 5. )FIF9 ! )
!E
FI !: 1.. E idE ! !E E+ acc.T F ! .(E
CTION[sm ,60ai ] can take four 3)values:
+ T.T TT! T .I2⇤ :F EE
→ T*F E!
T !
! !T .T.E
.T . I6+ ⇤F s7 ! T ⇤ r2 F .r2 I10: E
I : FT ! !.T Tid⇤! F . TT. ⇤ !FT . ⇤ F F !8 . id s6
2 10Ir2: :T T E ! ! .T E ⇤ + F .T ! E + .F
I1 : E !2.E T . !
E ! T
.T I ⇤:
4) FTT ! F T ⇤ F IT E!
10
.T !!: T.T T
.F
.T . I⇤⇤!
Example F6F:I9 T:Fr4 !E⇤ ! F .
E + T . I 6 : F ! 6. ).F ! id
(E 1) EI9→ E !9 E +state
: E+T Tr1. ids7 +
E ! E . +IT : IE
10 →
1. Shift s: shifts the next input symbol and then the state11s on the (E ).
T!
3
! .TExample r4
⇤ F (E T T ! !
r4
.F ..T
r4 I : I !: .T
11
⇤ F⇤ 7F8 T10
TT⇤⇤I.F !
F : TT⇤!
10 F .T ⇤2)F .EI6→: TET ! !10
. ⇤.T0Fr3 s5r3
Example:
I2 : AE!! T
2. Reduce
Example
stack (s0 X1 s1 . .3. .XTmTsm!
.
(denoted
parsing
,6ai a.F
!
T
by
11
i+1 ⇤
T !
rn
:
5)
!
6)
FEF
F
.T
table
where
!
→
→
+
⇤
(E
(E)
.T
id
nF
I
I
is
3).
a
for
:
:
I T
F
F
T
11
T!
F
the
!
!
4:
5!(.E
production
F F
.(E
.F
s5.
. . .Fan ) ! (s0 X1 s1 . . . Xm ai s, ai+1 . . . an ) !
expression
)
..Fid))number)
.(E r6I10F
T
r6: T
).s4
T !
!
!
Example
!
Tgrammar
r6T.F
.(E ⇤)
r6F .
2
IF11!
T 3
!: .(E .FF)I! 11 EF!
1. : (E ).!E(E+).3)T TI10→: T*F TT!
11
!.T
ET +
T ⇤⇤ F 1 r5
F.
r5 s6
I :
I4(s:06XF1Is91!E !
. :. . (.E
XE E
m s)m!
+
, ai aE .T
i+1 .+ . . aT n) .
! II25 :: TE F! !
!
8
id.
.T I s6
T . 9⇤Action
: E !
F s7 TableI6 r1: Er1 ! E
s11 + T T ! .7 .T
E Table
F
+ .T .(ESyntax
F a! . id Goto 6)Syntax
FI9→ E ! E + T5 .
id
: analysis r6
(s0 XE 1 s1! . . 6.
T !XmF+
. .E
T .T r s!
! T⇤r As,Fid i ai+1 . . . an )
⇤ T0!
Tstate
9 r1
.F . +⇤Ir39F *:T E!! )T E. Tr3⇤+F !T .F. TF⇤ ! ) analysis
4. T ! F
I9 : TE !F
m
.! !!idT r3 ( r3 $T !E .T F
F 6 s5
I Output the prediction
T ! .F A 1) EE→+ E+TT .I0 : E 10 .E , F ! .(E !
F) Syntax . idanalysis T ! T. ⇤ F
E ! .T
FT→. .T⇤ F I3 : FET1!
F 0! !s5.(EF I. )10
+r5T: r5T T
I6 :T s4 E !! ! r5T
E TTr5+⇤. ⇤FI18F
.T
! ..F2Syntax 3
I10 : T ! T ⇤ 7F . s5
3. Accept: parsingI!
T10 F1.:! isT
E !T
successfully
⇤!F)E +
.(E T2)T⇤ E
!completed 11
! .E
id s6)I I6 :: ET !!Eacc .TF
+⇤.T ! F .: idF ! (E .) 5. F ! (E )
analysis
.T 3) T → T*F SyntaxIan
4 : emptyF! !.entry
analysis
E (.E
.T I 10 : F ! ! (E T
F).! ⇤ F.(E. E ) ! E. + F I11 : F ! (E ). 8 s6
4. Error: parser I F 6!
:
detected F !
I
id an :(Eerror
T ). ! (typically
T ⇤ F . 2 11
r2 in the
s7 action
T T ! !
r2 .T .F
r2 ⇤ F
34
EE.! ! T .E + .T6. F ! id
I9 : E 9 r1
T11 !
I2. :.F ! T 4) T → F Syntax
10 E + .T I5 : analysis
FE ! id. + T F )I! 6 : .EE id+!
table). I : F ! (E ). T3 ! ! .E .T r4⇤I11 F r4 : TFF! !
!
r4 .(E
.F (E
r4 ). Syntax analysis 10 r3
I9 F: ! E ! .(ETE ) ! +
11 T .T. 5)⇤ FF → (E) E ! .T s4 F I!
T !
Syntax T
E) !8 E 2+ T3 .
T . ⇤! F
analysis .T ⇤ F
F ! 3. .! TidTT !. ⇤TF⇤ F T4 !s5.F Syntax analysis
F ! 9 :.(E
. id 11 r5
is T ! .F 6) F → id T 5 ! .T
F ! .(E ) I9 :FE! ⇤
r6 F r6 I r6
10 :
78 r6 T ! T ⇤! F .F
.
Syntax analysis !. EidT+ ! T. T. ⇤ F 88
I5 :I10F: ! 4.Tid. !F!
T T!⇤F.(E F. ) T 6 !s5.F s4 I11 : F ! (E F 9 ).! 3 .(E )
F ! . id Syntax ET! I10!E:T+ .T⇤TF! . T ⇤ 10F . Syntax analysis Syntax analysis
I11 : 5.
Syntax F ! FF!
analysis (E !). (E. )id
Syntax analysis F7 !s5.(E ) I9 : analysis s4 F ! . id 80
I1 : E80 ! E .s6 I10 T : T Is11
! !: T
11 T .F ⇤⇤Syntax
F!F . (E ).
I9 : E ! E + T . Syntax F ! . id analysis
I9 : E ! E + T .
6. F ! id E9 !analysis
E. + r1 Ts7I11 : F ! r1(E ).
: T r1! T ⇤ F . Syntax analysis
I : F ! id. I10 T ! T. ⇤ F
Syntax analysis
T ! T. ⇤ F I25 : E10 ! T . r3 I11 r3
: F ! r3
(Er3
). 88
I10 : T ! T ⇤ F . T11 ! T . r5⇤ F r5 r5 r5 I
10 : T !Syntax T ⇤ analysis
F.
I11 : F ! (E ). I3 : T ! F . I11 : F ! (E ). 34
Syntax analysis
Syntax analysis
F ! (.E )
I4 : analysis
Syntax
E ! .E + T
Syntax analysis E ! .T Syntax analysis 88
Syntax analysis
Syntax analysis 181
Constructing the LR(0) parsing table
) LR(0) because the chosen action (shift or reduce) only depends on the
current state (but the choice of the next state still depends on the token)
I60+ TT
word) → 4 .T
.ETn! T 9 T . 11 F → I : E ! 4.(E
E +
s5 .T s4
90 1 1 m m i i+1
1)3 :TF Example
! s6:F TTidE .! ..!
, F⇤!
⇤ Action
F.T6F
Fid .! E F.(E
⇤ Table ) IF 6 :!E 5.
! )E I9+!
F3. :T.FE! (E! TE ⇤ F+10 T . 6 : F ! (E ).
F) !6).(EF )→ id 11 FT!!
.Tid.+
!!E acc.T
FI6!: 1.. E idET ! E.T+ +⇤3)T F
.T
TT! → T*F
F ! .(E
T .I2⇤ :F EE E!
T I!
! ! 0 T .E
.T
.T
TE
accept
!
. 0Istate+ ⇤!! T
F
.F .E !F ! ! id .T I⇤10F: T !
Goto FTable.(E
!T
T ⇤. !Fid
F .⇤! T FT. ⇤ idFT . ⇤I11F : F ! 89(E 5. .T
).⇤ Fs6r6 r6 Action
id s11
r6
ACTION[s m , 0ai ] can
I1 :(initially,
E !2.the E
T E. !
take
T state
! Example
! four
T
.T
.F
is (s:4)
I ⇤
values:
0, a
F TT1 a→ 2 .F
! .1)
T . Fan⇤ E!$),→I .E+T
F T
10
. EidI!
where
T
II!213 ::: E
:I4 :TI.T
! Ta9F
.F
.T
T
E1. .:⇤
10
!
!!
.Ir2F
!
Example
Example
⇤ .6FEI
FidT
:a(.E
En:T
.E T
:
..!
s7
is
I )E
+ !
E + T* ⇤
⇤
the
: !
TE !E!
F +
input
! E TF
r2
.T
. EE
(!
T
+
.r2
⇤
+ .+
T
) I $: E
.F F
.
T I.T
. 6 : IF
E !T
9F:! ! ET.E
T 6.
!
(E
!
!
id+F .T
).EF 4.
.F
.F
+! T .!
T !
idFF ! . id1) EI(SLR) 9→ : E+T ET! Parsing
! E + Tr1. Tables
6 .F s5state ids7 +s4 for *r1 E
1. Shift s:word)
E ! E . +F T!1..IE
F
shifts the next input symbol ! .(E 10 ) I9 and : 2)E Ethen ! →E T
T the+! 3 4T state .EE
.T
FI! Example
11!
0
!
⇤ s :on
r4
1 T(E F
9
(.E
E.E.Ts5.F+
T
r4
! +
9
the)!
T T T
! T
(E ).
! ..F
r4
FT
⇤!
s4 r4
!. ⇤.(E
..T
F⇤ E F⇤+
11
I)7 : T
F
I 101
! TI:9.T
2
T! :T⇤⇤TE
3
I.F!
F
.
10 ⇤! I
5. F9 ! (E )F: T : E
T ⇤
E +
! F
! T.
T E . ⇤ + F T.
2) E6 → T. F!!107 .(Es5) 0 r3 s5r3 s4
! ⇤ r3
stack Example:
(s0 X1 s1 . .3.
I2 :ACTION[s
AE!! IT
.XT mExample
Tm!
s !I
. mE, 0ai!
, parsing
6 i] T
6 a : a .F
T
Eid
11
i+1 ⇤ F n
E
. .!:
5)
!
.
can take four 3)
!
6)
!
a FE
)F
F
.T
Etable
E ! !
→+ +
+
⇤
(E)
(s
id
(E
.T
F
T.T
XTI 3).
0 1T 1→ T*F
values:F
I
sfor
!:
:
I
. T
11
.
F
F
T
T.
T!
X .the
! !
Im2!
!
4:⇤ a
5 (.E
F F
s,
:i .F
I
F
.(E
.F
.T
.(E
s5
EE.
E
a
Ti+1
id)!
!!
expression
)
)
!
!!T .
2 r6I r6
!:
. .E
T
.
.T
.T a I 6+
. In ⇤r2F
F :⇤10
)
!
T
T
I
).
s6:Ts4
T
!
T:I :T
!
Example
T
!
!
E
Ts7 r6
! T
.(E
F
!
E⇤T!
grammar
!
.FT
F⇤)
! r6..T
.
F
id
⇤r2 .F
F E
acc
.r2
⇤
8 .T 2
+ F
ITF1011!
II.T
6 :: E
!
: F
F3 ! .(E )
:T.(E.F
!
!
!F).ET IT!
6.
11
id1.
+
⇤!
F
F(E
:.F E. FT!
!
T!
).. ⇤!
id
E(EF+ T)..3)T
I :
⇤ FTI10→: T*F
1) E
ET
TT!F
E+T
!
11
!9.T
ET
8 . id
+.
T ⇤⇤state
F
.T
1
F
s6
r5 r5
Fr1. ids7 s6+
Action
s11
*r1
r5 T
I:11sr4FExample
2. Reduce F :
1(denoted ! 2.
.(E E
by T .
E rn ! !
) inputwhere T
.T →
I ⇤n:4) is
F T
4 T a ! production
F T ⇤ F 10
.TE number)
! T .T
10
.F
.T . ⇤ F6
F : E ! E + T . I F ! F .
.(E !
id ).T(E ). I → : E ! E + T
2 . r2 s7
1.
T Shift
I Pop 2| | (= r ) E! 4.T s:.T⇤ shifts !
F !E F
the
T.mT+
next
T :.Fstack
10 symbol and then the →
I1Ftable:+E(E) F
F
E 0
I!
! ! 6 ! .(E
. 11
:1 .E
s5
E id+
T .!
3 state
)! .(E
4: TF
I Example
.T )⇤F!
:on
s4
9
F (E !Fr4the
TT
!
T(E
:!
(E
).
!.F
).
r4
..T
r4 I 11
9
7 :
F: ⇤FF8 ! 2.(Eid3.)
I
T3 10 ! : T T ⇤
2. ⇤ I .F !
F
10E : T
! T ⇤
T! F .
T ⇤ F I
4) 2)T E .
11
9
: TF !
:F TET!
! 10.F(E
! ET +
).
. ⇤.T0 r3 s5r3 r3
II3Push
: 2.TAReduce
stack F
!and IF
Example:
(s0 X!
2 s:.Fwhere
AE!
items
1 s1 . .
!!(E
.3.
sT
T
. id
X s!
=.)GOTO[s
!
m ,6parsing
Example Example
from Ithe
! ai a.F
(SLR) T IE11. .!
i+1 ⇤
:5)
.FanFE
Parsing
,IA]
)! !
→
F :→⇤E
(sF
E
(E
.T0 X!
nF! Iis!
3).sfor
71Tables
: .T
.I..T
EF
T
s5
F
.id
T .!
Xthe
+
!
5!T
11
for
F
.Fs5.F!
m ai s, aexpression
..Fid))Expression
i+1 . s4 . .! aIn6.) ). Example
.(E
id
s4! E
T)grammar
! I⇤7 E
Grammar
⇤ F . E !I11
+ .T10 F !
T !: .(E .FF)I! 111. : (E
F3.) T ! T ⇤ 5)
EF! ).!E(E+).3)TF T→
I6 →
→
I →(E) : TT! !.T
F
F !11.(E ) 31 r5 r5 s6r4 r4
T ⇤⇤ FF .
r5
5. F
(denoted ! .(E byT)mrn )! 6)r where 5.T F
id id. F
84 a production
! .(E
(.E
s6: Enumber) Fr6I10F
! r6: T T !
!
s11.+
!id.(E r6T.F
I.7)r6.T : T⇤ FF E T! . ⇤+ F I10 FT*F! . .F id 4 s5 r2 s7
. ⇤.!
F.+ .(E I. 90⇤! ! E T T ! . .Fid
I4(s:I06XF1:Is91!. :.I.!
E XE
(.Em s)
EmT! ,+ a!i aE
.T4.
i+1 T T
. aT!
F n) . ! F I
I 5 :
: T F
E ! !
I ! : .Tid.
T EFF
E !
6!
! F
Action .
.E.(Es5
E id +. )Table
T I I : :F s4 ! E (E
! Goto ). E +
Table .T 9 3
.(E
2. E ! T 4) T 11 : TF !
F
! (E ).52 r6 r6
Pop 2| | (=Fr )! items . id T !
from the2.Fstack 91 11
F!
F6Expression ! . :! .(E
id ) IF7 E:!+ 6)Syntax
FI9→ → id
(s0 XE 1 s1! . .I6.
T !
X
.I.E :F+.TrT
!s!T! ⇤andr As,Fid
FF s..F ai+1 ..s.id
i!
awhere . anGOTO[s)(SLR)TParsing
Tr0,! Example .F F.! r1
7Tables
⇤I9F.T
! .E*s5:ids7
.r3+E
T (! for ! ) TE
r1
s4. T Ir1
⇤6+ F T E.F .! TGrammar
F .(E .T
F E ! E .4.
).(Eid.) analysis
!10Syntax
TT !! F T ⇤ 5)F F →T(E)
EF!
: analysis !E.(E +)T3 . r4 r4
Em⇤
Tm m
T F I! T EFE T r3s11!
$T
IF.7⇤.T F
3Push A F=E+T A] E .T 64 s5
I Output the prediction I6.F I E 5.
. ::. .! XE
1) ! ! E→
ai aEE
(E+ )!
. .T .I.(E
0!: E 10
)mstate 5 :id
II25!::s5 .E ,+!
8 r3
id.
I.69⇤:T s6: E r3 F! ! E! .+ Tid T! :! T⇤. Fidanalysis T ⇤+.F F3. F! !T. .id⇤ F s5
F1:Is991! ,+ .T
A ! F Syntax
E ! T I.T !
4(s:0 X (.E Em s) ! i+1 .+ aTn) .
I : FT0! ! .(EF TFEI. )10 ! !
! r5 .T :id.
TIr5 T FE
s4
Action ! ! ! r5T
Table E T Ir5+
⇤ . :⇤.T
F 1EF. .(E ! 2 )E3Table
Goto + .T 75 . s5 r6 r6
I! ! 2) ⇤ E F T ! . id 11 T 6 ! .F F ! .(E ) F! 6) I FI →:: id
TE ! T
E + ⇤ F
T
F1.I:! isT T F⇤T .Fid . T ⇤(EFF .) 5. 4. (EF)
→ 3 Syntax analysis Syntax analysis
3. Accept: parsing (s 0X
)T
successfully
E1 sF .E . 6. !
X
. .ET mF r!
.T s! completed
r As, . .⇤a⇤i aFi+1 . . . aE ) ! .E + 9 T r1
:. +⇤Ir3E9FT*:! Ts7
EE! !
+T
r1
EF.I8T
r1
⇤.+ :FidF
! Syntax
T.F ! analysis 10 9
T10 .TE ⇤!
.(E 1!
+ T T
+ ! T
m
: TE 1)! F
EIE E+TFnFT1! .(.E T T0s6
id
state !
)!Iin
I.F
:6T
id
:F TT!
!
r3 ( F) r3!
!
.T
accF
T).
⇤.T!
⇤ET$T !..TEE1F .T T! I11 : FT ! ! (E T .). ⇤ 8F
6 s5 s6
4→:+ !
TT:! FF ).(E ! . id
r3.(E
FTI011 (E
3).F I9T(typically E .I! 0 :entry E 10 ! .E , action F ESyntax
TAT*F
Syntax analysis ! .+ Fanalysis
4. Error: parser
TI11 ::.FOutput
detectedFEIid !
I! !Tthe!
an :E(E
.T
prediction
error
T !→
)..T ⇤2)!Syntax
FIE5.an empty
FEF2!
.T !r2 10 .(E the
s7 I. )10 IIr56r2! :T: E.F
s4.T r2!
! ! ⇤r5I!6F :T
+ .!
⇤.E ⇤ 34
2) 3
F 6! EE.! 9T T F+I8.! id(E ) I10 : T ! T ⇤978F . s5 s6r1 s7
s5
!
I2. 10
T : +T
4) !T TT F ⇤! F T→.: . T⇤analysis I! 3 :id. .E E1 r4 +11
!! T F
.E + r5 E
F !
r5 E
!
id :TidF
.F .E !
Syntax +analysis
(E.T6. .) 5. F! F !
table). 3. Accept:
!
parsing
)TII!
10 F1.
!: .TF⇤! ! is
E successfully
!
.(E E
) →
F (E ). + T completed T 3 !
Ian
.TFF !
: empty
⇤ I
!11
F .(.E id )ITF!
r4 : s6 FI 6 :
:!
r4! E.F
T
T
.(E
! (E
r4! !
!
E).
)Syntax .T
acc
T
F
+
F).
⇤ !
.T
!
F .
⇤analysis
. ⇤F! .(E . E).T!⇤EF. + F Syntax analysis I11 : F ! (E ).10 r3 r3
I94.F: Error:
E ! .(Eparser
TE +
11 T :.T . 5).⇤id F
! T3))..T T → T*F ESyntax
4 ! .4 : s5.T
analysis
E2 ! .TI11 10 :s7the Faction Tr2! (E
! ⇤T! T F
⇤EF :(E
detected I! Fan error
(E) ! (typically
T ⇤T F entry in
9 T .T! .F 6F2+
34
! r2 s4
F I! :.(E EI9) :! .T r28 E
: .ET
F 6! .F .! T .E + .T6. F ! id
T11 → Syntax analysis E 3+ 119 r1
r5 s7r5
! :.F F )IT E
Syntax analysis
! 3. .!
F table). TidTT ! ⇤T
I2. EF! 10
TE + 4) T → F T I5! F E3! !!F.E
id. +⇤IF FTr4!
id
! ! id
tax analysis T I9 F: !
.! ESyntax
F
.F
.(ET
! 6)E) I! +F→
11 T:.T.id F⇤! F (E ).F5 ! .(E .T T r6
)⇤ .T
r6 r4
I :
11
E
I:r610
!
F:Fr6!
T78E T !
r4
+
T! .F
T
! (E
r4
.(E T
. T !
). ⇤!
.
Syntax ⇤ T FT . ..F
Fanalysis
⇤ ! F .T ⇤ F Syntax analysis 10 r3 r3
E ! .T F ! . id
I5 :I10F: ! 4.Tid. !F!
T FT ! ! F.(E
3.
⇤ F.! T.T)T!!
id
analysis
. ⇤TF ⇤ 5) F F → (E)F T6 ! !s5..F T4 !s5.F
id5 !
9
Syntax analysis
s4 I11F:F!
s4 I! 9F :.(E . id
! E) (E !
F98). !E32+.(E T3 .) 11 88 r5 r5
Syntax analysis T .F 6) F → id
SyntaxSyntax analysisanalysis F7 !s5.(E FT !
)!Syntax
.T r6⇤EFT
I.(E
9 : )analysis s4 !
r6I10
I9 : F
!E:T+
E! ! T⇤T
Ir6.10 :F
. E!
78 r6. TT!
idT+ F ! T .⇤ T10TF. .⇤⇤! ..F analysis
FFSyntax
Syntax analysis
I11Syntax: 5. FI5! F:IF10!
analysis (E
F !
:! ).
4.
(E T.id. )id
!F!
T T!⇤F.(E F . ) I1 : E80 ! EF T6. ! s5.F I10 T : T !s4 T I11 ⇤F:! F ..F⇤(E !). ! (EF9 .). ! id3 .(E )
80 88
F ! . Fid s6 . id !I
s11 11 T
E !: T .I F
!⇤ : T T F! T ⇤ F . Syntax analysis
: I9FI:10 ).E :+Syntax T . !F E!
10ISyntax Eanalysis +. T .
I9 : IE !analysis
FE! +(E T!). . . )id Syntax Syntax analysis
analysis 7 !s5.(E )Syntax analysis
! s4 10 idSyntax analysis
6. F 11!: 5. id FF! E 9 I! : EE.80+ T I11 (E 9T analysis
Syntax 80
(E 1 r1! I E
s7 . : T r1! T: T T
r1
!I ⇤! T :
F .. F ⇤ ⇤ F!F . (E ).
F E + TII.25 :: E F10 ! id. F ! .10 id s6 s11
11
T !I9 T : .E⇤ ! ! SyntaxTE.9 r3!analysis E r3. + Ts7I11 r3 : r3 Fr1). ! r1(E ). 9 :!
ISyntax
T Eanalysis
T!. E ⇤ F+ T .
Syntax analysis 6. F ! id I5 : F10r5!Iid. 11 r1 : F I10! : T (E !T ⇤ F . Syntax analysis 88
ISyntax
10 : analysisT ! TT⇤ ! F .T. ⇤ F T11 I! 2 : TE. ! ⇤ FTr5 . r3 I11 r3 r5 r5 r3 I r3 : T !
10 T ! TT ⇤F . ⇤. F
: F ! (E ). Syntax analysis 88
I11 : F ! I10 (E : ). T ! T ⇤ FI3. : T ! FT.11 ! T . r5⇤ F r5 r5
I11r5 : I10F : !T (E 34
!). T ⇤
Syntax analysis
F . Syntax analysis
SyntaxI analysis
I11 : F ! (E ). I : F ! I3 : (.E T )! F . 11 : F ! (E ).34
Syntax analysis
Syntax 4 analysis Syntax analysis
ESyntax I! 4 : .E F+
analysis !T(.E )
T id ( $+*) 34
F id ( $+*)
(Dragonbook)
Follow (R) contains ’=’. In I2 , when seeing ’=’ on the input, we don’t
know whether to shift or to reduce with R ! L.
1. Introduction
2. Context-free grammar
3. Top-down parsing
4. Bottom-up parsing
Shift/reduce parsing
LR parsers
Operator precedence parsing
Using ambiguous grammars
stack
Xm
Xm 1
X2
X1
Shift-reduce table
terminals and $
nonterminals and $
terminals,
Shift/Reduce/Error
(Ã modifier)
Syntax analysis 192
Weak precedence parsing algorithm
Create a stack with the special symbol $
a = getnexttoken()
while (True)
if (Stack= = $S and a = = $)
break // Parsing is over
Xm = top(Stack)
if (SRT [Xm , a] = shift)
Push a onto the stack
a = getnexttoken()
elseif (SRT [Xm , a] = reduce)
Search for the longest RHS that matches the top of the stack
if no match found
call error-recovery routine
Let denote this rule by Y ! Xm r +1 . . . Xm
Pop r elements o↵ the stack
Push Y onto the stack
Output Y ! Xm r +1 . . . Xm
else call error-recovery routine
Example:
Shift/reduce table
⇤ + ( ) id $
E !E +T E S S R
E !T T S R R R
T !T ⇤F F R R R R
T !F ⇤ S S
F ! (E ) + S S
F ! id ( S S
) R R R R
id R R R R
$ S S
⇤
We only need to consider the pairs X l Y with Y a nonterminal that were added in
S at the previous iteration
Step 1 Sl$
Step 2 E l+
+lT
T l⇤
⇤lF
E !E +T (lE
E !T E l)
T !T ⇤F Step 3.1 +lF
T !F ⇤ l id
F ! (E ) ⇤l(
F ! id (lT
Step 3.2 + l id
+l(
(lF
Step 3.3 (l(
(lid
⇤
We only need to consider the pairs X m Y with X a nonterminal that were added in
R at the previous iteration.
Step 1 E m$
Step 2 T m+
F m⇤
T m)
Step 3.1 T m$
E !E +T F m+
E !T )m⇤
T !T ⇤F id m ⇤
T !F F m)
F ! (E ) Step 3.2 F m$
F ! id )m+
id m +
)m)
idm)
Step 3.3 id m $
)m$
Shift/reduce table
⇤ + ( ) id $
E !E +T
E S S R
E !T T S R R R
T !T ⇤F F R R R R
T !F ⇤ S S
F ! (E ) + S S
F ! id ( S S
) R R R R
id R R R R
$ S S
S ! AbA|B
B ! b|c
A ! ✏
is transformed into
S ! AbA|Ab|bA|b|B
B ! b|c
1. Introduction
2. Context-free grammar
3. Top-down parsing
4. Bottom-up parsing
Shift/reduce parsing
LR parsers
Operator precedence parsing
Using ambiguous grammars
E ! E + E |E ⇤ E |(E )|id
Follow (E ) = {$, +, ⇤, )}
) states 7 and 8 have
shift/reduce conflicts for
+ and ⇤.
(Dragonbook)
Syntax analysis 208
Disambiguation
Example:
Parsing of id + id ⇤ id will give the configuration
(0E 1 + 4E 7, ⇤id$)
We can choose:
I ACTION[7, ⇤] =shift 5) precedence to ⇤
I ACTION[7, ⇤] =reduce E ! E + E ) precedence to +
(0E 1 + 4E 7, +id$)
We can choose:
I ACTION[7, +] =shift 4) + is right-associative
I ACTION[7, +] =reduce E ! E + E ) + is left-associative
(same analysis for I8 )
Syntax analysis 209
outline
1. Introduction
2. Context-free grammar
3. Top-down parsing
4. Bottom-up parsing
Shift/reduce parsing
LR parsers
Operator precedence parsing
Using ambiguous grammars
Top-down
I Easier to implement (recursively), enough for most standard
programming languages
I Need to modify the grammar sometimes strongly, less general than
bottom-up parsers
I Used in most hand-written compilers and some parser generators
(JavaCC, ANTLR)
Bottom-up:
I More general, less strict rules on the grammar, SLR(1) powerful
enough for most standard programming languages
I More difficult to implement, less easy to maintain (add new rules,
etc.)
I Used in most parser generators (Yacc, Bison)
LL(1) LR(1)
LALR(1)
SLR
LL(0) LR(0)
(Appel)
FIGURE 3.29. A hierarchy of grammar classes.
Syntax analysis For example, the items in states 6 and 13 of the LR(1) parser 212
for G
Error detection and recovery
Top-down parsing:
I Recursive descent: let each parsing function return the sub-trees for
the parts of the input they parse
I Table-driven: each nonterminal on the stack points to its node in the
partially built syntax tree. When the nonterminal is replaced by one
of its RHS, nodes for the symbols on the RHS are added as children
to the nonterminal node
epresents ansuch
containing rules assignment
as expression not an assignment statement. In C an
railing semicolon.
Syntax analysis
That is, in C (unlike in Algol) the semicolon is a statement 218
For your project
Parser generators:
I Yacc: Unix parser generator, LALR(1) (companion of Lex)
I Bison: free implementation of Yacc, LALR(1) (companion of Flex)
I ANTLR: LL(*), implemented in Java but output code in several
languages
I ...
http://en.wikipedia.org/wiki/Comparison_of_parser_generators
https://github.com/prashants/calc
Syntax analysis 220
Flex file: calc.lex
%{
#define YYSTYPE double /* Define the main semantic type */
#include "calc.tab.h" /* Define the token constants */
#include <stdlib.h>
%}
%option yylineno /* Ask flex to put line number in yylineno */
white [ \t]+
digit [0-9]
integer {digit}+
exponent [eE][+-]?{integer}
real {integer}("."{integer})?{exponent}?
%%
{white} {}
{real} { yylval=atof(yytext); return NUMBER; }
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return TIMES; }
"/" { return DIVIDE; }
"(" { return LEFT; }
")" { return RIGHT; }
"\n" { return END; }
. { yyerror("Invalid token"); }
%start Input
Input: /* epsilon */
| Input Line
;
Line:
END
| Expression END { printf("Result: %f\n", $1); }
;
Error handling:
%%
if (yyparse()) {
fprintf(stdout, "Successful parsing\n");
}
fclose(yyin);
fprintf(stdout, "End of processing\n");
return EXIT_SUCCESS;
}
How to compile:
bison -v -d calc.y
flex -o calc.lex.c calc.lex
gcc -o calc calc.lex.c calc.tab.c -lfl -lm
Example:
>./calc
1+2*3-4
Result: 3.000000
1+3*-4
Result: -11.000000
*2
syntax error on line 3 - *
Successful parsing
End of processing
state 9 state 11
6 Exp: Exp . PLUS Exp 6 Exp: Exp PLUS . Exp
7 | Exp . MINUS Exp
8 | Exp . TIMES Exp
9 | Exp . DIVIDE Exp NUMBER shift, and go to state 3
10 | MINUS Exp . MINUS shift, and go to state 4
LEFT shift, and go to state 5
$default reduce using rule 10 (Exp)
Exp go to state 17
state 10
6 Exp: Exp . PLUS Exp
7 | Exp . MINUS Exp
8 | Exp . TIMES Exp
9 | Exp . DIVIDE Exp
11 | LEFT Exp . RIGHT