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
The language of all non-null strings of a’s can be defined by a context free grammar as follows: S → aS|Sa|a The word a3 can be generated by __________ different trees.
A
two
B
three
C
four
D
five
Question 5 Explanation: 
Question 6
The context free grammar given by
S → XYX
X → aX|bX|λ
Y → bbb
generates the language which is defined by regular expression:
A
(a + b)*bbb
B
abbb(a + b)*
C
(a + b)*(bbb)(a + b)*
D
(a + b)(bbb)(a + b)*
Question 6 Explanation: 
Option(a) is not correct because S → XYX, Y → bbb it means the regular expression for given grammar will contain "bbb" in between.
Option(B) is incorrect because "bbbb(a+b)* can also be generated by given grammar.
Option (C) is correct because S → XYX X → aX|bX|λ Y → bbb , here "bbb" will be in middle and X → aX|bX|λ can generate (a+b)* and since S → XYX is having "X" before and after "Y" so it is correct.
Option (D) is not correct because X → aX|bX|λ can generate (a+b)^* and since S → XYX is having "X" before and after "Y" so we can have (a+b)* before and after "bbb"
Question 7
Consider the following language: L={W ε{a,b,c}*:na(ω)+nb(ω)=nc(ω)} then L is
A
Context free but not linear
B
Not context free
C
Context free and linear
D
Linear
Question 7 Explanation: 
The language L = {w in {a, b, c}* : na(ω) + nb(ω) = nc(ω} is indeed context-free. It is not linear, but it is context-free. Here's an example of a context-free grammar that generates this language: S -> aSc (This rule adds one 'a' and one 'c' to the string, maintaining the balance.) S -> bSd (This rule adds one 'b' and one 'd' to the string, maintaining the balance.) S -> ε (This rule allows the string to be empty.) Using this context-free grammar, you can generate strings that satisfy the condition na + nb = nc. For example: For na = 2 and nb = 2, you can generate aacbcd. For na = 3 and nb = 3, you can generate aabbcc. So, the language is context-free but not linear.
Question 8
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 8 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.
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