Syntax Analysis: Bottom-Up Parsing
Syntax Analysis: Bottom-Up Parsing
Shift-Reduce Parsing
Lecture 5
Haythem
c O. Ismail Bottom-Up Parsing 1 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Objectives
Haythem
c O. Ismail Bottom-Up Parsing 2 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Outline
1 Bottom-Up Parsing
2 Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 3 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Outline
1 Bottom-Up Parsing
2 Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 4 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Definition
Bottom-up parsing consists in the construction of a parse tree starting
with the leaves up towards the root.
Haythem
c O. Ismail Bottom-Up Parsing 5 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Definition
Bottom-up parsing consists in the construction of a parse tree starting
with the leaves up towards the root.
Haythem
c O. Ismail Bottom-Up Parsing 5 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Example: G2
Example
E −→ E + T | T
T −→ T ∗ F | F
F −→ ( E ) | id | number
Input: id ∗ id.
Haythem
c O. Ismail Bottom-Up Parsing 6 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Example
c Aho et al. (2007)
Haythem
c O. Ismail Bottom-Up Parsing 7 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Reductions
Example
Haythem
c O. Ismail Bottom-Up Parsing 8 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Reductions
Example
Haythem
c O. Ismail Bottom-Up Parsing 8 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Definition
Given a CFG G = hV, Σ, R, Si and w ∈ Σ∗ , bottom-up parsing, with a
left-to-right scan of the input, is the problem of constructing a
reduction from w to S such that if
αγβ 7→ αAβ
δ1 δ2 δ3 λ 7→ δ1 Bδ3 λ
∗
where Aβ 7→ λ and δ1 δ2 δ3 = α.
Definition
Given a CFG G = hV, Σ, R, Si and w ∈ Σ∗ , bottom-up parsing, with a
left-to-right scan of the input, is the problem of constructing a
reduction from w to S such that if
αγβ 7→ αAβ
δ1 δ2 δ3 λ 7→ δ1 Bδ3 λ
∗
where Aβ 7→ λ and δ1 δ2 δ3 = α.
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Outline
1 Bottom-Up Parsing
2 Shift-Reduce Parsing
Haythem
c O. Ismail Bottom-Up Parsing 11 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
PDA Perspective
Haythem
c O. Ismail Bottom-Up Parsing 12 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Exercise
Example
Draw the state diagram of the shift-reduce PDA which is equivalent to
the CFG G2 .
Do it yourself.
Haythem
c O. Ismail Bottom-Up Parsing 13 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Exercise (Cont’d)
Example
c Aho et al. (2007)
Haythem
c O. Ismail Bottom-Up Parsing 14 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Conflicts
Haythem
c O. Ismail Bottom-Up Parsing 15 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Example
stmnt −→ if bexpr then stmnt
stmnt −→ if bexpr then stmnt else stmnt
stmnt −→ . . .
.. ..
. .
bexpr −→ . . .
.. ..
. .
What should the PDA do in the following configuration?
S TACK I NPUT
. . . if bexpr then stmnt else . . .
Haythem
c O. Ismail Bottom-Up Parsing 16 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Example
This is a shift/reduce conflict.
We may impose the common disambiguation strategy by
preferring shifting over reducing.
Haythem
c O. Ismail Bottom-Up Parsing 17 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Example
Haythem
c O. Ismail Bottom-Up Parsing 18 / 19
Bottom-Up Parsing
Shift-Reduce Parsing
Example
Assume the PDA can cheat and detect that shifting is no good.
Which rule should be used to reduce id?
This is a reduce/reduce conflict.
Haythem
c O. Ismail Bottom-Up Parsing 19 / 19