Parsing
Parsing
Parsing
• Top-down parser:
• starts at the root of derivation tree and fills in
• picks a production and tries to match the input
• may require backtracking
• some grammars are backtrack-free (predictive)
• Bottom-up parser:
• starts at the leaves and fills in
• starts in a state valid for legal first tokens
• uses a stack to store both state and sentential forms
TOP DOWN PARSING
• A top-down parser starts with the root of the parse tree, labeled
with the start or goal symbol of the grammar.
Program:
E()
{
if(l==‘i’)
{
match(‘i’);
E’();
}
} l=getchar();
E’()
{
if(l==‘+”)
{
match(‘+’);
match(‘i’);
E’();
}
else
return ;
}
Match(char t)
{
if(l==t)
l=getchar();
else
printf(“Error”);
}
Main()
{
E();
If(l==‘$’)
{
printf(“parsing successful”);
}
}
PREDICTIVE LL(1) PARSING
• The first “L” in LL(1) refers to the fact that the input is processed from
left to right.
• The second “L” refers to the fact that LL(1) parsing determines a
leftmost derivation for the input string.
• The “1” in parentheses implies that LL(1) parsing uses only one
symbol of input to predict the next grammar rule that should be used.
• The data structures used by LL(1) are 1. Input buffer 2. Stack
3. Parsing table
• The construction of predictive LL(1) parser is bsed on two
very important functions and those are First and Follow.
• For construction of predictive LL(1) parser we have to follow
the following steps:
• STEP1: computate FIRST and FOLLOW function.
• STEP2: construct predictive parsing table using first and follow
function.
• STEP3: parse the input string with the help of predictive parsing
table
FIRST
• If X is a terminal then First(X) is just X!
• If there is a Production X → ε then add ε to first(X)
• If there is a Production X → Y1Y2..Yk then add first(Y1Y2..Yk)
to first(X)
• First(Y1Y2..Yk) is either
• First(Y1) (if First(Y1) doesn't contain ε)
• OR (if First(Y1) does contain ε) then First (Y1Y2..Yk) is everything in
First(Y1) <except for ε > as well as everything in First(Y2..Yk)
• If First(Y1) First(Y2)..First(Yk) all contain ε then add ε to
First(Y1Y2..Yk) as well.
FOLLOW
• First put $ (the end of input marker) in Follow(S) (S is the
start symbol)
• If there is a production A → aBb, (where a can be a whole
string) then everything in FIRST(b) except for ε is placed in
FOLLOW(B).
• If there is a production A → aB, then everything in
FOLLOW(A) is in FOLLOW(B)
• If there is a production A → aBb, where FIRST(b) contains
ε, then everything in FOLLOW(A) is in FOLLOW(B)
EXAMPLE OF FIRST AND FOLLOW
The Grammar
•E → TE'
•E' → +TE'
•E' → ε
•T → FT'
•T' → *FT'
•T' → ε
•F → (E)
•F → id
PROPERTIES OF LL(1) GRAMMARS
1. No left-recursive grammar is LL(1)
2. No ambiguous grammar is LL(1)
3. Some languages have no LL(1) grammar
4. A ε–free grammar where each alternative expansion for A begins with a distinct terminal is a
simple LL(1) grammar.
Example:
S → aS | a
is not LL(1) because FIRST(aS) = FIRST(a) = { a }
S → aS´
S´ → aS | ε
accepts the same language and is LL(1)
PREDICTIVE PARSING TABLE
Method:
1.∀ production A → α:
α) ∀a ∈ FIRST(α), add A → α to M[A,a]
b) If ε ∈ FIRST(α):
Ι. ∀b ∈ FOLLOW(A), add A → α to M[A,b]
II. If $ ∈ FOLLOW(A), add A → α to M[A,$]
2.Set each undefined entry of M to error