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

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

A
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  
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
One of the uses of CNF is to turn parse tree into :
A
AVL trees
B
Binary search trees
C
Binary trees
D
None of the above
Question 5 Explanation: 
One of the uses of CNF is to turn parse trees into binary trees. – These trees have some convenient properties, one of which we exploit here.
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
CFG can be recognized by a
A
push-down automata
B
2-way linear bounded automata
C
both (A) and (B)
D
none of these
Question 6 Explanation: 
CFG can be recognized by push-down automata. Moreover every CFG is a CSG and every CSG can be recognized by linear bounded automata. Hence every CFG can be recognized by linear bounded automata.
Question 7
A context free grammar for L = { w | n​ 0​ ( w ) > n​ 1​ ( w ) } is given by:
A
S → 0 | 0 S | 1 S S
B
S → 0 S | 1 S | 0 S S | 1 S S | 0 | 1
C
S → 0 | 0 S | 1 S S | S 1 S | S S 1
D
S → 0 S |1 S | 0 | 1
Question 7 Explanation: 
In given language L the number of 0’s should be greater than the number of 1’s.
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.
Question 8
Given the following grammars:
G​ 1​ : ​ S → AB|aaB
A → aA | ∈
B → bB | ∈
G2: S → A|B
A → aAb | ab
B → abB | ∈
Which of the following is correct?
A
G ​ 1​ is ambiguous and G ​ 2​ is unambiguous grammars
B
G ​ 1​ is unambiguous and G ​ 2​ is ambiguous grammars
C
both G ​ 1​ and G ​ 2​ are ambiguous grammars
D
both G ​ 1​ and G ​ 2​ are unambiguous grammars
Question 8 Explanation: 
G1 is ambiguous because for generating string string “aa” we are having two different leftmost trees.

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