Title: Lecture 8 Bottom Up Parsing
1Lecture 8 Bottom Up Parsing
CSCE 531 Compiler Construction
- Topics
- Overview Bottom-Up Parsing
- Handles
- Shift-reduce parsing
- Operator precedence parsing
- Readings 4.6
- Homework Test 1 Feb 15
February 6, 2006
2Overview
- Last Time
- First and Follow
- LL(1) property
- Todays Lecture
- Panic mode error recovery in Predictive parsing
- Overview Bottom-Up Parsing
- Handles
- Shift-reduce parsing
- Operator precedence parsing
- Homework
- LL(1) table for core (pdf email handout) grammar
- Test 1 Feb 15
3Panic Mode Error Recovery for LL(1)
4Recall Bottom-up Parsing
- Bottom-up parsers
- Start at the leaves and grow tree toward root
- As input is consumed, encode possibilities on a
stack
5Bottom-up Parsing Terminology
- A derivation consists of a series of rewrite
steps - S ? ?0 ? ?1 ? ?2 ? ? ?n1 ? ?n
- Each ?i is a sentential form and if ?n ? T (
tokens ) then it is a sentence - To get ?i from ?i1, expand some A ? ?i1 by
using A ?? - i.e., Replace the occurrence of A ? ?i1 with ?
to get ?i - In a rightmost derivation, A would be the last
nonterminal in ?i1
6Bottom-up Parsing
- In finding a rightmost derivation
- S ? ?0 ? ?1 ? ?i1 ? ?i ? ?n1 ? ?n
- At each step we need to find a place where the
right-hand side of a production occurs and
replace it with the non-terminal on the LHS. - Again we are building the parse tree from leaves
to root - As we are constructing it is not always connected
- Nodes with no parent in the partially
constructed tree form its upper fringe - Since each replacement of ? with A shrinks the
upper fringe, we call it a reduction.
7Example of Indentifying Reductions
- E? EE E E ( E ) id num
- Rightmost derivation of x (z w) with
token sequence id ( id id ) - E?E E ? E ( E ) ? E ( E E ) ? E ( E
id ) ? E (idid) ? id ( id id ) - Frontier id ( id id )
8Finding Handles
- The parser must find a substring ? in the trees
frontier that matches some production A ? ? that
occurs as one step in the rightmost derivation
- We call this substring ? a handle
- S ?rm ?1 ? rm ?i1aAw ? rm a?w ?i ? rm ?
rm ?n - Because ?i is a right-sentential form, the
substring to the right of a handle (w) contains
only tokens
9Figure 4.20 a Tree with Handle A?ß
S
A
a a1 a2 ak
ß
w w1 w2 ws
10Grammar Unambiguous ? Unique Handles
- Sketch of Proof
- G is unambiguous implies
- rightmost derivation is unique, which implies
- there is a unique production A ? ? applied to
derive ?i from ?i1 - and a unique position k at which A?? is applied
- thus the handle is unique
- This all follows from the definitions
11Shift-reduce Parsing
- A parser based on recognizing handles is called a
Shift Reduce parsers - A shift-reduce parser has just four actions
- Shift the next token is pushed (shifted) onto
the stack - Reduce the right end of a handle is on the top
of stack - Locate the left end of the handle within the
stack - Pop the handle off stack push appropriate
lhs - Accept stop parsing report success
- Error call an error handling/recovery routine
12Shift-reduce Parsing
- Accept - finish up
- Error - error recovery, panic mode again
- Shift
- push the current token
- call to the scanner to get the next token
- Reducing by A ? ß requires
- Popping ß from the stack
- Push A
13Stack Implementation of Shift-Reduce Parsers
Stack Input Action
id ( id id ) Shift id
id ( id id ) Reduce E?id
E ( id id ) Shift
E ( id id ) Shift (
E ( id id ) Shift id
E ( id id ) Reduce E?id
E ( E id ) Shift
E ( E id ) Shift id
E ( E id ) Reduce E?id
E ( E E ) Reduce E?EE
E ( E ) Shift )
E ( E ) Reduce E? ( E )
E E Reduce E? ( E )
14More Examples
- Now what about
- id ( id id ) id
- See fig 4.22 for another example
15Justification of the Stack Implementation
- Consider two derivations
- S ? ? aAz ? aßByz ? ? aßµyz
- Rewriting by A? ßBy
- S ? ? aBxAz ? aBxyz ? ? aµxyz
- Rewriting by A? y
16Justification of the Stack Implementation
- Consider the first derivation
- S ? ? aAz ? aßByz ? aßµyz
- Rewriting by A? ßBy, and then B ? µ
Stack Input Actions
a ß µ yz Reduce by B? µ
a ß B yz
- Since B is the rightmost nonterminal in aßByz,
the handle cannot be all in the stack - So the parser can shift the string of tokens y
onto the stack
Stack Input Actions
a ß B y z Reduce by A? ßBy
a A z
17Justification of the Stack Implementation
- Consider the second derivation
- (2) S ? ? aBxAz ? aBxyz ? aµxyz
- Rewriting by A? y and then B ? µ
Stack Input Actions
a µ xyz Reduce by B? µ
a B xyz
- Now the parser can shift the string of tokens xy
onto the stack
Stack Input Actions
a B x y z Reduce by A? y
a B x A z
18An Important Lesson about Handles
- To be a handle, a substring of a sentential form
? must have two properties - It must match the right hand side ? of some rule
A ? ? - There must be some rightmost derivation from the
goal symbol that produces the sentential form ?
with A ? ? as the last production applied - Simply looking for right hand sides that match
strings is not good enough - Critical Question How can we know when we have
found a handle without generating lots of
different derivations? - Answer we use look ahead in the grammar along
with tables produced as the result of analyzing
the grammar. - LR(1) parsers build a DFA that runs over the
stack finds them
19An Important Lesson about Handles
- To be a handle, a substring of a sentential form
? must have two properties - It must match the right hand side ? of some rule
A ? ? - There must be some rightmost derivation from the
goal symbol that produces the sentential form ?
with A ? ? as the last production applied - We have seen that simply looking for right hand
sides that match strings is not good enough - Critical Question How can we know when we have
found a handle without generating lots of
different derivations? - Answer we use look ahead in the grammar along
with tables produced as the result of analyzing
the grammar. - There are a number of different ways to do this.
- We will look at two operator precedence and LR
parsing
20Model of an LR Parser
input
a1 ai an
output
LR Parsing Program
Stack
sm
Xm
sm-1
Xm-1
s0
Parsing Table Action goto
21LR Parsing Algorithm
22Expression LR-Parsing Table fig 4.31
State Action goto
Id ( ) E T F
0 S5 S4 1 2 3
1 S6 accept
2 R2 S7 R2
3 R4 R4 R4
4 S5 S4 8 2 3
5 R6 R6 R6
6 S5 S4 9 3
7 S5 S4 10
8 S6 S11
9 R1 S7 R1 R1
10 R3 R3 R3 R3
11 R5 R5 R5 R5
23LR Parse of id ( id id ) id
Stack Input Action
0 id ( id id ) id Shift id