Compiler Design Syntax Analysis Top Down
Compiler Design Syntax Analysis Top Down
Outline
• Role of parser
• Context free grammars
• Top down parsing
The role of parser
token
Source Lexical Parse tree Rest of Front Intermediate
Parser
program Analyzer End representation
getNext
Token
Symbol
table
Error handling
• -(id+id)
• E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)
Ambiguity
• Idea:
• A statement appearing between a then and an else must be matched
Elimination of left recursion
• Algorithm
• For each non-terminal A, find the longest prefix α
common to two or more of its alternatives. If α<> ɛ, then
replace all of A-productions A->αβ1 |αβ2 | … | αβn |
γ by
• A -> αA’ | γ
• A’ -> β1 |β2 | … | βn
• Example:
• S -> I E t S | i E t S e S | a
• E -> b
Top Down Parsing
Introduction
• A Top-down parser tries to create a parse tree from the root towards
the leafs scanning input from left to right
• It can be also viewed as finding a leftmost derivation for an input string
• Example: id+id*id
E E E E
E -> TE’ lm lm
E
lm
E
lm lm
E’ -> +TE’ | Ɛ T E’ T E’ T E’ T E’ T E’
T -> FT’
T’ -> *FT’ | Ɛ F T’ F T’ F T’ F T’ + T E’
F -> (E) | id id id Ɛ id Ɛ
Recursive descent parsing
void A() {
choose an A-production, A->X1X2..Xk
for (i=1 to k) {
if (Xi is a nonterminal
call procedure Xi();
else if (Xi equals the current input symbol a)
advance the input to the next symbol;
else /* an error has occurred */
}
}
Recursive descent parsing (cont)
• General recursive descent may require backtracking
• The previous code needs to be modified to allow backtracking
• In general form it cant choose an A-production easily.
• So we need to try all alternatives
• If one failed the input pointer needs to be reset and another
alternative should be tried
• Recursive descent parsers cant be used for left-recursive grammars
Example
S->cAd
A->ab | a Input: cad
S S S
c A d c A d c A d
a b a
First and Follow
*
Computing follow
S -> iEtSS’ | a
S’ -> eS | Ɛ
E -> b
Input Symbol
Non -
terminal a b e i t $
S S -> a S -> iEtSS’
S’ S’ -> Ɛ S’ -> Ɛ
S’ -> eS
E E -> b
Non-recursive predicting
parsing
a + b $
Predictive
parsing output
stack X
Y program
Z
$
Parsing
Table
M
Predictive parsing algorithm
• id+id*id$