Professional Documents
Culture Documents
Introduction
• Every language has rules that prescribe the
syntactic structure of well formed programs.
– Top-down parsing
• Recursive Descent Parsing
• Non-recursive Predictive Parsing
– Bottom-up parsing
• LR Parsing
– if we reach the end of the input and the tree pointer passes the last
symbol of the tree, we have finished parsing.
Parser
X program output
Y
Z
$
Parsing table, M
Stack
– Initially, the stack contains the start symbol of the grammar followed
by $.
Input Stack
Exercise: Parse 1+2 1+2$ E$
1+2$ TR$
1+2$ 1R$
+2$ R$
+2$ +TR$
2$ TR$
2$ 2R$
$ R$
4/5/2021 Compiler
$ Design $ 19
Constructing a predictive parsing table
• Makes use of two functions: first and follow
First
• First(α) = set of terminals that begin the
strings derived from α.
• If α => λ in zero or more steps, λ is in first(α)
Find:
First(TR)
First(+TR)
First(-TR)
First(0)
Find:
Follow(E)
Follow(R)
Follow(T)
Is G an LL(1) grammar?
Read more
+E$ id * id $ shift
id + E $ * id $ reduce by E id
E+E$ * id $ shift
*E+E$ id $ shift
id * E + E $ $ reduce by E id
E*E+E$ $ reduce by E E * E
E+E$ $ reduce by E E + E
4/5/2021 E$ $
Compiler Design accept 51
Conflict during shift/reduce parsing
• There are two types of conflicts in
shift/reduce parsing:
– shift/reduce conflict: the parser knows the entire
stack content and the next k symbols but cannot
decide whether it should shift or reduce.
– reduce/reduce conflict: the parser cannot decide
which of the several productions it should use for
a reduction.
Parser
st program output
a
c
k
action | goto
parsing table
– Xi is a grammar symbol
E’ E
EE+T
ET
TT*F
TF
F (E)
F id
• Theorem
– The set of valid items embodies all the useful information
that can be gleamed from the stack.
ET
TT*F
TF
F (E)
F id
G2’:
S’ S
SL=R Follow (S) = {$}
SR Follow (R) = {$, =}
Follow (L) = {$, =}
L *R
L id
RL
Closure [I] := I
Repeat
For each item [A α. Bβ, a+ in closure(I), each
production B γ in G’ and each terminal b in First(βa) such
that [B.γ, b+ is not in closure (I) do
add [B .γ, b+ to closure (I)
until no more items can be added to closure (I)
G2’:
S’ S
SL=R
SR
L *R
L id
RL
4/5/2021 Compiler Design 96
Canonical LR parsing (cont’d)
C = {I0, I1, I2, I3, I4, I5, I6, I7, I8, I9}
I0 = ,*S’ .S, $], [S .L = R, $], [S .R, $], [L .*R, =|$], [L .id, =|$], [R .L, $]}
I1 = goto (I0, S) = ,*S’ S., $]}
I2 = goto (I0, L) = {[S L . = R, $], [R L., $]}
I3 = goto (I0, R) = {[S R., $]}
I4 = goto (I0, *) = {[L * . R, =|$], [L .*R, =|$], [L .id, =|$], [R .L, =|$]}
I5 = goto (I0, id) ={[L id ., =|$]}
I6 = goto (I2, =) = {[S L = . R, $], [R . L, $ ], [L .*R, $], [L .id,$]}
I7 = goto (I4, R) ={[L * R ., =|$]}
I8 = goto (I4, L) ={[R L ., =|$ ]}
goto (I4, *) = I4
goto (I4, id) = I5
I9 = goto (I6, R) ={[S L = R ., $]}
I10 = goto (I6, L) ={[R L ., $ ]}
I11 = goto (I6, *) = {[L * . R, $], [L .*R, $], [L .id, $], [R .L, $]}
I12 = goto (I6, id) ={[L id ., $]}
goto (I11, *) = I11
goto (I11, id) = I12
goto (11, L) = I10
I13 = goto (I11, R) = {[L*R., $]}