Shift-Reduce Table Longest Sequence at The Top of Stack Matching The RHS of A Rule
Shift-Reduce Table Longest Sequence at The Top of Stack Matching The RHS of A Rule
stack
Xm 1
Xm
X2
X1
Shift-reduce table
terminals and $
nonterminals and $
terminals,
Shift/Reduce/Error
(Ã modifier)
Syntax analysis 195
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
id m ⇤
T !T ⇤F
)m⇤
T !F
F m)
F ! (E )
Step 3.2 F m$
F ! id
id m +
)m+
)m)
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
E ! E + E |E ⇤ E |(E )|id
Follow (E ) = {$, +, ⇤, )}
) states 7 and 8 have
shift/reduce conflicts for
+ and ⇤.
(Dragonbook)
Syntax analysis 210
Disambiguation
Example:
Parsing of id + id ⇤ id will give the configuration
(0E 1 + 4E 7, ⇤id$)
We can choose:
I ACTION[7, ⇤] =shift ) precedence to ⇤
I ACTION[7, ⇤] =reduce E ! E + E ) precedence to +
(0E 1 + 4E 7, +id$)
We can choose:
I ACTION[7, +] =shift ) + is right-associative
I ACTION[7, +] =reduce E ! E + E ) + is left-associative
(same analysis for I8 )
Syntax analysis 211
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 217
Conclusion: top-down versus bottom-up parsing
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
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 like Yacc or Bison (but JavaCC is
top-down)
The choice of a parsing technique is left open for the project but we
ask you to implement the parser by yourself (Yacc, bison or other
parser generators are forbidden)
Weak precedence parsing was the recommended method in previous
implementations of this course
Motivate your choice in your report and explain any transformation
you had to apply to your grammar to make it fit the parser’s
constraints
To avoid mistakes, you should build the parsing tables by program