Top Down Parsing
Top Down Parsing
Top down parsing is an attempt to construct a parse tree for the input string, starting from the root . Top down parsing attempts to find left most derivation of string An unambiguous grammar does not alone guarantee that it is suitable for top down parsing For top down (predictive parsers) parsers to work, grammar should not be left recursive and should be left factored
When Top down parsing doesnt Work Well Consider productions S p S a | a: In the process of parsing S we try the above rules Applied consistently in this order, get infinite loop Could re-order productions, but search will have lots of backtracking and defining a general rule for ordering will become complex Problem here is left-recursive grammar:
Left Recursion
E E + T + T
Elimination of Left recursion Consider the left-recursive grammar SpSE|F S generates all strings starting with a F and followed by a number of E Can rewrite using right-recursion S p F S S p E S | I
4
Elimination of left Recursion. Example Consider the grammar S p 1 | S 0 ( F = 1 and E = 0 ) can be rewritten as S p 1 S S p 0 S | I
More Elimination of Left Recursion In general S p S E1 | | S En | F1 | | Fm All strings derived from S start with one of F1,,Fm and continue with several instances of E1,,En Rewrite as S p F1 S | | Fm S S p E1 S | | En S | I
General Left Recursion The grammar SpAE|H (1) ApSF (2) is also left-recursive because S p+ S F E This left recursion can also be eliminated by first substituting (2) into (1) There is a general algorithm (e.g. Aho, Sethi, Ullman 4.3)
7
General Left Recursion The grammar SpAE|H (1) ApSF (2) is also left-recursive because S p+ S F E This left recursion can also be eliminated by first substituting (2) into (1) There is a general algorithm (e.g. Aho, Sethi, Ullman 4.3)
8
Top-Down Parsing
Top-down parser Recursive-Descent Parsing Backtracking is needed (If a choice of a production rule does not work, we backtrack to try other alternatives.) It is a general parsing technique, but not widely used. Not efficient Predictive Parsing no backtracking efficient needs a special form of grammars (LL(1) grammars). Predictive Parsing is a special form of Recursive Descent parsing without backtracking. Non-Recursive (Table Driven) Predictive Parser is also known as LL(1) parser.
9
10
11
Left Factoring Consider the grammar EpT+E|T T p int | int * T | ( E ) Impossible to predict because For T two productions start with int For E it is not clear how to predict
Left-Factoring Example
Starting with the grammar E T+E|T T int | int * T | ( E )
Left-Factoring (cont.)
In general, A p EF1 | EF2 where E is non-empty and the first symbols of F1 and F2 (if they have one)are different. when processing E we cannot know whether expand A to EF1 or A to EF2 But, if we re-write the grammar as follows A p EA A p F1 | F2 so, we can immediately expand A to EA
14
Left-Factoring -- Algorithm
For each non-terminal A with two or more alternatives (production rules) with a common non-empty prefix, let say A p EF1 | ... | EFn | K1 | ... | Km convert it into A p EA | K1 | ... | Km A p F1 | ... | Fn
15
Left-Factoring Example1
A p abB | aB | cdg | cdeB | cdfB left factor the given grammar
16
A p aA | cdA A p bB | B A p g | eB | fB
17
Predictive Parser
a grammar
eliminate left recursion left factor
When re-writing a non-terminal in a derivation step, a predictive parser can uniquely choose a production rule by just looking the current symbol in the input string. A p E1 | ... | En input: ... a ....... current token
18
When we are trying to write the non-terminal stmt, if the current token is if we have to choose first production rule. When we are trying to write the non-terminal stmt, we can uniquely choose the production rule by just looking the current token. We eliminate the left recursion in the grammar, and left factor it. But it may not be suitable for predictive parsing (not LL(1) grammar).
19
output
LL(1) Parser
input buffer
our string to be parsed. We will assume that its end is marked with a special symbol $.
output
a production rule representing a step of the derivation sequence (left-most derivation) of the string in the input buffer.
stack
contains the grammar symbols at the bottom of the stack, there is a special end marker symbol $. initially the stack contains only the symbol $ and the starting symbol S. $S initial stack when the stack is emptied (ie. only $ left in the stack), the parsing is completed.
parsing table
a two-dimensional array M[A,a] each row is a non-terminal symbol each column is a terminal symbol or the special symbol $ each entry holds a production rule.
21
If X and a are the same terminal symbol (different from $) parser pops X from the stack, and moves the next symbol in the input buffer. If X is a non-terminal parser looks at the parsing table entry M[X,a]. If M[X,a] holds a production rule XpY1Y2...Yk, it pops X from the stack and pushes Yk,Yk-1,...,Y1 into the stack. The parser also outputs the production rule XpY1Y2...Yk to represent a step of the derivation. none of the above
4.
error
all empty entries in the parsing table are errors. If X is a terminal symbol different from a, this is also an error case.
22
input
abba$ abba$ bba$ bba$ ba$ ba$ a$ a$ $
23
Derivation(left-most): SaBaabBaabbBaabba
S
parse tree
a B a
b b
B B
I
24
( E p TE
) E p I
$ E p I T p I
T p FT T p I
26
FIRST(E) is a set of the terminal symbols which occur as first symbols in strings derived from E where E is any string of grammar symbols. if E derives to I, then I is also in FIRST(E) . FOLLOW(A) is the set of the terminals which occur immediately after (follow) the non-terminal A in the strings derived from the starting symbol. * a terminal a is in FOLLOW(A) if S EAaF * $ is in FOLLOW(A) if S EA
27
FIRST Example
E p TE E p +TE | I T p FT T p *FT | I F p (E) | id FIRST(F) = {(,id} FIRST(T) = {*, I} FIRST(T) = {(,id} FIRST(E) = {+, I} FIRST(E) = {(,id} FIRST(TE) = {(,id} FIRST(+TE ) = {+} FIRST(I) = {I} FIRST(FT) = {(,id} FIRST(*FT) = {*} FIRST(I) = {I} FIRST((E)) = {(} FIRST(id) = {id}
29
if A p EBF is a production rule everything in FIRST(F) is FOLLOW(B) except I If ( A p EB is a production rule ) or ( A p EBF is a production rule and I is in FIRST(F) ) everything in FOLLOW(A) is in FOLLOW(B). We apply these rules until nothing more can be added to any follow set.
30
FOLLOW Example
E p TE E p +TE | I T p FT T p *FT | I F p (E) | id FOLLOW(E) = { $, ) } FOLLOW(E) = { $, ) } FOLLOW(T) = { +, ), $ } FOLLOW(T) = { +, ), $ } FOLLOW(F) = {+, *, ), $ }
31
32
T p FT T p *FT T p I
FIRST(I)={I} none but since I in FIRST(I) and FOLLOW(T)={$,),+} T p I into M[T,$], M[T,)] and M[T,+] FIRST((E) )={(} FIRST(id)={id} F p (E) into M[F,(] F p id into M[F,id]
33
F p (E) F p id
( E p TE
) E p I
$ E p I T p I
T p FT T p I
LL(1) Grammars
A grammar whose parsing table has no multiply-defined entries is said to be LL(1) grammar.
one input symbol used as a look-head symbol do determine parser action
LL(1)
The parsing table of a grammar may contain more than one production rule. In this case, we say that it is not a LL(1) grammar.
35
36
e
EpeS EpI
i
S p iCtSE
$
EpI
39
40
Error-Productions
If we have a good idea of the common errors that might be encountered, we can augment the grammar with productions that generate erroneous constructs. When an error production is used by the parser, we can generate appropriate error diagnostics. Since it is almost impossible to know all the errors that can be made by the programmers, this method is not practical.
Global-Correction
Ideally, we we would like a compiler to make as few change as possible in processing incorrect inputs. We have to globally analyze the input to find the error. This is an expensive method, and it is not in practice.
41
42
id E E T T F E p TE T p FT F p id
( E p TE T p FT
T p *FT synch
F p (E)
43
If M[A,a] is blank, skip the input symbol If M[A,a] is synch, pop Non terminal from TOS & continue parsing If terminal on TOS mismatch current token, pop terminal from TOS
44
We should be careful when we design these error routines, because we may put the parser into an infinite loop.
45