0% found this document useful (0 votes)
162 views76 pages

10-11-12-13-Top Down Parser

Here are the procedures for a recursive descent parser for the given grammar: Procedure E() { T(); E'(); } Procedure E'() { if(input symbol = '+') { advance(); T(); E'(); } } Procedure T() { F(); T'(); } Procedure T'() { if(input symbol = '*') { advance(); F(); T'(); } } Procedure F() { if(input symbol = '(') { advance(); E(); if(input symbol = ')') advance(); } else if(input symbol = id) advance();

Uploaded by

RAHUL DHANOLA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views76 pages

10-11-12-13-Top Down Parser

Here are the procedures for a recursive descent parser for the given grammar: Procedure E() { T(); E'(); } Procedure E'() { if(input symbol = '+') { advance(); T(); E'(); } } Procedure T() { F(); T'(); } Procedure T'() { if(input symbol = '*') { advance(); F(); T'(); } } Procedure F() { if(input symbol = '(') { advance(); E(); if(input symbol = ')') advance(); } else if(input symbol = id) advance();

Uploaded by

RAHUL DHANOLA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 76

Content of this lecture

• Introduction to Parsing Technique


• Top down parser

2
Parsing Techniques

• Parsing technique is used to check tokens generated are satisfying the grammatical
rules of the language or not, if they satisfy then corresponding Parse tree is
generated otherwise error is reported to error handler.
• Based on the methodology adopted for the generation of parse tree, parsing
techniques can be categorized in to two categories:
– Top down parser
– Bottom parser

3
Parsing Techniques

• Top Down Parser


– Input is scanned from left to right one symbol at a time
– Build the parse tree from top(root) to bottom(leaves)
– We have to decide which rule A → β should be applied to a node labelled A.
– Expanding A results in new nodes (children of A) labelled by symbols in β
– It can be also viewed as finding a leftmost derivation for an input string
• Bottom up Parser
– Input is scanned from left to right one symbol at a time
– Build the parse tree from bottom(leaves) to top(root).
– Input string will be reduced to starting symbol.
– Reducing rule A → β results in adding a node A to the tree. A’s children are the nodes labelled by
the symbols in β

4
Parsing Techniques

• Consider the grammar G1 = ({E, F, T}, {a, (,), ∗,+}, P, E) where the productions are:
E→E+T
E→T
T→T∗F
T→F
F → (E)
F→a
and the string x = a + a ∗ a

5
Parsing Techniques

• Top-down method: rules are considered in the same order as a leftmost derivation
E⇒E+T
E⇒T+T
E⇒F+T
E⇒a+T
E⇒a+T∗F
E⇒a+F∗F
E⇒a+a∗F
E⇒a+a∗a

6
Parsing Techniques

• Bottom-up method: rules are considered in the same order as a reverse rightmost
derivation
a+a∗a
⇐F+a∗a
⇐T+a∗a
⇐E+a∗a
⇐E+F∗a
⇐E+T∗a
⇐E+T∗F
⇐E+T
⇐E
7
Top Down Parsing Techniques

8
Top Down Parsing with Backtracking

• In this category, parser can make repeated scans of input.


• If required input string is not achieved by applying one production rule then another
production rule can be applied at each step to get the required string.

9
Top Down Parsing with Backtracking

10
Top Down Parsing with Backtracking Algorithm

• SaAd
• Abc|b

11
Top Down Parsing with Backtracking Algorithm

• SaAd Procedure S ()
{
• Abc|b if (input symbol = ‘a’) then
{
advance();
if A() then
{
if (input symbol=‘d’) then
{
advance();
declare success;
}
else error;
}
else error;
}
else error
}

12
Top Down Parsing with Backtracking Algorithm
Procedure A ()
{
• SaAd Procedure S ()
if (input symbol = ‘b’) then
{
• Abc|b if (input symbol = ‘a’) then
{
advance();
{
if (input symbol=‘c’) then
advance();
{
if A() then
advance();
{
declare success;
if (input symbol=‘d’) then
}
{
else error;
advance();
}
declare success;
if (input symbol = ‘b’) then
}
{
else error;
advance();
}
declare success;
else error;
}
}
else error;
else error
}
}

13
Top Down Parsing with Backtracking- Limitations

• Its not efficient as undoing semantic action requires lot of overhead.


• Entries made in the symbol table during parsing have to be removed while
backtracking.
• The order in which the alternative production rules are applied can also affect the
efficiency.
• Existence of left recursion will cause parser to enter into infinite loop. This must me
removed.
• Its difficult to locate the position of error on occurrence of failure.

14
Top Down Parsing without Backtracking

• In this category, once the production rule is applied, it can’t be undone. The parser
which follows top down parsing without backtracking are:
– Recursive Descent Parser
– Predictive Parser
– LL(1) parser

• Need to remove Left recursion and Left factoring from grammar before doing top
down parsing without backtracking.

15
Recursive Descent Parser

• It is defined as a parser that uses various recursive procedures to process the input
string with no backtracking.
• It can be easily implemented using a language which is recursive in nature.
• The first symbol of the string on R.H. S. of a production will uniquely determines the
correct alternative to choose.
• In this technique, there is no need for the procedures to return an indication of
success or failure since the calling procedure has no scope of trying another
alternate. Rather on failure, an error correcting routine is called.

16
Recursive Descent Parser

• Write down the algorithm using recursive procedure to implement following


grammar:
E -> TE’
E’ -> +TE’ | Ɛ
T -> FT’
T’ -> *FT’ | Ɛ
F -> (E) | id

17
Recursive Descent Parser

• Write down the algorithm using recursive procedure to implement following


grammar: Procedure E()
E -> TE’ {
E’ -> +TE’ | Ɛ T();
T -> FT’ E’();
T’ -> *FT’ | Ɛ }
F -> (E) | id

18
Recursive Descent Parser

• Write down the algorithm using recursive procedure to implement following


grammar: Procedure E()
E -> TE’ {
E’ -> +TE’ | Ɛ T();
T -> FT’ E’();
T’ -> *FT’ | Ɛ }
F -> (E) | id Procedure E’ ()
{
if (input symbol = +) then
{
advance();
T();
E’();
}
} 19
Recursive Descent Parser

• Write down the algorithm using recursive procedure to implement following


grammar: Procedure E() Procedure T()
E -> TE’ { {
E’ -> +TE’ | Ɛ T(); F();
T -> FT’ E’(); T’();
T’ -> *FT’ | Ɛ } }
F -> (E) | id Procedure E’ ()
{
if (input symbol = +) then
{
advance();
T();
E’();
}
} 20
Recursive Descent Parser

• Write down the algorithm using recursive procedure to implement following


grammar: Procedure E() Procedure T()
E -> TE’ { {
E’ -> +TE’ | Ɛ T(); F();
T -> FT’ E’(); T’();
T’ -> *FT’ | Ɛ } }
F -> (E) | id Procedure E’ () Procedure T’ ()
{ {
if (input symbol = +) then if (input symbol = *) then
{ {
advance(); advance();
T(); F();
E’(); T’();
} }
} } 21
Recursive Descent Parser

• Write down the algorithm using recursive procedure to implement following


grammar: Procedure E() Procedure T() Procedure F ()
E -> TE’ { { {
T(); F(); if (input symbol = ‘id’)
E’ -> +TE’ | Ɛ
T’(); {
T -> FT’ E’();
advance();
T’ -> *FT’ | Ɛ } }
}
F -> (E) | id Procedure E’ () Procedure T’ () else if (input symbol=‘(‘)
{ { {
if (input symbol = ‘+’) then if (input symbol = ‘*’) then advance();
{ E();
{
if (input symbol=‘)’)
advance(); advance();
advance();
T(); F();
else error();
E’(); T’(); }
} } else error()
} } } 22
Predictive Parser

• It is a top down backtracking less parser which generates the parse tree in LL(1)
manner.
• There should be no left recursion and left factoring in the grammar.

23
Components of Predictive Parser
• Input tape: which contains the string to be parsed, only terminals are there in the
input tape. $ symbol is there at the end of the string which indicates that string has
finished.
• Stack: which contains sequence of grammar symbols (terminals and non terminals
both) preceded by $ symbol. $ indicates the bottom of the stack.
• Parsing Table: It is a two dimensional array M[A, a] where A is representing the non
terminal and a is representing the terminal or $ symbol.
• Program: Parser is controlled by a program.

24
How Program controls Parser
• If current input symbol and top stack symbol are $ then parser halts and announce
successful completion of parsing.
• If current input symbol and top stack symbol matches but both are not $ then parser
pop off the top stack symbol and advances the input pointer to next input symbol.
• If top of the stack is Non terminal (X) and present input symbol is ‘a’ then program
consults entry M [X,a] in the parsing table and take the action accordingly.
– If this entry is an error entry then parser calls the error recovery routine.
– If M[X,a]={XUvW} then parser replaces the X on top of the stack by WvU where U will be top.
• Initial configuration of parser is Stack: $S Input: w$
• To fill the entries of parsing table, we use First() and Follow() functions.

25
Steps for finding First
• First(α) represents the set of the terminal symbols which occurs as first
symbol in the strings derived from α where α is any string of grammar
symbols.
– If Xa then First(X)=a
– If Xλ then First(X)= λ
– First of terminal will be that terminal itself
– If Xaα then First(X)=a
– If XY1Y2Y3…Yk then
• First(X)=First(Y1) ;if First(Y1) does not contain λ
• First(X)=First(Y1)-{λ} U First(Y2);if First(Y1) contain λ and First(Y2) does not contain λ
• First(X)=First(Y1)-{λ} U First(Y2)-{λ} U First(Y3);if First(Y1) and First(Y2) contain λ and First(Y3) does not
contain λ

26
Steps for finding Follow
• 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
– $ is in Follow(X) if X is start symbol
– If A αBβ and β≠ λ then
• If first(β) does not contain λ then Follow(B)={First(β)}
• If first(β) contains λ then Follow(B)={First(β)}-{λ} U Follow(A)
– If A αB then Follow(B)={Follow(A)}

27
Numerical
• Find first and follow for the following grammar
– SAaA|BbB
– AbB
– B ɛ

28
Numerical
• Find first and follow for the following grammar
– SAaA|BbB
– AbB
– B ɛ

– First(B)=ɛ
– First(A)=b
– First(S)=b

29
Numerical
• Find first and follow for the following grammar
– SAaA|BbB
– AbB
– B ɛ

– First(B)=ɛ Follow(B)={$, a, b}
– First(A)=b Follow(A)={$, a}
– First(S)=b Follow(S)={$}

30
Numerical
• Find first and follow for the following grammar
– EE+T|T
– TT*F|F
– F(E)|id

31
Numerical
• Find first and follow for the following grammar
– EE+T|T
– TT*F|F
– F(E)|id

32
Numerical
• Find first and follow for the following grammar
– EE+T|T
– TT*F|F
– F(E)|id

– First(E)={(,id}
– First(T)={(,id}
– First(F)={(,id}

33
Numerical
• Find first and follow for the following grammar
– EE+T|T
– TT*F|F
– F(E)|id

– First(E)={(,id} Follow(E)={$,+,)}
– First(T)={(,id} Follow(T)={$,+,),*}
– First(F)={(,id} Follow(F)={$,+,),*}

34
Numerical
• Find first and follow for the following grammar
– SL=R
– SR
– L*R
– Lid
– RL

35
Numerical
• Find first and follow for the following grammar
– SL=R
– SR
– L*R
– Lid
– RL

– First(S)={*,id}
– First(L)={*,id}
– First(R)={*,id}

36
Numerical
• Find first and follow for the following grammar
– SL=R
– SR
– L*R
– Lid
– RL

– First(S)={*,id} Follow(S)={$}
– First(L)={*,id} Follow(L)={$,=}
– First(R)={*,id} Follow(R)={$,=}

37
Numerical
• Find first and follow for the following grammar
– ETE`
– E`+TE`|ɛ
– TFT`
– T`*FT`|ɛ
– F(E)|id

38
Numerical
• Find first and follow for the following grammar
– ETE`
– E`+TE`|ɛ
– TFT`
– T`*FT`|ɛ
– F(E)|id

– First(E)={(,id}
– First(T)={(,id}
– First(F)={(,id}
– First(E`)={+, ɛ}
– First(T`)={*, ɛ}

39
Numerical
• Find first and follow for the following grammar
– ETE`
– E`+TE`|ɛ
– TFT`
– T`*FT`|ɛ
– F(E)|id

– First(E)={(,id} Follow(E)={),$}
– First(T)={(,id} Follow(T)={+,),$}
– First(F)={(,id} Follow(F)={+,*,),$}
– First(E`)={+, ɛ} Follow(E`)={),$}
– First(T`)={*, ɛ} Follow(T`)={+,),$}

40
Rules for filling the entries in Parsing Table
• For each production rule A->α in the grammar do the following:
– For each terminal a in First(α) add Aα in M[A, a]
– If ɛ is in First(α), and b is in Follow(A) then add Aα to M[A, b]
– If ɛ is in First(α), and $ is in Follow(A) then add Aα to M[A, $]
– All the remaining entries in the table are error.

41
Algorithm-Predictive Parser
• Input: String to be parsed and parsing table M for Grammar
• Output: Either stack will become empty or it will give an error
• Steps:
– While(Stack is not empty)do
– {
• Let X be top stack symbol and let a be next input symbol
• If (X is terminal or $) then
– If(X=a) then
» Pop X from stack and remove a from the input
– Else
» Error()
• Else
– If M[X,a]=AY1Y2…Yn then
» Pop X and Push YnYn-1…Y1 onto stack
– Else
» Error()

–} 42
Numerical
• Construct predictive parsing table for the following grammar and check whether
string id+id*id is accepted or not.

– ETE`
– E`+TE`|ɛ
– TFT`
– T`*FT`|ɛ
– F(E)|id

43
Numerical
– ETE`
– E`+TE`|ɛ
– TFT`
– T`*FT`|ɛ
– F(E)|id

• Step 1: Eliminate left recursion and left factoring: Already removed

44
Numerical
– ETE`
– E`+TE`|ɛ
– TFT`
– T`*FT`|ɛ
– F(E)|id

• Step 1: Eliminate left recursion and left factoring: Already removed


• Step 2: Compute First of all non terminals: First(E)=First(T) =First(F) {(,id} First(E`)={+, ɛ} First(T`)={*, ɛ}

45
Numerical
– ETE`
– E`+TE`|ɛ
– TFT`
– T`*FT`|ɛ
– F(E)|id

• Step 1: Eliminate left recursion and left factoring: Already removed


• Step 2: Compute First of all non terminals: First(E)=First(T) =First(F) {(,id} First(E`)={+, ɛ} First(T`)={*, ɛ}
• Step 3: Compute Follow of all non terminals: Follow(E)=Follow(E`) {),$} Follow(T)=Follow(T`) {+, ),$}
Follow(F)={+,*,),$}

46
Non INPUT
Terminal
SYMBOLS

Numerical
id + * ( ) $

– ETE` E

– E`+TE`|ɛ E’

– TFT` T
– T`*FT`|ɛ T’
– F(E)|id
F

• Step 1: Eliminate left recursion and left factoring: Already removed


• Step 2: Compute First of all non terminals: First(E)=First(T) =First(F) {(,id} First(E`)={+, ɛ} First(T`)={*, ɛ}
• Step 3: Compute Follow of all non terminals: Follow(E)=Follow(E`) {),$} Follow(T)=Follow(T`) {+, ),$}
Follow(F)={+,*,),$}
• Step 4: Construct the predictive parsing table

47
Non INPUT
Terminal
SYMBOLS

Numerical
id + * ( ) $

– ETE` E E TE’ E  TE’

– E`+TE`|ɛ E’ E +TE’ E’  ε E’  ε

– TFT` T T  FT’ T  FT’


– T`*FT`|ɛ T’ T’  ε T’  *FT’ T’  ε T’  ε
– F(E)|id
F F  id F  (E)

• Step 1: Eliminate left recursion and left factoring: Already removed


• Step 2: Compute First of all non terminals: First(E)=First(T) =First(F) {(,id} First(E`)={+, ɛ} First(T`)={*, ɛ}
• Step 3: Compute Follow of all non terminals: Follow(E)=Follow(E`) {),$} Follow(T)=Follow(T`) {+, ),$}
Follow(F)={+,*,),$}
• Step 4: Construct the predictive parsing table

48
Non INPUT
Terminal
SYMBOLS

Numerical
id + * ( ) $

– ETE` E E TE’ E  TE’

– E`+TE`|ɛ E’ E’ +TE’ E’  ε E’  ε

– TFT` T T  FT’ T  FT’


– T`*FT`|ɛ T’ T’  ε T’  *FT’ T’  ε T’  ε
– F(E)|id
F F  id F  (E)

• Step 1: Eliminate left recursion and left factoring: Already removed


• Step 2: Compute First of all non terminals: First(E)=First(T) =First(F) {(,id} First(E`)={+, ɛ} First(T`)={*, ɛ}
• Step 3: Compute Follow of all non terminals: Follow(E)=Follow(E`) {),$} Follow(T)=Follow(T`) {+, ),$}
Follow(F)={+,*,),$}
• Step 4: Construct the predictive parsing table.
• Step 5: Check the acceptance of the string id+id*id using predictive parsing program.

49
MATCHED STACK INPUT ACTION
E$ id+id * id$
TE’$ id+id * id$ E->TE’
FT’E’$ id+id * id$ T->FT’
– ETE`
id T’E’$ id+id * id$ F->id
– E`+TE`|ɛ
id T’E’$ +id * id$ Match id
– TFT`
id E’$ +id * id$ T’->Є
– T`*FT`|ɛ
id +TE’$ +id * id$ E’-> +TE’
– F(E)|id
id+ TE’$ id * id$ Match +
Non INPUT SYMBOLS
Termin id+ FT’E’$ id * id$ T-> FT’
al
id+ idT’E’$ id * id$ F-> id
id + * ( ) $ id+id T’E’$ * id$ Match id
E E TE’ E  TE’ id+id * FT’E’$ * id$ T’-> *FT’
E’ E +TE’ E’  ε E’  ε id+id * FT’E’$ id$ Match *
T T  FT’ T  FT’
id+id * idT’E’$ id$ F-> id
id+id * id T’E’$ $ Match id
T’ T’  ε T’  *FT’ T’  ε T’  ε
id+id * id E’$ $ T’-> Є
F F  id F  (E)
id+id * id $ $ E’-> Є 50
LL(1) Grammar
• The algorithm for constructing a predictive parsing table can be applied to any
grammar G to produce a parsing table M.
• For some grammars however, M may have some entries that are multiply-defined.
• If G is left recursive or ambiguous, then M will have at least one multiply-defined
entry.
• A grammar whose parsing table has no multiply-defined entries is said to be LL(1).

51
LL(1) Grammar
• LL(1) grammars have several distinctive properties.
– Parsing table has no multiply-defined entries is said to be LL(1).
– No ambiguous or left recursive grammar can be LL(1).
– A grammar G is LL(1) iff whenever Aα|β are two distinct productions of G, the following
conditions hold :-
• FIRST(α) , FIRST(β) must be disjoint.

• At most one of the strings α or β can derive ε i.e. FIRST(α) ⋂ FIRST(β) ≠ ε.


• If β ⇒∗ ε, then FIRST(α) ⋂ Follow(A)=φ i.e. α does not derive any string beginning with the terminal
in FOLLOW(A)

• In predictive parser when we restrict grammar to be only LL(1) grammar then it is


called LL(1) parser.

52
LL(1) Grammar
G1 = ({E, F, T}, {a, (,), ∗,+}, P, E) where the productions are:
E → E + T|T
T → T ∗ F|F
F → (E)|a

Is this LL(1) grammar

53
LL(1) Grammar
G1 = ({E, F, T}, {a, (,), ∗,+}, P, E) where the productions are:
E → E + T|T
T → T ∗ F|F
F → (E)|a

It is not LL(1) grammar

54
LL(1) Grammar
G2 = ({S, A}, {a, b,c,d}, P, S) where the productions are:
S → cAd
A → ab|a

Is this LL(1) grammar?

55
LL(1) Grammar
G2 = ({S, A}, {a, b,c,d}, P, S) where the productions are:
S → cAd
A → ab|a

It Is not LL(1) grammar?

56
LL(1) Grammar
G3 = ({E, E′, T, T′, F}, {a, ∗, +,(,)}, P, E) with the set of productions P:
E → TE′ T → F T′ F → (E)|a
E′ → +TE′| ε T′ → ∗F T′| ε

Is this LL(1) grammar?

57
LL(1) Grammar
G3 = ({E, E′, T, T′, F}, {a, ∗, +,(,)}, P, E) with the set of productions P:
E → TE′ T → F T′ F → (E)|a
E′ → +TE′| ε T′ → ∗F T′| ε

It is LL(1) grammar

58
LL(1) Grammar
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε

Is this LL(1) grammar

59
LL(1) Grammar
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε

It is LL(1) grammar, now do the parsing of the string abba

60
LL(1) Parser
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε

It is LL(1) grammar, now do the parsing of the string ‘abba‘

First (S)={a}
First (B)={b, ε}

61
LL(1) Parser
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε

It is LL(1) grammar, now do the parsing of the string abba

First (S)={a} Follow(S)={$}


First (B)={b, ε} Follow(B)={a}

62
LL(1) Parser
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε

It is LL(1) grammar, now do the parsing of the string abba

First (S)={a} Follow(S)={$}


First (B)={b, ε} Follow(B)={a}

63
LL(1) Parser
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε

It is LL(1) grammar, now do the parsing of the string abba

First (S)={a} Follow(S)={$}


First (B)={b, ε} Follow(B)={a}

64
LL(1) Parser
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε

It is LL(1) grammar, now do the parsing of the string abba

First (S)={a} Follow(S)={$}


First (B)={b, ε} Follow(B)={a}

65
LL(1) Parser
G4 = ({S, B}, {a,b}, P, S) with the set of productions P:
SaBa
BbB| ε

It is LL(1) grammar,
now do the parsing of the string abba

First (S)={a} Follow(S)={$}


First (B)={b, ε} Follow(B)={a}

66
LL(1) Grammar
G1 = ({S, S`,C}, {i, t, a, e, b}, P, S) where the productions are:
SiCtSS`|a
S`eS| ε
C→b Construct parsing table for this and check is this grammar LL(1)

67
LL(1) Grammar
G1 = ({S, S`,C}, {i, t, a, e, b}, P, S) where the productions are:
SiCtSS`|a
S`eS| ε
C→b Construct parsing table for this and check is this grammar LL(1)

Step 1: Remove Left recursion

68
LL(1) Grammar
G1 = ({S, S`,C}, {i, t, a, e, b}, P, S) where the productions are:
SiCtSS`|a
S`eS| ε
C→b Construct parsing table for this and check is this grammar LL(1)

Step 1: Remove Left recursion


Step 2: Remove Left Factoring

69
LL(1) Grammar
G1 = ({S, S`,C}, {i, t, a, e, b}, P, S) where the productions are:
SiCtSS`|a
S`eS| ε
C→b Construct parsing table for this and check is this grammar LL(1)

Step 1: Remove Left recursion


Step 2: Remove Left Factoring
Step 3: Calculate First and Follow.

70
LL(1) Grammar
G1 = ({S, S`,C}, {i, t, a, e, b}, P, S) where the productions are:
SiCtSS`|a
S`eS| ε
C→b Construct parsing table for this and check is this grammar LL(1)

Step 1: Remove Left recursion


Step 2: Remove Left Factoring
Step 3: Calculate First and Follow.
First(S)={i,a}
First(S`)={e, ε}
First(C)={b}

71
LL(1) Grammar
G1 = ({S, S`,C}, {i, t, a, e, b}, P, S) where the productions are:
SiCtSS`|a
S`eS| ε
C→b Construct parsing table for this and check is this grammar LL(1)

Step 1: Remove Left recursion


Step 2: Remove Left Factoring
Step 3: Calculate First and Follow.
First(S)={i, a} Follow(S)={$,e}
First(S`)={e, ε} Follow(S`)={$,e}
First(C)={b} Follow(C)={t}

72
LL(1) Grammar
G1 = ({S, S`,C}, {i, t, a, e, b}, P, S) where the productions are:
SiCtSS`|a
S`eS| ε
C→b Construct parsing table for this and check is this grammar LL(1)

Step 1: Remove Left recursion


Step 2: Remove Left Factoring
Step 3: Calculate First and Follow.
First(S)={i, a} Follow(S)={$,e}
First(S`)={e, ε} Follow(S`)={$,e}
First(C)={b} Follow(C)={t}
Step 4: Construct parsing table.
73
LL(1) Grammar
G1 = ({S, S`,C}, {i, t, a, e, b}, P, S) where the productions are:
SiCtSS`|a
S`eS| ε
C→b Construct parsing table for this and check is this grammar LL(1)

Step 1: Remove Left recursion


Step 2: Remove Left Factoring
Step 3: Calculate First and Follow.
First(S)={i, a} Follow(S)={$,e}
First(S`)={e, ε} Follow(S`)={$,e}
First(C)={b} Follow(C)={t}
Step 4: Construct parsing table.
74
LL(1) Grammar
G1 = ({S, L}, {(,),a, ,},P,S) where the productions are:
S(L)|a
LL,S| S
Construct parsing table for this, check is this grammar LL(1), also check string ‘(a,a)’ is
accepted:

75
Dr Anurag Jain
Assistant Professor (SG), Virtualization Department, School of Computer Science,
University of Petroleum & Energy Studies, Dehradun - 248 007 (Uttarakhand)
Email: [email protected] , [email protected]
Mobile: (+91) -9729371188

You might also like