Parsing Problems

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Try the following exercises.

Compute the First and Follow sets as well as construct the


parsing table for the following LL(1) grammars. Solutions are on the next page.

Exercise 1

S→Aa
A→BD
B→b|
D→d|

Exercise 2

C → P F class id X Y
P → public | 
F → final | 
X → extends id | 
Y → implements I | 
I → id J
J→,I|

Exercise 3

prog → stmt
stmt → if expr then block | while expr do block | expr ;
expr → term => id | isZero? term | not expr | ++ id | −− id
term → id | const
block → stmt | { stmts }
stmts → stmt stmts | 

1
Exercise 1

1. S→Aa
2. A→BD
3. B→b
4. B→
5. D→d
6. D→

First(S) = {b, d, a}
First(A) = {b, d, }
First(B) = {b, }
First(D) = {d, }

Follow(S) = {$}
Follow(A) = {a}
Follow(B) = {d, a}
Follow(D) = {a}

a b d $
S 1 1 1
A 2 2 2
B 4 3 4
D 6 5
Exercise 2

1. C → P F class id X Y
2. P → public
3. P → 
4. F → final
5. F → 
6. X → extends id
7. X → 
8. Y → implements I
9. Y → 
10. I → id J
11. J → , I
12. J → 

First(C) = {public, final, class}


First(P) = {public, }
First(F) = {final, }

2
First(X) = {extends, }
First(Y) = {implements, }
First(I) = {id}
First(J) = {‘,’, }

Follow(C) = {$}
Follow(P) = {final, class}
Follow(F) = {class}
Follow(X) = {implements, $}
Follow(Y) = {$}
Follow(I) = {$}
Follow(J) = {$}

public final class id extends implements , $


C 1 1 1
P 2 3 3
F 4 5
X 6 7 7
Y 8 9
I 10
J 11 12
Exercise 3

1. prog → stmt
2. stmt → if expr then block
3. stmt → while expr do block
4. stmt → expr ;
5. expr → term => id
6. expr → isZero? term
7. expr → not expr
8. expr → ++ id
9. expr → −− id
10. term → id
11. term → const
12. block → stmt
13. block → { stmts }
14. stmts → stmt stmts
15. stmts → 

First(prog) = {if, while, id, const, isZero?, not, ++, −−}


First(stmt) = {if, while, id, const, isZero?, not, ++, −−}
First(expr) = {id, const, isZero?, not, ++, −−}
First(term) = {id, const}

3
First(block) = {’{’, if, while, id, const, isZero?, not, ++, −−}
First(stmts) = {, if, while, id, const, isZero?, not, ++, −−}

Follow(prog) = {$}
Follow(stmt) = {$, if, while, id, const, isZero?, not, ++, −−}
Follow(expr) = {then, do, ;}
Follow(term) = {=>, then, do, ;}
Follow(block) = {$, if, while, id, const, isZero?, not, ++, −−}
Follow(stmts) = {‘}’}

if
while id const isZero? not ++ −− { then do ; => } $
prog 11 1 1 1 1 1 1
stmt 23 4 4 4 4 4 4
expr 5 5 6 7 8 9
term 10 11
block 12 12 12 12 12 12 12 12 13
stmts 14 14 14 14 14 14 14 14 15

You might also like