Context-Free-Grammar
Question 1 |
Let G be a context-free grammar where G = ({S, A, B, C},{a,b,d},P,S) with the productions in P given below.
S → ABAC A → aA ∣ ε B → bB ∣ ε C → d
(ε denotes null string). Transform the grammar G to an equivalent context-free grammar G' that has no ε productions and no unit productions. (A unit production is of the form x → y, and x and y are non terminals.)
Theory Explanation. |
Question 2 |
(a) Let G1 = (N, T, P, S1) be a CFG where,
N = {S1, A, B}, T = {a,b} and
P is given by
S1 → aS1b S1 → aBb S1 → aAb B → Bb A → aA B → b A → a
What is L(G1)?
(b) Use the grammar in part(a) to give a CFG
for L2 = {ai bj ak bl | i, j, k, l ≥ 1, i=j or k=l} by adding not more than 5 production rule.
(c) Is L2 inherently ambiguous?
Theory Explanation. |
Question 3 |
A simple Pascal like language has only three statements.
(i) assignment statement e.g. x:=expression
(ii) loop construct e.g. for i:=expression to expression do statement.
(iii) sequencing e.g. begin statement ;…; statement end
(a) Write a context-free grammar (CFG) for statements in the above language. Assume that expression has already been defined. Do not use optional
parentheses and * operator in CFG.
(b) Show the parse tree for the following statement:
for j:=2 to 10 do begin x:=expr1; y:=expr2 end
Theory Explanation. |
Question 4 |
S → aSb | X
X → aX | Xb | a | b ,
where S and X are non-terminals, and a and b are terminal symbols. The starting non-terminal is S. Which one of the following statements is CORRECT?
The language generated by G is (a + b)* | |
The language generated by G is a*(a + b)b* | |
The language generated by G is a*b*(a + b) | |
The language generated by G is not a regular language |
Question 5 |
AVL trees | |
Binary search trees | |
Binary trees | |
None of the above |
Theorem 7.17: Suppose we have a parse tree according to a Chomsky- Normal-Form grammar G = (V, T, P, S), and suppose that the yield of the tree is a terminal string w.
Question 6 |
push-down automata | |
2-way linear bounded automata
| |
both (A) and (B) | |
none of these |
Question 7 |
G 1 : S → AB|aaB
A → aA | ∈
B → bB | ∈
G2: S → A|B
A → aAb | ab
B → abB | ∈
Which of the following is correct?
G 1 is ambiguous and G 2 is unambiguous grammars | |
G 1 is unambiguous and G 2 is ambiguous grammars | |
both G 1 and G 2 are ambiguous grammars | |
both G 1 and G 2 are unambiguous grammars |
G2 is ambiguous because for generating string ab we are having two different leftmost trees.
NOTE: A grammar is ambiguous only if it can generate two different trees(either leftmost derivation tree or rightmost derivation tree) for a same string.
Question 8 |
S → 0 | 0 S | 1 S S | |
S → 0 S | 1 S | 0 S S | 1 S S | 0 | 1 | |
S → 0 | 0 S | 1 S S | S 1 S | S S 1 | |
S → 0 S |1 S | 0 | 1 |
L= { 0, 00, 000, 100, 001, 010,...........}
Option(A) is incorrect because can’t generate string “001”
Option(B) is incorrect because it is generating string”1” in which number of 0’s is less than number of 1’s.
Option(C) is correct because it can generate all the strings generated by the given language L.
Option(D) is incorrect because it is generating string”1” in which number of 0’s is less than number of 1’s.