Assignment # 2
Assignment # 2
Assignment # 2
(CS) -7
Date of submission: 25-10-2021 (10 points)
Q.1
Given the grammar :
lexp → number | ( op lexp-seq)
op → + | - | *
lexp-seq → lexp-seq lexp | lexp
Write pseudo code to compute the numeric value of an lexp by recursive descent.
Q.2
Show the actions of an LL(1) parser that uses the following table to recognize the
following Arithmetic expression..
3 * (4 – 5 + 6)
M(N,T) ( Number ) + - * $
Exp exp→term exp’ exp→term exp’
exp’ exp’ exp’→ exp’→ exp’→
→𝜀 𝑎𝑑𝑑op 𝑎𝑑𝑑op 𝜀
term term
exp’ exp’
addop addop addop
→+ →-
Term term→ term→
factor term’ factor term’
term’ term’ term’→ term’→ term’→mulop term’→
→𝜀 𝜀 𝜀 factor term’ 𝜀
mulop mulop → *
factor factor→(exp) factor→number
Q.3
Consider the following grammar.
lexp→ atom | list
atom→ number | identifier
list→ ( lexp-seq)
lexp-seq → lexp-seq lexp | lexp
a. Remove the Left-recursion.
b. Construct first and follow sets for the non-terminal of the resulting grammar.
c. Is the resulting grammar LL(1)?Justify your answer from the two conditions on
its First and Follow sets.
d. Construct the LL(1) parsing table for the above grammar.
e. Show the actions of an LL(1) parser that uses the above grammar to recognize
the following input string.
( a ( B ( 2 ) ) ( c ) ) $
Q.4.
S→ S+T | T
T→ T*F | F
F → (S) | id
Remove the left recursion from the above grammar.
-----------------------------------------------------------------------------------------
Q.12 Consider the following grammar:
P→bSe
S → AR
R → AR | ∈
A → id = E ;
E → FT
T → + FT | ∈
F → (E) | id
a) Construct the First and Follow sets for the above grammar.
b) Construct the Parsing table for the above grammar:
c) Is the above grammar LL(1)? Justify your answer from the two conditions on its First and
Follow sets.
d) Parse the input b id = id + 1 ; e $ using the parsing table constructed in part (b).
Q.13
i. Given the grammar A → (A) A | ∈
Is the above grammar LL(1)? Prove with logic.
ii. Given the grammar A → a A a | ∈
Is the above grammar LL(1)? Prove with logic.
Construct First and Follow sets for the non-terminal A of the above two parts.
Q.14
a. Consider the following Grammar.
S → a | ab | abc | abcd
Is it suitable for Top-down parser? If not Why?
Modify the above grammar (i.e.without changing the meaning of the grammar) in such a way
that it should be suitable for L.L.(1) parser.
b. Consider the following Context-free grammar.
S→ S S + | S S* |a
Q. 15
Consider the following grammar.:
lexp → atom | list
atom→ number | identifier
list→ (lexp-seq)
lexp-seq → lexp , lexp-seq | lexp
a) Left factor this grammar.
b) Construct first and follow sets for the non-terminal of the resulting grammar.
c) Is the resulting grammar LL(1)?. Justify your answer from the two conditions
on its First and Follow sets.
d) Construct the LL(1) parsing table for the above grammar.
e) Show the actions of an LL(1) parser that uses the above grammar to recognize
the following input string.
( A , ( b , ( 2 ) ) , ( c ) ) $