0% found this document useful (0 votes)
8 views3 pages

CFG Practice Questions and PDA Design

Uploaded by

Vedniyas Vyas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

CFG Practice Questions and PDA Design

Uploaded by

Vedniyas Vyas
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module-2 Practice Questions

CFG: Derivations, Derivation tree, ambiguity in a grammar

1. (a) Derive left and right most derivations for the input string a=b⋆c+d/e for the given grammar
EE+E|E-E|E⋆E|E/E|(E)|id
(b) Find the left most and right most derivations for the string +⋆-xyx in the grammar:
E+EE | ⋆EE | -EE | x | y | ε
(c) Consider G whose productions are S  aAS I a, A  SbA | SS | ba. Show that
S => aabbaa and construct a derivation tree whose yield is aabbaa.
(d) Let G be the grammar S  0B | 1A, A  0 | 0S | 1AA, B  1 | 1S | 0BB.
For the string 00110101, find (i) the leftmost derivation, (ii) the rightmost derivation, and
(iii) the derivation tree.
(e) If G is the grammar S  SbS | a, show that G is ambiguous.
(f) If G is the grammar S  S+S | S*S | a | b, show that G is ambiguous.
(g) Show that the grammar S  a | abSb | aAb, A  bS | aAAb is ambiguous.
(h) Show that the grammar S  aB | ab, A  aAB | a. B  ABb | b is ambiguous.

CFG: Simplification of CFGs

2. (a) Find a reduced grammar equivalent to the grammar S  aAa, A bBB, B  ab, C  aB.
(b) Given the grammar S  AB, A  a, B  C | b, C  D, D  E,E  a, find an equivalent
grammar which is reduced and has no unit productions.
(c) Construct a reduced grammar equivalent to the grammar
S  aAa, A  Sb | bCC | DaA. C  abb | DD, E  aC, D  aDA
(d) Consider the grammar G whose productions are S  as | AB, A  Λ, B  Λ, D  b.
Construct a grammar G1 without null productions generating L(G) - { Λ }.

Normal forms: CFG to CNF


3. (a) What is CNF? Convert the following grammar into CNF:
S→ABa, A→aab, B→Ac
(b) Consider the grammar ({S,A,B}, {a,b}, P, S) that has the productions:
S→bA | aB, A→bAA | aS | a, B→aBB | bS | b.
Find an equivalent grammar in CNF.
(c) Convert the following Context Free Grammar to Chomsky Normal Form
S→AaB | aaB, A→ ε, B→bbA | ε
(d) Convert the following grammar to Chomsky Normal Form
S→bA | aB, A→bAA | aS | a, B→aBB | bS | b
(e) Convert the following Context Free Grammar to Chomsky Normal Form
S→1A | 0B, A→1AA | 0S | 0, B→0BB | 1
(f) Convert the following Context Free Grammar G = ({E, T, F}, {+,⋆, (, ), id}, P, E) to
Chomsky Normal Form, where P is given as
E→E + T | T, T→T ⋆ F |F, F→(E) | id
(g) Convert the following Context Free Grammar to Chomsky Normal Form
S→ABA, A→aA | ε, B→bB | ε

Normal forms: CFG to GNF


4. (a) Convert the following grammar to Greibach Normal Form
G = ({A1, A2, A3}, {a ,b},P,A) Where P consists of the following
A1→A2A3, A2→A3A1 | b, A3→A1A2 | a
(b) Convert the following grammar to Greibach Normal Form
S→ABA | AB | BA | AA | B, A→aA | a, B→bB | b
(c) Convert the following grammar to Greibach Normal Form
S→aAS | a, A→SbA | SS |ba
(d) Find the GNF equivalent to the following:
S→AA | a, A→SS | b

(e) Build the grammar G’ in GNF equivalent to the following grammar:


G = ({S, X, Y}, {a, b}, { S  Xa, X  aY, Y  Xa|b}, S)

PDA: Construction of a PDA for a given Context-free Language (CFL)


5. (a) Define Push Down Automata. Construct Push Down Automata accepting the following
language L ={anbn|n ≥ o}
+
(b) Construct the PDA that recognizes the languages L={x=xR: x є {a,b} }.
(c) Design Push Down Automata for L ={anbn|n ≥ 1}.
(d) Design Push Down Automata for L = {a2nbn | n ≥ 1}.
(e) Design Push Down Automata for L = {anb2n | n >0}.
(f) Design PDA for L = {anbncm |n, m ≥ 1}.
(g) Design PDA for L = {ambncn |n, m ≥ 1}.
(h) Design PDA for L = {anbmcn |n, m ≥ 1}.
(i) Construct the PDA that recognizes the languages L={wwR|wє (0 +1)*, wR is reverse of
w}.

Note: Acceptance might be asked by either final state or by null store (empty stack).

PDA: Conversion of CFG to PDA


6. (a) Construct the PDA to the following grammar:
S→aAA, A→aS | bS | a
(b) Convert the following Context Free Grammar to Push Down Automata
S→aSbb | aab
(c) Find Push Down Automata that accepts the Context-free Grammar:
S→XY, X→aX | bX|a, Y→Ya | Yb|a
(d) Construct PDA for the given CFG: S→aSb, S→ab, Where S is the only variable and{a,b} are
terminals.
(e) Convert the following grammar in a PDA that accepts the language by empty stack
S→0S1|A, A→1A0|S|
PDA: Conversion of PDA to CFG
7. (a) Convert the following Push Down Automata to Context Free Grammar
M= ({q0,q1},{a,b}{z0,za},δ,q0,z0,φ) where δ is given by
δ(q0,a,z0) =(q0, zaz0)
δ(q0,a, za) =(q0,zaza)
δ(q0,b, za) =(q1,Є)
δ(q1,b, za) =(q1,Є)
δ(q1, Є, z0) =(q1,Є)

(b) Construct CFG for the PDA given below A=({q0,q1},{0,1},{S,A},δ, q0,S,φ) where δ is
given as below
δ(q0,1,S)={(q0,AS)}
δ(q0,ε,S)={(q0,ε)}
δ(q0,1,A)={(q0,AA)}
δ(q0,0,A)={(q1,A)}
δ(q0,1,A)={(q1,ε)}
δ(q1,0,S)={(q0,S)}

(c) Construct CFG for the PDA M=({q0,q1},{0,1},{R,Z0},δ, q0,Z0,φ) where δ is given by
δ(q0,1,Z0)={(q0,RZ0)}
δ(q0,1,R)={(q0,RR)}
δ(q0,0,R)={(q1,R)}
δ(q1,0,Z0)={(q0,Z0)}
δ(q0,ε,Z0)={(q0,ε)}
δ(q1,1,R)={(q1,ε)}

Proving the given language L as not context-free using Pumping lemma


8. Using pumping lemma Prove that the following languages are not context-free.
(a) 𝐿 = {𝑎𝑛 𝑏 𝑛 𝑐 𝑛 | 𝑛 ≥ 1}
(b) 𝐿 = {𝑎𝑝 | 𝑝 is prime}
(c) The set of all strings over {a, b, c} in which the number of occurrences of a, b, c is the
same.
(d) 𝐿 = {𝑎𝑚 𝑏 𝑚 𝑐 𝑛 | 𝑚 ≤ 𝑛 ≤ 2𝑚}
(e) 𝐿 = {𝑎𝑚 𝑏 𝑛 | 𝑛 = 𝑚2 }

Common questions

Powered by AI

Converting a CFG to PDA involves simulating the leftmost derivation of strings generated by the CFG. 1. Create a start state with stack containing the start variable of grammar. 2. For each production, develop transition rules that simulate substituting variables with production body. 3. Make transitions to states carrying out leftmost derivation by popping a variable from stack and pushing the RHS of production. 4. Ensure acceptance by final state or empty stack as per requirement .

To prove the ambiguity of the grammar S → SbS | a, construct derivation trees for the same string and show that multiple distinct trees can be derived. For instance, from the string 'abab', derive as: S -> SbS -> aSbS -> aaSbS -> aabS -> aaba and alternatively S -> SbS -> SbSbS -> abSbS -> ababS -> abab. Both trees result in the same string, showing the grammar is ambiguous .

Ambiguity is shown by deriving the same string in multiple distinct ways using different parse trees. For the given grammar S→aB|ab, show multiple derivation trees for a string like 'ab'. For example, derive 'ab' as S → ab and alternatively using productions through S→aB, B→b. Different derivations resulting in 'ab' from both methods highlight the grammar's ambiguity .

The language L = {anbncn|n ≥ 1} is not context-free because it does not satisfy the conditions of the Pumping Lemma, which stipulates that strings can be decomposed into uvwxy and repeated while staying in the language. However, no division of 'uvwxy' allows repeating the sequence and ensuring equal numbers of 'a's, 'b's, and 'c's as required by the structure of L. Pumping 'vw' will disturb the balance needed between these symbols .

To construct a CFG from the given PDA, model the stack operations into non-terminal symbols of CFG. Each transition in PDA is represented by a production in CFG. For each state transition in PDA, derive productions that translate stack operations into derivations. Replace PDA transitions with non-terminals using productions that simulate pushing and popping operations of PDA to derive strings in the language .

To design a PDA for L = {anbn | n >= 0}, follow these steps: 1. Start with initial state q0 with an empty stack symbol. 2. On reading 'a', push 'A' onto the stack. 3. Transition to a new state on reading each 'b' and pop 'A' from the stack for each 'b'. 4. Accept by empty stack when all symbols are read, ensuring all 'A's pushed are popped, requiring an equal number of 'a's and 'b's .

The Pumping Lemma for CFLs states that for any context-free language L, there exists a pumping length p such that any string s in L of length at least p can be decomposed into uvwxy such that for any i ≥ 0, uviwxiy is in L. For L = {ap | p is prime}, no such decomposition uvwxy will maintain condition that the number of 'a's is still a prime number when pumping. Thus, L cannot satisfy the conditions of the Pumping Lemma for CFLs, proving it is not context-free .

The leftmost derivation for 'a=b⋆c+d/e' using the grammar E→E+E|E-E|E⋆E|E/E|(E)|id can be: E -> E+E -> E+E/E -> E+E/id -> E+E/id -> E/id+E/id -> id/id+E/id -> id/id+id/id. The rightmost derivation can be derived as: E -> E/E -> E/id -> E/id -> E/id+d/E -> E/id+d/id -> E/id+c+d/id -> E/id/id+d/id -> id/id/id+d/id .

To convert a CFG into Chomsky Normal Form (CNF), ensure that all productions are of the form A → BC or A → a, where A, B, C are non-terminal symbols and a is a terminal symbol. Remove any ε-productions, unit productions, and useless symbols, then ensure all rules follow the binary form by introducing new non-terminal symbols as necessary. For example, convert S→ABa, A→aab, B→Ac to CNF by breaking down multi-symbols on the right side .

Converting a CFG to GNF requires ensuring all production rules begin with a terminal symbol optionally followed by non-terminal symbols. This form enables better understanding and parsing algorithms, notably top-down parsing. To achieve GNF, left-recursion must be removed and productions reorganized to ensure the leading symbol is a terminal by introducing new non-terminals and rewriting rules to match GNF constraints systematically .

You might also like