Lecture-6-Bottom-Up-Parsing
Lecture-6-Bottom-Up-Parsing
Parsing
Compiler Construction
1
Bottom-Up Parsing
A bottom-up parse corresponds to the construction of a parse tree
for an input string beginning at the leaves (the bottom) and working
up towards the root (the top).
4
Consider the following expression grammar:
E → E+T | T
T → T*F | F
F → id | (E)
The following reductions will be done for input “id+id”:
The key decisions during bottom-up parsing are about when to reduce
and about what production to apply, as the parse proceeds.
5
Handle Pruning
Bottom-up parsing during a left-to-right scan of the input constructs a
rightmost derivation in reverse.
Informally, a "handle" is a substring that matches the body of a
production, and whose reduction represents one step along the
reverse of a rightmost derivation.
6
Handles during a parse of id1 * id2
8
During a left-to-right scan of the input string, the parser shifts zero or
more input symbols onto the stack, until it is ready to reduce a string β of
grammar symbols on top of the stack.
It then reduces β to the head of the appropriate production.
The parser repeats this cycle until it has detected an error or until the
stack contains the start symbol and the input is empty:
11
Exercises
1. For the grammar S 0 S 1 | 0 1, indicate the handle in each of the
following right-sentential forms:
a. 000111
b. 00S11
2. For the grammar SSS+ | SS* | a, indicate the handle in each of the
following right-sentential forms:
a. SSS+a*+
b. SS+a*a+
c. aaa*a++
12
3. Give bottom-up parses for the following input strings and grammars:
a.The input 000111 according to the grammar S0S1 | 01
b.The input aaa*a++ according to the grammar S SS+ | SS* | a.
13