0% found this document useful (0 votes)
11 views7 pages

Tutorial 2

Uploaded by

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

Tutorial 2

Uploaded by

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

Date:

Ramaiah Institute of Technology


(Autonomous Institute, Affiliated to VTU)
Department of Computer Science and Engineering(AI&ML)

Subject: Automata Theory and Compiler Design Subject code: CI53/CY53

Tutorial 2 and 3

I. a) Is this grammar suitable to be parsed using the predictive parsing


method? Justify and modify the grammar if needed.

b) Compute the FIRST and FOLLOW set of non-terminal symbols of the


grammars given

c) Construct Predictive Parsing Table

i. S → aAbA | aAbc | ScA A → aAbab

Solution: Not Suitable for Predictive parsing, Left Recursion is present

Eliminate Left Recursion using general formula

AAα| β is changed to

A βA’

A’ αA’ | ε

New G after ELR:

S→ aAbAX | aAbcX

X->cAX | ε

A → aAbab

where X is the new Non-terminal

New G after Left Factoring:

S->aAbY

Y->AX|cX

X->cAX | ε

A → aAbab

NT First Follow
S {a} {$}
A {a} {b,c,$}
Y {a,c} {$}

Prepared by SINI ANNA ALEX, CSE(AI&ML), MSRIT


X {c, ε} {$}
Predictive parsing table for the grammar
NT/input symbol
a b c $
S S → aAbY
Y Y->AX Y->cX
X X->cAX X-> ε
A A → aAbab

ii. S → 0 S 1 | 0 1

Solution: No Left Recursion, Left factoring is required.

S→0X
X->S1|1

NT First Follow
S {0} {1,$}
X {0,1} {1,$}
Predictive parsing table for the grammar
NT/input symbol
0 1 $
S S → 0X
X X->S1 X->1

iii. S → + S S | * S S | a

Solution: Yes

NT First Follow
S {+,*,a} {+,+,a,$}

NT/input symbol
+ * a $
S S → +SS S → *SS S→a

iv. S → S ( S ) S | ε

Solution: No
S->X

Prepared by SINI ANNA ALEX, CSE(AI&ML), MSRIT


X → ( S )S X | ε

NT First Follow
S {(, ε } {$,),(}
X {(, ε } {$,),(}

Predictive parsing table for the grammar:

NT/input symbol
( ) $
S S→X S→X S→X
X X → (S)S X X→ε X→ε
X→ε

Not LL(1)
v. S → S + S | S S | ( S ) | S* |a

Solution: Not Suitable , Eliminate Left Recursion (ELR)

New G: S->(S)X | aX
X->+SX|SX|*X| ε

NT First Follow
S {(, a } {$,+,*,a,(,)}
X {+,*,a,(, ε } {$,+,*,a,(,)}
Predictive parsing table for the grammar
NT/input
symbol ( ) + * a $
S S->(S)X S->aX
X X->SX X->ε X->+SX X->*X X->SX X->ε
X->ε X->ε X->ε X->ε

vi. S → a S b S | b S a S |ε

Solution: Yes

NT First Follow
S { a, b, ε } {$,a,b}
Predictive parsing table for the grammar
NT/input symbol
a b $
S S → aSbS S → bSaS S→ε
S→ε S→ε

Prepared by SINI ANNA ALEX, CSE(AI&ML), MSRIT


Not LL(1)

vii. S → A A → BC | DBC B → Bb | ε

C→c|ε D→a|d

Solution: No, ELR

New G:

S→A A → BC | DBC

B->X

X->bX | ε
C→c|ε D →a|d

NT First Follow
S { b,c,a,d,ε } {$}
A { b,c,a,d,ε } {$}
B { b,ε } {$,c}
C { c,ε } {$}
D { a,d } {b,c,$}
X { b,ε } {$,c}

Predictive parsing table for the grammar


NT/input
symbol a b c d $
S S→A S→A S→A S→A S→A
A A → DBC A → BC A → BC A → DBC A → BC
B B->X B->X B->X
X X->bX X->ε X->ε
C C→c C →ε
D D→a D→d

viii. S→()|a|(A) A→S|A,S

Solution: No, ELR

New G: S → ( ) | a | ( A )

A → S // Indirect Left Recursion

replaced by A( ) | a | ( A )

Prepared by SINI ANNA ALEX, CSE(AI&ML), MSRIT


A( ) | a | ( A ) | A,S // immediate Left recursion

Eliminate Left recursion

A ( )X | aX | ( A )X

X->,SX | ε

New G after Elimination of Left recursion:-

S→()|a|(A)

A ( )X | aX | ( A )X

X->,SX | ε

Left Factor the grammar:

New G:
S → (Y | a

Y->)| A)

A → ( X’ | aX

X’  )X | A)X

X->,SX | ε

NT First Follow
S { (,a } {$,),,}
A {(,a } {)}
X {,ε } {)}
Y { ),(,a} { $,),, }
X’ { ),(, a} {)}

Predictive parsing table for the grammar


NT/input
symbol a ( ) , $
S S→a S → (Y
A A → SX A → SX
X X-> ε X->,SX
Y Y →A) Y →A) Y →)
X’ X’  A)X X’  A)X X’  )X

viii. S → A B e A → d B | aS | c B→AS|b

Prepared by SINI ANNA ALEX, CSE(AI&ML), MSRIT


Solution: Yes

NT First Follow
S { d,a,c} {$,b,d,a,c,e}
A {d,a,c} {b,d,a,c}
B {d,a,c,b} {b,d,a,c,e}

Predictive parsing table for the grammar


NT/input
symbol a b c d e $
S S → ABe S → ABe S →ABe
A A → aS A →c A→dB
B B → AS B→b B → AS B → AS

ix. S → CC C → cC | d

Solution: Yes

NT First Follow
S { d,c} {$}
C {d,c} {d,c,$}

Predictive parsing table for the grammar


NT/input symbol
c d $
S S → CC S → CC
C C → cC C→d

x. S → AaAb | BbBa | s A→ε B→ ε

Solution: Yes

NT First Follow
S {a,b,s} {$}
A {ε} {a,b}
B { ε} {a,b}

Predictive parsing table for the grammar


NT/input symbol
a b s $
S S → AaAb S → BbBa S →s
A A→ε A→ε
B B→ε B→ε

Prepared by SINI ANNA ALEX, CSE(AI&ML), MSRIT


II. Show using intermediate steps how the following input strings are
parsed using the recursive descent parsing method with the help of the
grammar given.

i. S → CC and C → cC | d with input string cdd$

C C

c C

c C

Backtracking

C C

c C c C

d Backtracking

C C

c C d Final RDP Tree


d

Signature of the Faculty with date:

Prepared by SINI ANNA ALEX, CSE(AI&ML), MSRIT

You might also like