Professional Documents
Culture Documents
Chapter 3 (Topdownparsing) Mod
Chapter 3 (Topdownparsing) Mod
Parsing Stack
Parsing
Table
LL Parsing Actions
The LL parsing actions are:
- Match: to match top of parser stack with next input token
- Predict: to predict a production and apply it in a derivation step
- Accept: to accept and terminate the parsing of a sequence of tokens
- Error: to report an error message when matching or prediction fails
Consider the following grammar:S(S)S|
Parser Stack Input Parser Action
S (())$ Predict S ( S ) S
(S)S (())$ Match (
Parsing of ( ( ) ) S)S ())$ Predict S ( S ) S
(S)S)S ())$ Match (
Stack grows backward S)S)S ))$ Predict S
from right to left )S)S ))$ Match )
S)S )$ Predict S
)S )$ Match )
S $ Predict S
Empty $ Accept
Grammar Analysis: Nonterminals that Derive
v Grammar analysis is necessary to
- Determine whether a grammar can be used in LL parsing
- Construct the LL parsing table that defines the actions of an LL parser
v A common analysis is to determine which nonterminals derive
- Nonterminals that derive are called nullable
v To determine which nonterminals derive …
- We use an iterative marking algorithm
- First, nonterminals that derive directly in one step are marked
- Nonterminals that derive in two, three, … steps are found and marked
- Continue until no more nonterminals can be marked as deriving
v Consider the following grammar
ABCD
A, B, C, and D are all nullable
BbC|
B, C, and D derive directly
CcD|
Dd| A derives indirectly: A B C D C D D
Grammar Analysis: The Follow Set
v Suppose we have the following grammar
- We follow derivations of S as shown below …
S AcB aAcB…
A aA
A SAcB cbBaAcB…
B bBS cbBScbBAcB
B cB cbBcB…
c
We predictAaAwhen
- Next token is a because First(aA) = {a}
We predictAwhen
- Next token is c because Follow(A) = {c}
Similarly, we predictBbB Swhen
- Next token is b because First(b B S) = {b}
We predictBwhen
- Next token is a, c, or $ (end-of-file token) because Follow(B) = {a, c, $}
Top-Down Parsing – 15 Compiler Design – © Muhammed Mudawwar
Grammar Analysis: The First Set
v Suppose we have the following grammar:
- The RHS of the productions of S do not begin with terminals
- Parser has no immediate guidance which production to apply to expand S
- We may follow all possible derivations of S as shown below
h c a
SAa| Bb Dca
ADc| CA Aa
i c a
Parsing Stack
for i from n downto 1 do Stack.push(Yi) end for else process
a syntax error at Token end if
end if
end while
if Token = $ then accept parsing
else report a syntax error at Token end if
Scanner
next token
Tracing an LL(1) Parser Parser Stack Input Parser Action
E id*(id+id)$ Predict E T Q
Consider the parsing of id * (id + id) $ TQ id*(id+id)$ Predict T F R
FRQ id*(id+id)$ Predict F id
id R Q id*(id+id)$ Match id
to left
RQ *(id+id)$ Predict R * F R
1: E T Q 6: R * F R
*FRQ *(id+id)$ Match *
2: Q + T Q 7: R / F R
FRQ (id+id)$ Predict F ( E )
3: Q – T Q 8: R
from right
(E)RQ (id+id)$ Match (
4: Q 9: F ( E ) E)RQ id+id)$ Predict E T Q
TQ)RQ id+id)$ Predict T F R
5: T FR 10: F id
FRQ)RQ id+id)$ Predict F id
id R Q ) R Q id+id)$ Match id
RQ)RQ +id)$ Predict R
+ – * / ( ) id $ Q)RQ +id)$ Predict Q + T Q