Nothing Special   »   [go: up one dir, main page]

Assignment # 2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

ASSIGNMENT # 2 (CLO-2) BCS-7 A & B.

Date of submission: 28-03-2022 (10 points)

Question:What are the two conditions for a Grammar to be L.L.(1).


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→term exp’
exp’
exp’ exp’ exp’ exp’ exp’→
→ε → add o → add ε
p term op
exp’ term
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.

a) Can an LL(1) grammar be ambiguous? Why or why not?


b) Can an ambiguous grammar be LL(1)? Why or why not?
c) Must an unambiguous grammar be LL(1)? Why or why not?
d) Can a left recursive grammar be LL(1). Why or why not?
Q. 5
A Non-terminal is use-less if there is no derivation from the start symbol to a
string of tokens in which A appears.
a. Give a mathematical formulation of this property.
b. Is it likely that a programming language grammar will have a useless symbol?
Explain
c. Show that, if a grammar has a useless symbol, the computation of First and
Follow sets as given in this chapter may produce sets that are too large to
accurately construct an LL(1) parsing table.
Q.6
ERROR RECOVERY TABLE
Trace the actions of an LL(1) parser on the input (2+ - 3)* 4 - + 5 using the
error recovery as given in the following table.
M(N,T) ( Number ) + - * $
Exp exp→term exp→term exp’ Pop Scan Scan Scan Pop
exp’
exp’ Scan Scan exp’ exp’ exp’ Scan exp’→
→ε → add o → add ε
p term op
exp’ term
exp’
Addop Pop Pop Scan addop addop Scan Pop
→+ →-
term term→ term→ Pop Pop Pop Scan Pop
factor term’ factor term’
term’ Scan Scan term’ term’→ term’ term’→mulop term’
→ε ε →ε factor term’ →ε
mulop Pop Pop Scan Scan scan mulop→ * Pop
factor factor→(exp) factor→number Pop Pop pop Pop Pop
Q.7
Compute FIRST and FOLLOW sets for the following Grammar.
Consider the expression grammar below:
E T E'
E' + T E' | ∈
T F T'
T' * F T' | ∈
F ( E ) | id
-------------------------------------------------------------------------------------
Q.8
Consider the following Grammar:
declaration → type var-list
type → int | float
var-list → identifier, var-list | identifier

a. Left-factor this grammar.


b. Construct First and Follow sets for the non-terminals of the resulting grammar.
c. Show that the resulting grammar is LL(1).
d. Construct the LL(1) parsing table for the resulting grammar.
e. Show the actions of the corresponding LL(1) parser, given the input string int x,y,z.
Q.9
For the following EBNF, Convert it into CFG that has no left-recursion and is also
left-factored(without changing the meaning of the grammar).Then find the First and
Follow sets for the modified grammar along with the complete paring table.
G={N,T,S,P}
N={A,B,C,D}
T = { "(" , ")" , "+" , "a" , "[" , "]" , "." }
Start symbol = A
P = productions:
A→B "."
B→ [ "a" | "(" C ")" | "[" B "]" ]
C→B D
D→{ "+" B }
Convert the above EBNF into CFG more suitable for Top-down parsing.
(i.e. A grammar that is left-factored and also without left-recursion.)
Q.10
Consider the following grammar:
A →x C B
B→ z|Ax
C→ yBz|∈
Where {A, B, C} is the set of nonterminal symbols, A is the start symbol, {x, y, z} is the set of
terminal symbols, and ∈ denotes the empty string.
i) Is the above grammar LL(1).
ii) Construct the first and follow sets for the above grammar.
iii) Make the predictive parsing two-dimensional(M,N) table.
Answer the following questions:
a) What does table(A, x) contain? a) ∈ b) x c) xCB d) A x.
b) What does table(A, y) contain? a) x b) error c) xCB d) A x.
c) What does table(A, z) contain? a) yBZ b) error c) xCB d) A x.
d) What does table (B, x) contain? a) error b) z c) x d) A x.
e) What does table (B, y) contain? a) ∈ b) error c) z d) A x.
f) What does table (B, z) contain? a) error b) c x c) z d) A x.
g) What does table (C, x) contain? a) ∈ b) error c) yBz d) xCB.
h) What does table(C, y) contain? a) error b) x c) yBz d) xCB.
i) What does table(C, z) contain? a) error b) ∈ c) y B z d) xCB.
----------------------------------------------------------------------------------------
Q.11
a. Consider the following grammar:
S → iEtS | iEtSeS | a
E →b
Is this left-factored? If no, then make it left-factored else write yes.

b. Remove the left recursion from the following grammars.


i) S → S + T | T
T→ T*F | F
F → (S) | id
ii) S → Aa | b
A → Ac | Sd | ∈
-----------------------------------------------------------------------------------------
Q.12 Consider the following grammar:
P→bSe
S → AR
R → AR | ∈
A→ id = E ;
E → FT
T → + FT | ∈
F → (E) | id | int
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 following 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

And the input string a a + a*


Draw the table showing the stages of the parsing stack, position of input tape header and
action taken at each step.
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 ) ) $

--------------------------------------------- THE END --------------------------------------------------------------

You might also like