Context-Free-Grammar
Question 1 |
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 2 |
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 3 |
(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 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 |
S -- < Sa | b | |
S -- > aSb | ab | |
S -- < abX, X --> cY, Y -- > d | aX | |
None of the given options |
Question 6 |
h<((|W| - 1)/k-1) | |
logk|W| < h | |
logk|W| < h < ((|W| - 1)/k-1) | |
logk|W| <= h <= ((|W| - 1)/k-1) |
For max height (since k>1, consider k=2) , hence it is CNF form and in CNF height is log2|W|+1)
So, in general it should be logk((|w|-1) / (k-1))
Question 7 |
The language of the following CFG on Σ= {a, b} given by
S → aSb I SS I ε
with na (w) denoting the number of a's present in w is
{a^nb^n : n ≥ 0} | |
{w: na(w) = nb(w) and na(v) >= nb(v) where v is any prefIx of w} | |
{w: na(w) ≠ nb(w)} | |
{w: na(w) = nb(w)} |
Question 8 |
The class of context-free grammar is not closed under ____ operations
Concatenation
| |
Intersection
| |
Star | |
Union
|