Title: Lecture 7 Predictive Parsing
1Lecture 7 Predictive Parsing
CSCE 531 Compiler Construction
- Topics
- Review Top Down Parsing
- First
- Follow
- LL (1) Table construction
- Readings 4.4
- Homework Program 2
February 1, 2006
2Overview
- Last Time
- Ambiguity in classic programming language
grammars - Expressions
- If-Then-Else
- Top-Down Parsing
- Modifying Grammars to facilitate Top-down parsing
- Todays Lecture
- Regroup halfway to Test 1
- First and Follow
- LL(1) property
- References
- Homework
3Removing the IF-ELSE Ambiguity
- Stmt ? if Expr then Stmt
- if Expr then Stmt else Stmt
- other stmts
- Stmt ? MatchedStmt UnmatchedStmt
- MatchedStmt ? if Expr then MatchedStmt else
MatchedStmt - OthersStatements
- UnmatchedStmt ? if Expr then MatchedStmt else
- if Expr then MatchedStmt else UmatchedStmt
4Recursive Descent Parsers
- Recall the parser from Chapter 2
- A recursive descent parser has a routine for each
nonterminal. These routines can call each other.
If one of these fails then it may backtrack to a
point where there is an alternative choice. - In certain cases the grammar is restricted enough
where backtracking would never be required. - Such a parser is called a predictive parser.
- The parser from Chapter 2 is a predictive parser.
5Transition Diagrams for Predictive Parsers
- To construct the transition diagram for a
predictive parser - Eliminate left recursion from the grammar
- Left factor the grammar
- For each nonterminal A do
- Create an initial state and final state.
- For each production A ? X1X2 Xn create a path
from the initial state to the final state
labeled X1X2 Xn - end
6Example
- E ? T E
- E ? T E - T E e
- T ? F T
- T ? F T / F T e
- F ? id num ( E )
e
E
E
T
E
T
E
1
2
3
1
2
3
3
E
-
T
T
2
3
F
T
4
5
6
Etcetera Some of the rest in the text.
7Predictive Parsing using Transition Diagrams
8Table Driven Predictive Parsing
input
x (
Predictive Parsing Program
output
Stack
W
X
Y
S
R
Parsing Table M
9Table Driven Predictive Parsing
- The stack is initialized to contain S, the is
the bottom marker. - The input has a added to the end.
- The parse table, MX, a contains what should be
done when we see nonterminal X on the stack and
current token a - Parse Actions for
- X top of stack, and
- a current token
- If X a then halt and announce success.
- If X a ! then pop X off the stack and
advance the input pointer to the next token. - If X is nonterminal consult the table entry MX,
a, details on next slide.
10MX, a Actions
- If X is nonterminal then consult MX, a.
- The entry will be either a production or an error
entry. - If MX, a X ? UVW the parser
- replaces X on the top of the stack
- with W, V, U with the U on the top
- As output print the name of the production used.
11Algorithm 4.3
- Set ip to the first token in w.
- Repeat
- Let X be the top of the stack and a be the
current token - if X is a terminal or then
- if X a then
- pop X from the stack and advance the ip
- else error()
- else / X is a nonterminal /
- if MX, a X ? Y1Y2 Yk then begin
- pop X from the stack
- push Yk Yk-1 Y2Y1 onto the stack with Y1 on
top - output the production X ? Y1Y2 Yk
- end
- else error()
- Until X
12Parse Table for Expression Grammar
id - / ( )
E E?TE E?TE
E E?TE E?-TE E?e E?e
T T?FT T?FT
T T?e T?e T?FT T?/FT T?e T?e
F F?id F?(E)
Figure 4.15
13Parse Trace of (z q) x w y
Stack Input Output
E ( id id ) id id id
ET ( id id ) id id id E?T E
ETF ( id id ) id id id T?F T
ET)E( ( id id ) id id id F?( E )
ET)E id id ) id id id
ET)ET id id ) id id id E?T E
ET)ETF id id ) id id id T?F T
ET)ETid id id ) id id id F?id
ET)ET id ) id id id
ET)E id ) id id id T?e
14First and Follow Functions
- We are going to develop two auxilliary functions
for facilitating the computing of parse tables. - FIRST(a) is the set of tokens that can start
strings derivable from a, - also if a ? e then we add e to First(a).
- FOLLOW(N) is the set of tokens that can follow
the nonterminal N in some sentential form, i.e., - FOLLOW(N) t S ? aNtß
15Algorithm to Compute First
- Input Grammar symbol X
- Output FIRST(X)
- Method
- If X is a terminal, then FIRST(X) X
- If X ? ? is a production, then add ? to FIRST(X).
- For each production X ? Y1Y2 Yk
- If Y1Y2 Yi-1 ? ? then add all tokens in
FIRST(Yi) to FIRST(X) - If Y1Y2 Yk ? ? then add ? to FIRST(X)
16Example of First Calculation
FIRST(token) token for tokens - / ( ) id
num FIRST(F) id, num, ( FIRST(T) ? T??
so T? FT so T? /FT so FIRST(T)
? FIRST(T) FIRST(F) FIRST(E)
? FIRST(E) ?
- E ? T E
- E ? T E - T E ?
- T ? F T
- T ? F T / F T ?
- F ? id num ( E )
17Algorithm to Compute Follow (p 189)
- Input nonterminal A
- Output FOLLOW(A)
- Method
- Add to FOLLOW(S), where
- is the end_of_input marker
- And S is the start state
- If A ? aBß is a production, then every token in
FIRST(ß) is added to FOLLOW(B) (note not ?) - If A ? aB is a production or
- if A ? aBß is a production and ß ? ? then every
- token in FOLLOW(A) is added to FOLLOW(B)
18Example of FOLLOW Calculation
- Add to FOLLOW(E)
- E? TE
- Add FIRST(E) to FOLLOW(T)
- E? T E (similarly E?T E)
- Add FIRST(E) to FOLLOW(T)
- E??, so FOLLOW(E) is added to FOLLOW(T)
- T?F T
- Add FIRST(T) to FOLLOW(F)
- T??, so FOLLOW(T) is added to FOLLOW(F)
- F?( E )
- Add FIRST( ) ) to FOLLOW(E)
- E ? T E
- E ? T E - T E ?
- T ? F T
- T ? F T / F T ?
- F ? id num ( E )
N FOLLOW(N)
E
E
T -
T
F
19Construction of a Predictive Parse Table
- Algorithm 4.4
- Input Grammar G
- Output Predictive Parsing Table MN, a
- Method
- For each production A?a do
- For each a in FIRST(a), add A?a to
MA, a - If ? is in FIRST(a), add A?a to MA,
b for each token b in FOLLOW(A) If ?
is in FIRST(a) and is in FOLLOW(A) then
add A?a to MA, - Mark all other entries of M as error
20Predictive Parsing Example
- Example 4.18 in text table in Figure 4.15 (slide
11) - Example 4.19
- S ? iEtSS a
- S ? eS ?
- E ? b
FIRST(S) i, a FIRST(S) ?, e FIRST(E)
b FOLLOW(S) , e FOLLOW(S) ,
e FOLLOW(E) t
Nonter-minals a b e i t
S S?a S?iEtSS
S S?eS S?? S??
E E?b
21LL(1) Grammars
- A grammar is called LL(1) if its parsing table
has no multiply defined entries. - LL(1) grammars
- Must not be ambiguous.
- Must not be left-recursive.
- G is LL(1) if and only if whenever A ? a ß
- FIRST(a) n FIRST(ß) F
- At most one of a and ß can derive ?
- If ß ? ? then FIRST(a) n FOLLOW(A) F
22Error Recovery in Predictive Parsing
- Panic Mode Error recovery
- If MA, a is an error, then throw away input
tokens until one in a synchronizing set. - Heuristics for the synchronizing sets for A
- Add FOLLOW(A) to the synchronizing set for A
- If is a separator or terminator of statements
then keywords that can begin statements should
not be in synchronizing set for the nonterminal
Expr because a missing would cause skipping
keywords. -
23Parse Table with Synch Entries
24Trace with Error Recovery
25Bottom up Parsing
- Idea recognize right hand sides of productions
so that we produce a rightmost derivation - Handle-pruning
26Reductions in a Shift-Reduce Parser
- Figure 4.21
- E ? E E E E ( E ) id
Right-Sentential Form Handle Reducing Production
id1 id2 id3 id1 E ? id
E id2 id3 id2 E ? id
E E id3 id3 E ? id How?
E E E E E E ? E E
E E E E E ? E E
E
27(No Transcript)