Lecture 09
Lecture 09
Bottom-up parsing
Goal of parser : build a derivation
Top-down parser : build a derivation by working from
the start symbol towards the input.
Builds parse tree from root to leaves
Builds leftmost derivation
Bottom-up parser : build a derivation by working from
the input back toward the start symbol
Builds parse tree from leaves to root
Builds reverse rightmost derivation
Bottom-up parsing
The parser looks for a substring of the parse tree's
frontier...
...that matches the rhs of a production and
...whose reduction to the non-terminal on the lhs
represents on step along the reverse of a rightmost
derivation
Such a substring is called a handle.
Important:
Not all substrings that match a rhs are handles.
Bottom-up parsing techniques
Shift-reduce parsing
Shift input symbols until a handle is found. Then,
reduce the substring to the non-terminal on the lhs of
the corresponding production.
Operator-precedence parsing
Based on shift-reduce parsing.
Identifies handles based on precedence rules.
Example: Shift-reduce parsing
Grammar:
1. S S+S
2. S S-S
3. S (S)
4. S a
Input to parse:
a1-(a2+a3)
Example: Shift-reduce parsing
Grammar:
1. E 2E2
2. E 3E3
3. E 4
Input to parse:
32423
Example: Shift-reduce parsing
STACK ACTION
$ Shift
Grammar:
$ id1 Reduce (rule 5)
1. S E
2. E E+E $E Shift
3. E E*E
$E+ Shift
4. E num
5. E id $ E + num Reduce (rule 4)
$E+E Shift
$E+E* Shift
$ E + E * id2 Reduce (rule 5)
Input to parse:
id1 + num * id2 $E+E*E Reduce (rule 3)
$E+E Reduce (rule 2)
4 r1 r1 r1
5 r3 r3 r3
1. S ->AB
2. A -> a Step 3
3. B -> b
Example 2
Augmented Grammar:
Example 2: Solution
Example 2: Solution
Example 3
Grammar
Parsing Table
Example 3
Input: aabb$
Stack