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  
A
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.)

A
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?

A
Theory Explanation.
Question 4
Consider the context-free grammar G below
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?
A
The language generated by G is (a + b)*
B
The language generated by G is a*(a + b)b*
C
The language generated by G is a*b*(a + b)
D
The language generated by G is not a regular language
Question 5
Which of the following CFG’s can’t be simulated by a Finte State Machine?
A
S -- < Sa | b
B
S -- > aSb | ab
C
S -- < abX, X --> cY, Y -- > d | aX
D
None of the given options
Question 5 Explanation: 
The language generated by the grammar in option 2 is {a^n b^n | n>=1}, which is not regular language ,hence cannot be simulated by a finite state machine. Also we can see that grammar in option 2 is neither left linear grammar nor right linear grammar due to production S -->aSb , hence can’t be simulated by a finite state machine.
Question 6
Let G = (V, T, S, P) be a context-free grammar such that every one of its productions is of the form A → ν, with |ν| = k > 1. The derivation tree for any string W ∈ L (G) has a height such that
A
h<((|W| - 1)/k-1)
B
logk|W| < h
C
logk|W| < h < ((|W| - 1)/k-1)
D
logk|W| <= h <= ((|W| - 1)/k-1)
Question 6 Explanation: 
Min height of any tree is logk|W|
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
{a^nb^n : n ≥ 0}
B
{w: na(w) = nb(w) and na(v) >= nb(v) where v is any prefIx of w}
C
{w: na(w) ≠ nb(w)}
D
{w: na(w) = nb(w)}
Question 7 Explanation: 
The given grammar generates the strings with “no. of a’s = no. of b’s”
Question 8

The class of context-free grammar is not closed under ____ operations

A
Concatenation
B
Intersection
C
Star
D
Union
Question 8 Explanation: 
Context free grammar is closed under concatenation, kleene star, union but not closed under intersection.
There are 8 questions to complete.

Access quiz wise question and answers by becoming as a solutions adda PRO SUBSCRIBER with Ad-Free content

Register Now

If you have registered and made your payment please contact solutionsadda.in@gmail.com to get access