Top Down Parsing
Top Down Parsing
c A d A -> a
Next rule to parse A is taken
A->a, turns out CORRECT ,
Parser stops a
Mrs.Soma Ghosh (gsn.comp@co
eptech.ac.in)
Predictive parser
• It is a recursive-descent parser that needs
no backtracking
• Suppose
A -> A1 | A2 | ….| An
• If the non-terminal to be expanded next is
‘A’ , then the choice of rule is made on the
basis of the current input symbol ‘a’ .
• T -> *F T’’ | T’
+
T T
• T -> (E) |id T ( E )
+ *
T F
START
FINAL
T ( E )
id
Mrs.Soma Ghosh (gsn.comp@co
eptech.ac.in)
SIMULATION METHOD
• Start from the start state
• If a terminal comes consume it, move to next
state
• If a non – terminal comes go to the state of the
“dfa” of the non-term and return on reaching the
final state
• Return to the original “dfa” and continue parsing
• If on completion( reading input string
completely), you reach a final state, string is
successfully parsed.
Mrs.Soma Ghosh (gsn.comp@co
eptech.ac.in)
Disadvantage :
• It is inherently a recursive parser, so it
consumes a lot of memory as the stack
grows.
• To remove this recursion, we use
LL-parser, which uses a table for lookup.
id + * ( ) $
Grammar
ETE'
E'+TE' | ë
T FT'
T'*FT' | ë
F(E) | id
( ë stands for epsilon)
S Sa SiEtS
S’
S’ Së Së
Se
S
E Eb
Ambiguity
Mrs.Soma Ghosh (gsn.comp@co
eptech.ac.in)
The grammar is ambiguous and it
is evident by the fact that we have
two entries corresponding to
M[S’,e] containing S € and S’
eS. This ambiguity can be
resolved if we choose
S’eS i.e associating the else’s with the closest previous “then”.