FORMAL LANGUAGE & AUTOMATA THEORY

100406
Back to FORMAL LANGUAGE & AUTOMATA THEORY

Module 2: Context-Free Languages (CFL) and PDA

  1. Q1a. A language accepted by deterministic pushdown automata is closed under which of the following? (i) Complement (ii) Union (iii) Both (i) and (ii) (iv) None of the above20232m

    Module 2: Context-Free Languages (CFL) and PDA

    A language accepted by deterministic pushdown automata is closed under which of the following?

    (i) Complement
    (ii) Union
    (iii) Both (i) and (ii)
    (iv) None of the above

    View this question on its own page →
    Worked Solution

    Answer

    Correct Option: (i) Complement

    Explanation

    Languages accepted by Deterministic Pushdown Automata (DPDA) are Deterministic Context-Free Languages (DCFLs).

    Closure Properties of DCFLs

    Operation Closed?
    Complement Yes
    Union No
    Intersection No
    Intersection with Regular Yes
    Concatenation No
    Kleene Star No

    Reason:

    • A DPDA can be complemented by swapping final/non-final states after ensuring it is total and handles end-of-input deterministically, similar to DFA complementation, but adapted for stack handling.
    • DCFLs are not closed under union: e.g., L1={anbn∣n≥0}L_1 = \{a^n b^n \mid n\ge0\} and L2={anb2n∣n≥0}L_2 = \{a^n b^{2n} \mid n\ge0\} are DCFLs, but L1∪L2L_1 \cup L_2 is not a DCFL.
    • Since union fails, (iii) Both is false and (iv) is false.

    Contrast with general CFLs (NPDA)

    CFLs (non-deterministic) are not closed under complement nor intersection, but are closed under union, concatenation, Kleene star – opposite behaviour.

    Final Answer

    DCFL is closed under complement only. Hence option (i) Complement is correct.

  2. Q1d. Context-free languages are NOT closed under (i) Union (ii) Concatenation (iii) Intersection (iv) Kleene star20252m

    Module 2: Context-Free Languages (CFL) and PDA

    Context-free languages are NOT closed under

    (i) Union
    (ii) Concatenation
    (iii) Intersection
    (iv) Kleene star

    View this question on its own page →
    Worked Solution

    Answer

    Correct Option: (iii) Intersection

    Explanation

    Closure properties of Context-Free Languages (CFLs):

    Operation Closed?
    Union L1∪L2L_1 \cup L_2 Yes – S→S1∣S2S \rightarrow S_1 \mid S_2
    Concatenation L1L2L_1L_2 Yes – S→S1S2S \rightarrow S_1S_2
    Kleene star L∗L^* Yes – S→SS1∣ϵS \rightarrow SS_1 \mid \epsilon
    Intersection L1∩L2L_1 \cap L_2 No
    Complement L‾\overline{L} No

    Counterexample for intersection:
    L1={anbncm∣n,m≥0}L_1 = \{a^n b^n c^m \mid n,m\ge0\} (CFL), L2={ambncn∣n,m≥0}L_2 = \{a^m b^n c^n \mid n,m\ge0\} (CFL)
    L1∩L2={anbncn}L_1 \cap L_2 = \{a^n b^n c^n\} which is not CFL (pumping lemma).

    Since De Morgan: L1∩L2= L1‾∪L2‾ ‾L_1 \cap L_2 = \overline{\,\overline{L_1}\cup\overline{L_2}\,}, non-closure under intersection implies non-closure under complement.

    Final Answer

    CFLs are NOT closed under Intersection → Option (iii).

  3. Q1e. If L_1 and L_2 are CFLs, L_1 - L_2 is ____ context-free. (i) always (ii) sometimes (iii) never (iv) None of the above20212m

    Module 2: Context-Free Languages (CFL) and PDA

    If L1L_1 and L2L_2 are CFLs, L1−L2L_1 - L_2 is ____ context-free.

    (i) always
    (ii) sometimes
    (iii) never
    (iv) None of the above

    View this question on its own page →
    Worked Solution

    Answer

    Correct Option: (ii) sometimes (i.e., not always — CFLs are not closed under difference)

    Explanation

    L1−L2=L1∩L2‾L_1 - L_2 = L_1 \cap \overline{L_2}.

    • CFLs are closed under union, concatenation, Kleene star, but NOT under intersection, complement, difference.
    • Since complement of a CFL is not necessarily CFL, difference need not be CFL.

    Counterexample:
    L1=a∗b∗c∗L_1 = a^*b^*c^* (actually regular, also CFL)
    Let L1={anbncm}L_1 = \{a^n b^n c^m\} (CFL) and L2={ambncn}L_2 = \{a^m b^n c^n\} (CFL)
    L1∩L2={anbncn}L_1 \cap L_2 = \{a^n b^n c^n\} not CFL.
    Then L1−(Σ∗−L2)L_1 - ( \Sigma^* - L_2) style constructions show difference can be non-CFL.

    But sometimes it is CFL: e.g., if L2L_2 is regular, then L1−L2=L1∩L2‾L_1 - L_2 = L_1 \cap \overline{L_2} is CFL because CFL ∩ Regular = CFL. So difference is sometimes CFL.

    Many textbooks phrase options as:

    • (i) always — false
    • (ii) sometimes / not always closed
    • (iii) never — false (counterexample above shows sometimes)

    Hence correct is not necessarily CFL → Option (ii) sometimes (or “not always”). If exam key uses “never”, they mean “not closed”, also acceptable as (iii) in that phrasing — but strictly “sometimes” is precise.

    Final Answer

    L1−L2L_1 - L_2 is not guaranteed CFL → Not closed → Option (ii) sometimes / not always (exam may list as (iii) never in weaker phrasing).

  4. Q1f. Which of the following does not have left recursion? (i) Chomsky Normal Form (ii) Greibach Normal Form (iii) Backus–Naur Form (iv) All of the above20212m

    Module 2: Context-Free Languages (CFL) and PDA

    Which of the following does not have left recursion?

    (i) Chomsky Normal Form
    (ii) Greibach Normal Form
    (iii) Backus–Naur Form
    (iv) All of the above

    View this question on its own page →
    Worked Solution

    Answer

    Correct Option: (i) Chomsky Normal Form and (ii) Greibach Normal Form — both have no left recursion; if single choice required, (i) CNF is the textbook answer, and BNF may contain left recursion.

    Explanation

    Left recursion: Production A→AαA \rightarrow A\alpha.

    • Chomsky Normal Form (CNF): A→BCA \rightarrow BC or A→aA \rightarrow a, where B,C∈VB,C\in V, a∈Ta\in T. No AA on RHS leftmost as non-terminal with same AA, and no A→AαA\rightarrow A\alpha. No left recursion.
    • Greibach Normal Form (GNF): A→aαA \rightarrow a\alpha, where a∈Ta\in T, α∈V∗\alpha\in V^*. Starts with terminal → no left recursion by definition.
    • Backus-Naur Form (BNF): General notation for CFG, e.g., A::=A+B∣BA ::= A + B \mid B. May contain left recursion (common in expression grammars).

    Options:

    • (i) CNF → no left recursion ✓
    • (ii) GNF → no left recursion ✓
    • (iii) BNF → can have left recursion ✗
    • (iv) All of above → false because BNF can have it.

    If exam expects single answer, CNF (and GNF) are correct; many keys mark (i) Chomsky Normal Form.

    Final Answer

    CNF / GNF have no left recursion → Option (i) (and (ii)). BNF can have left recursion.

  5. Q1f. A given grammar is called ambiguous if: (i) two or more productions have the same non-terminal on LHS (ii) a derivation tree has more than one associated sentence (iii) there exists a sentence with more than one derivation tree (iv) brackets are not present in the grammar20232m

    Module 2: Context-Free Languages (CFL) and PDA

    A given grammar is called ambiguous if:

    (i) two or more productions have the same non-terminal on LHS
    (ii) a derivation tree has more than one associated sentence
    (iii) there exists a sentence with more than one derivation tree
    (iv) brackets are not present in the grammar

    View this question on its own page →
    Worked Solution

    Answer

    Correct Option: (iii) there exists a sentence with more than one derivation tree

    Explanation

    Ambiguous Grammar: Grammar GG is ambiguous iff there exists a string w∈L(G)w \in L(G) that has two or more distinct derivation trees (or equivalently two distinct leftmost / rightmost derivations).

    • (i) “two productions have same LHS” → false; e.g., S→a∣bS \rightarrow a \mid b has same LHS but not ambiguous (each string one tree).
    • (ii) “derivation tree has more than one sentence” — every tree yields exactly one sentence (its yield), false.
    • (iii) True — definition of ambiguity: ∃w:∣{parse trees for w}∣≥2\exists w: |\{ \text{parse trees for }w\}| \ge 2.
    • (iv) “brackets not present” — irrelevant.

    Example ambiguous: S→S+S∣S∗S∣aS \rightarrow S+S \mid S*S \mid a; string a+a∗aa+a*a has two trees (different precedence).

    Final Answer

    Ambiguous iff some sentence has ≥2 parse trees → Option (iii).

  6. Q1g. Which one of the following statements is true? (i) The intersection of two CFLs is context-free (ii) Every CFL can be accepted by a DPDA (iii) The union of two CFLs is context-free (iv) The complement of a CFL is context-free20232m

    Module 2: Context-Free Languages (CFL) and PDA

    Which one of the following statements is true?

    (i) The intersection of two CFLs is context-free
    (ii) Every CFL can be accepted by a DPDA
    (iii) The union of two CFLs is context-free
    (iv) The complement of a CFL is context-free

    View this question on its own page →
    Worked Solution

    Answer

    Correct Option: (iii) The union of two CFLs is context-free

    Explanation

    CFL closure:

    Operation Closed? Example
    Union L1∪L2L_1 \cup L_2 Yes S→S1∣S2S \rightarrow S_1 \mid S_2
    Intersection L1∩L2L_1 \cap L_2 No anbnc∗∩a∗bncn=anbncna^n b^n c^* \cap a^* b^n c^n = a^n b^n c^n not CFL
    Complement No Follows from De Morgan
    Every CFL is DPDA No {anbn}∪{anb2n}\{a^n b^n\} \cup \{a^n b^{2n}\} is CFL but not DCFL
    Intersection of 2 CFLs is CFL No

    Thus only union is guaranteed.

    Options:

    • (i) Intersection → false
    • (ii) Every CFL is DPDA → false (DCFL ⊂ CFL)
    • (iii) Union → true ✓
    • (iv) Complement → false

    Final Answer

    Union of two CFLs is CFL → Option (iii).

  7. Q1h. ____ is the acyclic graphical representation of a grammar. (i) Binary tree (ii) Octtree (iii) Parse tree (iv) None of the above20212m

    Module 2: Context-Free Languages (CFL) and PDA

    ____ is the acyclic graphical representation of a grammar.

    (i) Binary tree
    (ii) Octtree
    (iii) Parse tree
    (iv) None of the above

    View this question on its own page →
    Worked Solution

    Answer

    Correct Option: (iii) Parse tree

    Explanation

    Parse tree (derivation tree / syntax tree): Acyclic, rooted, ordered tree representing derivation.

    • Root = start symbol SS
    • Internal nodes = non-terminals
    • Leaves = terminals (yield = derived string)
    • No cycles (grammar is context-free, derivation finite).

    Not binary tree specifically (parse tree can be nn-ary), not octtree. Binary tree is for CNF.

    Final Answer

    Parse tree → Option (iii).

  8. Q1i. Which of the following CFLs is deterministic? (i) {a^n b^n} (ii) Arithmetic expressions (iii) Palindromes (iv) {ww^R}20252m

    Module 2: Context-Free Languages (CFL) and PDA

    Which of the following CFLs is deterministic?

    (i) {anbna^n b^n}
    (ii) Arithmetic expressions
    (iii) Palindromes
    (iv) {wwRww^R}

    View this question on its own page →
    Worked Solution

    Answer

    Correct Option: (i) {anbn}\{a^n b^n\}

    Explanation

    Deterministic CFL (DCFL): Accepted by DPDA (deterministic pushdown).

    • (i) {anbn}\{a^n b^n\}: DPDA can push aa's, then pop for bb's, deterministic (no choice). DCFL.
    • (ii) Arithmetic expressions (e.g., E→E+E∣E∗EE \rightarrow E+E \mid E*E) → inherently ambiguous, CFL but not deterministic (needs guessing).
    • (iii) Palindromes {wwR}\{ww^R\} → not deterministic (needs to guess middle). {w#wR}\{w\#w^R\} is DCFL, but plain palindromes is NPD A.
    • (iv) {wwR}\{ww^R\} similar to (iii) — not DCFL.

    Hence only {anbn}\{a^n b^n\} is deterministic.

    Final Answer

    {anbn}\{a^n b^n\} is DCFL → Option (i).

  9. Q1j. Which of the following is a PDA? (i) Finite automaton with a stack (ii) Turing machine with a stack (iii) Finite automaton with a queue (iv) Turing machine with a queue20242m

    Module 2: Context-Free Languages (CFL) and PDA

    Which of the following is a PDA?

    (i) Finite automaton with a stack
    (ii) Turing machine with a stack
    (iii) Finite automaton with a queue
    (iv) Turing machine with a queue

    View this question on its own page →
    Worked Solution

    Answer

    Correct Option: (i) Finite automaton with a stack

    Explanation

    PDA definition: P=(Q,Σ,Γ,δ,q0,Z0,F)P = (Q,\Sigma,\Gamma,\delta,q_0,Z_0,F)

    • Essentially a finite automaton (finite control) augmented with a stack (LIFO memory).
    • Stack provides unbounded memory but restricted access (push/pop at top only).
    • Enables recognition of CFLs: e.g., {anbn}\{a^n b^n\} by pushing aa's, popping for bb's.

    Options:

    • (ii) TM with stack → equivalent to TM (stack is not needed)
    • (iii) FA with queue → queue automaton = TM-powerful
    • PDA = FA + stack.

    Final Answer

    PDA = FA + stack → Option (i).

  10. Q2a. Given the grammar G = (\{S\}, \{a, b\}, P, S) P: S \rightarrow aSb | ab Derive the string aabb using leftmost derivation, rightmost derivation and also draw the parse tree.20257m

    Module 2: Context-Free Languages (CFL) and PDA

    Given the grammar
    G=({S},{a,b},P,S)G = (\{S\}, \{a, b\}, P, S)
    P:S→aSb∣abP: S \rightarrow aSb | ab

    Derive the string aabb using leftmost derivation, rightmost derivation and also draw the parse tree.

    View this question on its own page →
    Worked Solution

    Answer

    Grammar: G=({S},{a,b},P,S)G = (\{S\}, \{a,b\}, P, S) with P:S→aSb∣abP: S \rightarrow aSb \mid ab

    1. Leftmost Derivation (LMD)

    At each step, replace the leftmost non-terminal.

    S⇒lmaSb(using S→aSb)⇒lmaaSbb(using S→aSb on leftmost S)⇒lmaabbb(using S→ab on leftmost S)=aabb\begin{aligned} S &\Rightarrow_{\text{lm}} aSb & \text{(using $S \rightarrow aSb$)} \\ &\Rightarrow_{\text{lm}} aaSbb & \text{(using $S \rightarrow aSb$ on leftmost $S$)} \\ &\Rightarrow_{\text{lm}} aabbb & \text{(using $S \rightarrow ab$ on leftmost $S$)} \\ &= \mathbf{aabb} \end{aligned}

    LMD Steps: S⇒lmaSb⇒lmaaSbb⇒lmaabbb=aabbS \xRightarrow{lm} aSb \xRightarrow{lm} aaSbb \xRightarrow{lm} aabbb = aabb

    2. Rightmost Derivation (RMD)

    At each step, replace the rightmost non-terminal.

    S⇒rmaSb(using S→aSb)⇒rmaaSbb(using S→aSb on rightmost S)⇒rmaabbb(using S→ab on rightmost S)=aabb\begin{aligned} S &\Rightarrow_{\text{rm}} aSb & \text{(using $S \rightarrow aSb$)} \\ &\Rightarrow_{\text{rm}} aaSbb & \text{(using $S \rightarrow aSb$ on rightmost $S$)} \\ &\Rightarrow_{\text{rm}} aabbb & \text{(using $S \rightarrow ab$ on rightmost $S$)} \\ &= \mathbf{aabb} \end{aligned}

    RMD Steps: S⇒rmaSb⇒rmaaSbb⇒rmaabbb=aabbS \xRightarrow{rm} aSb \xRightarrow{rm} aaSbb \xRightarrow{rm} aabbb = aabb

    Note: Since this grammar is unambiguous and has only one non-terminal, LMD and RMD coincide in the sequence of sentential forms.

    3. Parse Tree (Derivation Tree)

                S
               /|\
              a S b
                |
                S
               /|\
              a S b
                |
                ab
    

    Text/Mermaid representation:

    MermaidDiagram
    Rendering diagram…

    Tree structure:

    • Root: SS
    • Internal nodes: SS (non-terminals)
    • Leaves (yield): a,a,b,ba, a, b, b (reading left to right = aabb)
    • Height: 3 (excluding root)

    Verification

    • String generated: aabb ✓
    • Language of grammar: L(G)={anbn∣n≥1}L(G) = \{a^n b^n \mid n \ge 1\}
    • aabb corresponds to n=2n=2 ✓

    Final Answer

    LMD: S⇒lmaSb⇒lmaaSbb⇒lmaabbbS \xRightarrow{lm} aSb \xRightarrow{lm} aaSbb \xRightarrow{lm} aabbb
    RMD: S⇒rmaSb⇒rmaaSbb⇒rmaabbbS \xRightarrow{rm} aSb \xRightarrow{rm} aaSbb \xRightarrow{rm} aabbb
    Parse Tree: As shown above (yield = aabb).

  11. Budget App•POCKETLY

    Pocketly on Android

    Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.

    Install Pocketly
  12. Q2b. State the pumping lemma for context-free languages.20237m

    Module 2: Context-Free Languages (CFL) and PDA

    State the pumping lemma for context-free languages.

    View this question on its own page →
    Worked Solution

    Answer

    Pumping Lemma for Context-Free Languages (CFL)

    Statement:
    Let LL be a context-free language. Then there exists a constant p≥1p \ge 1 (the pumping length) such that for every string w∈Lw \in L with ∣w∣≥p|w| \ge p, ww can be written as

    w=uvxyz w = uvxyz

    satisfying the following conditions:

    1. ∣vxy∣≤p|vxy| \le p — the middle part vxyvxy is short (bounded by pp)
    2. ∣vy∣≥1|vy| \ge 1 — at least one of vv or yy is non-empty (not both empty)
    3. For all i≥0i \ge 0: uvixyiz∈Luv^i x y^i z \in L — pumping vv and yy together (0, 1, 2, … times) keeps the string in LL

    Interpretation

    • For any sufficiently long CFL string, you can find a decomposition into 5 parts such that the inner vv and yy can be "pumped" simultaneously (repeated same number of times) and the result stays in the language.
    • The simultaneous pumping of vv and yy is the key difference from the regular pumping lemma (which pumps a single yy).
    • ∣vxy∣≤p|vxy| \le p means the "pumpable" region is near the center of the string (within length pp).

    How to Use (Proving Non-CFL)

    To prove LL is not context-free:

    1. Assume LL is CFL → pumping lemma holds with some pp.
    2. Choose a specific string w∈Lw \in L with ∣w∣≥p|w| \ge p (usually w=apbpcpw = a^p b^p c^p or similar).
    3. Show that for every valid decomposition w=uvxyzw = uvxyz satisfying (1) and (2), there exists some ii such that $uv^i x y^i z
      otin L$.
    4. Contradiction → LL not CFL.

    Common Strategy: Choose ww so that any vxyvxy of length ≤p\le p cannot span all "coordinated" parts (e.g., three blocks apbpcpa^p b^p c^p). Pumping disrupts the balance.


    Example Application

    Prove L={anbncn∣n≥0}L = \{a^n b^n c^n \mid n \ge 0\} is not CFL.

    1. Let pp be pumping length. Take w=apbpcp∈Lw = a^p b^p c^p \in L, ∣w∣=3p≥p|w| = 3p \ge p.
    2. By lemma, w=uvxyzw = uvxyz with ∣vxy∣≤p|vxy| \le p, ∣vy∣≥1|vy| \ge 1.
    3. Since ∣vxy∣≤p|vxy| \le p, the substring vxyvxy cannot contain aa's, bb's, and cc's simultaneously — it lies within at most two adjacent blocks.
    4. Cases:
      • vxyvxy only in aa's: pumping adds aa's → $#a
        eq #b = #c$ ❌
      • vxyvxy only in bb's: pumping adds bb's → $#b
        eq #a, #c$ ❌
      • vxyvxy only in cc's: pumping adds cc's ❌
      • vxyvxy spans apbpa^p b^p boundary: pumping changes #a\#a and #b\#b differently (or adds both) → $#a
        eq #bor or #b
        eq #c$ ❌
      • vxyvxy spans bpcpb^p c^p boundary: similar ❌
    5. All decompositions fail for some $i
      e 1$. Contradiction.

    Contrast with Regular Pumping Lemma

    Aspect Regular (Type-3) CFL (Type-2)
    String split w=xyzw = xyz w=uvxyzw = uvxyz
    Pumped part yy (single) vv and yy (pair)
    Constraint ∣xy∣≤p|xy| \le p ∣vxy∣≤p|vxy| \le p
    Non-empty ∣y∣≥1|y| \ge 1 ∣vy∣≥1|vy| \ge 1
    Result xyiz∈Lxy^i z \in L uvixyiz∈Luv^i x y^i z \in L

    Important Notes

    • The lemma gives necessary but not sufficient condition for CFL.
    • There are non-CFL languages that satisfy the lemma (e.g., some non-context-free languages still pump).
    • For CFL, pumping is simultaneous of two substrings (vv and yy), reflecting stack push/pop pairing.
    • The pumping length pp depends on the grammar (e.g., number of variables in Chomsky Normal Form).

    Final Answer

    Pumping Lemma for CFL: ∃p≥1\exists p \ge 1 such that ∀w∈L,∣w∣≥p\forall w \in L, |w| \ge p, w=uvxyzw = uvxyz with ∣vxy∣≤p|vxy| \le p, ∣vy∣≥1|vy| \ge 1, and ∀i≥0,uvixyiz∈L\forall i \ge 0, uv^i x y^i z \in L. Used to prove languages like {anbncn}\{a^n b^n c^n\} are not context-free.

  13. Q3a. Design a PDA (empty stack and final state both) for L = \{ WW^r \mid W \in \{a,b\}^* \}.20237m

    Module 2: Context-Free Languages (CFL) and PDA

    Design a PDA (empty stack and final state both) for L={WWr∣W∈{a,b}∗}L = \{ WW^r \mid W \in \{a,b\}^* \}.

    View this question on its own page →
    Worked Solution

    Answer

    Language: L={WWr∣W∈{a,b}∗}L = \{ W W^r \mid W \in \{a,b\}^* \} — even-length palindromes over {a,b}\{a,b\}.

    WrW^r = reverse of WW. Examples: ϵ\epsilon, aaaa, bbbb, abbaabba, baabbaab, aabbaaaabbaa, …


    PDA Design (NPDA)

    This language is not deterministic (NPDA needed) — the PDA must guess the midpoint.

    PDA: P=(Q,Σ,Γ,δ,q0,Z0,F)P = (Q, \Sigma, \Gamma, \delta, q_0, Z_0, F)

    • Q={q0,q1,q2}Q = \{q_0, q_1, q_2\}
      • q0q_0: reading first half, pushing symbols
      • q1q_1: reading second half, popping & matching
      • q2q_2: accept (final state)
    • Σ={a,b}\Sigma = \{a, b\}
    • Γ={a,b,Z0}\Gamma = \{a, b, Z_0\} (Z0Z_0 = bottom marker)
    • q0q_0 = start
    • F={q2}F = \{q_2\} (final state)
    • Empty stack also accepted: Z0Z_0 remains at bottom, all input symbols pushed then popped

    Transition Function δ\delta

    Current State Input Stack Top Action Next State Push/Pop
    q0q_0 aa X∈{a,b,Z0}X \in \{a,b,Z_0\} Push aa q0q_0 aa
    q0q_0 bb XX Push bb q0q_0 bb
    q0q_0 ϵ\epsilon XX Guess midpoint, switch to pop q1q_1 ϵ\epsilon (no change)
    q1q_1 aa aa Pop & match q1q_1 ϵ\epsilon (pop)
    q1q_1 bb bb Pop & match q1q_1 ϵ\epsilon (pop)
    q1q_1 ϵ\epsilon Z0Z_0 All matched, stack = Z0Z_0 q2q_2 ϵ\epsilon

    Formally:

    • δ(q0,a,X)={(q0,aX)}\delta(q_0, a, X) = \{(q_0, aX)\} for X∈{a,b,Z0}X \in \{a,b,Z_0\}
    • δ(q0,b,X)={(q0,bX)}\delta(q_0, b, X) = \{(q_0, bX)\} for X∈{a,b,Z0}X \in \{a,b,Z_0\}
    • δ(q0,ϵ,X)={(q1,X)}\delta(q_0, \epsilon, X) = \{(q_1, X)\} for X∈{a,b,Z0}X \in \{a,b,Z_0\} (midpoint guess)
    • δ(q1,a,a)={(q1,ϵ)}\delta(q_1, a, a) = \{(q_1, \epsilon)\}
    • δ(q1,b,b)={(q1,ϵ)}\delta(q_1, b, b) = \{(q_1, \epsilon)\}
    • δ(q1,ϵ,Z0)={(q2,ϵ)}\delta(q_1, \epsilon, Z_0) = \{(q_2, \epsilon)\}

    State Diagram

             a,b / X → aX,bX
          ┌───────────────────┐
          ▼                   │
      (q0)──ϵ, X/X──►(q1)──a/a:pop,b/b:pop──►(q1)
          │               │
          │               ϵ, Z0/ϵ
          │               ▼
          └──────────────►(q2)  [Accept]
    

    How It Works (Example: W=abW=ab → abbaabba)

    1. Push phase (q0q_0):

      • Read a, push a: stack = a Z0
      • Read b, push b: stack = b a Z0
      • Guess midpoint (ϵ\epsilon): go to q1q_1, stack unchanged b a Z0
    2. Pop phase (q1q_1):

      • Read b, top=b → pop → stack = a Z0
      • Read a, top=a → pop → stack = Z0
      • Input exhausted, ϵ\epsilon-transition on Z0Z_0 → q2q_2
    3. Accept: q2∈Fq_2 \in F, or stack = Z0Z_0 (empty-stack acceptance).


    Acceptance Modes

    Mode Condition
    Final State Reach q2∈Fq_2 \in F
    Empty Stack After reading all input, stack = Z0Z_0 (all pushed symbols matched and popped, only Z0Z_0 remains)

    Both equivalent here; PDA accepts by both.


    Why Nondeterministic?

    • The ϵ\epsilon-transition from q0q_0 to q1q_1 guesses the midpoint.
    • For odd-length strings (not in LL), no guess works → rejects.
    • For WWrWW^r, correct guess at exact middle → accepts.
    • DPDA cannot guess midpoint deterministically → LL not DCFL.

    Alternative: CFG

    S→aSa∣bSb∣ϵS \rightarrow aSa \mid bSb \mid \epsilon generates LL.


    Final Answer

    NPDA with 3 states (q0,q1,q2q_0, q_1, q_2):

    • q0q_0: push a,ba,b onto stack while reading first half
    • ϵ\epsilon-transition: guess midpoint, switch to q1q_1
    • q1q_1: pop & match each input symbol with stack top
    • ϵ\epsilon on Z0Z_0: accept (q2q_2)
    • Accepts by final state q2q_2 and empty stack (Z0Z_0 only).
  14. Q3a. Design a context-free grammar (CFG) that generates all palindromes over the alphabet {0, 1}. The grammar should generate both even-length palindromes (e.g., 0110) and odd-length palindromes (e.g., 010).20257m

    Module 2: Context-Free Languages (CFL) and PDA

    Design a context-free grammar (CFG) that generates all palindromes over the alphabet {0, 1}. The grammar should generate both even-length palindromes (e.g., 0110) and odd-length palindromes (e.g., 010).

    View this question on its own page →
  15. Q3a. Prove that if L is accepted by a PDA, then L can be generated by a CFG.20227m

    Module 2: Context-Free Languages (CFL) and PDA

    Prove that if LL is accepted by a PDA, then LL can be generated by a CFG.

    View this question on its own page →
    Worked Solution

    Answer

    Theorem: LL is accepted by a PDA   ⟺  \iff LL is generated by a CFG (i.e., CFL = NPDA languages).

    We prove PDA →\rightarrow CFG using the Triplet Construction (Sipser / standard textbook method).


    Given

    PDA P=(Q,Σ,Γ,δ,q0,Z0,F)P = (Q, \Sigma, \Gamma, \delta, q_0, Z_0, F) accepting L(P)L(P) by final state (empty stack can be converted).

    Assume δ\delta has no ϵ\epsilon-transitions on input (can be eliminated). If ϵ\epsilon-moves exist, handle via closure.


    Construction of CFG G=(V,Σ,R,S)G = (V, \Sigma, R, S)

    Variables VV:

    • Start symbol SS
    • Triplet variables [pAq][p A q] for every p,q∈Qp,q \in Q and A∈ΓA \in \Gamma
      • Meaning: "Starting in state pp with stack top AA, PP can reach state qq with AA popped off (and any symbols pushed above it matched)."

    Productions RR:

    1. Start: S→[q0Z0p]S \rightarrow [q_0 Z_0 p] for each p∈Fp \in F

      • Begin at q0q_0 with initial stack Z0Z_0, end in any final state pp.
    2. From PDA transitions:
      For every transition $\delta(p, a, A)
      i (q, \alpha)where where a \in \Sigma \cup {\epsilon}$:

      • Case α=ϵ\alpha = \epsilon (pop AA, no push):
        [pAq]→a [p A q] \rightarrow a
        (if a=ϵa=\epsilon, RHS is ϵ\epsilon)

      • Case α=B1B2…Bk\alpha = B_1 B_2 \dots B_k (k≥1k \ge 1):
        For all r1,r2,…,rk+1∈Qr_1, r_2, \dots, r_{k+1} \in Q:
        [pArk+1]→a  [qB1r1]  [r1B2r2]…[rkBkrk+1] [p A r_{k+1}] \rightarrow a\; [q B_1 r_1]\; [r_1 B_2 r_2] \dots [r_k B_k r_{k+1}]

        • Push B1…BkB_1\dots B_k; must pop them in order via intermediate states.
        • aa consumed on transition; each [riBi+1ri+1][r_i B_{i+1} r_{i+1}] matches push/pop of Bi+1B_{i+1}.
    3. Empty stack (optional, for empty-stack acceptance):
      Add [pϵp]→ϵ[p \epsilon p] \rightarrow \epsilon for all p∈Qp \in Q (not needed for final-state).


    Intuition of Triplet [pAq][p A q]

    • Represents a matching push-pop pair on symbol AA.
    • pp: state where AA is pushed
    • qq: state where AA is popped
    • The derivation inside [pAq][p A q] generates the string read between push and pop of AA.

    Proof of Correctness (Sketch)

    Soundness (CFG ⊆\subseteq PDA):
    Every derivation in GG corresponds to a valid PDA computation:

    • S⇒∗[q0Z0p]S \Rightarrow^* [q_0 Z_0 p] maps to initial config.
    • Production [pAr]→a[qBr1]…[p A r] \rightarrow a [q B r_1] \dots mimics transition (p,a,A)→(q,B… )(p,a,A) \to (q,B\dots).
    • Base case [pAq]→a[p A q] \rightarrow a mimics (p,a,A)→(q,ϵ)(p,a,A) \to (q,\epsilon).
    • Induction on derivation length shows derived string is accepted by PP.

    Completeness (PDA ⊆\subseteq CFG):
    For any accepting run of PP on ww, build a parse tree in GG:

    • Root S→[q0Z0p]S \to [q_0 Z_0 p].
    • Each push of AA at state pp and corresponding pop at qq gives variable [pAq][p A q].
    • The substring read between push and pop is generated by that variable.
    • By induction on height of push-pop pairs, ww is generated.

    Example

    PDA for {anbn}\{a^n b^n\}:

    • δ(q0,a,Z0)=(q0,XZ0)\delta(q_0, a, Z_0) = (q_0, XZ_0)
    • δ(q0,a,X)=(q0,XX)\delta(q_0, a, X) = (q_0, XX)
    • δ(q0,b,X)=(q1,ϵ)\delta(q_0, b, X) = (q_1, \epsilon)
    • F={q1}F = \{q_1\}

    CFG variables: S,[q0Z0q1],[q0Xq0],[q0Xq1],…S, [q_0 Z_0 q_1], [q_0 X q_0], [q_0 X q_1], \dots
    Productions:
    S→[q0Z0q1]S \rightarrow [q_0 Z_0 q_1]
    [q0Xq1]→a[q0Xq1]b∣ab[q_0 X q_1] \rightarrow a [q_0 X q_1] b \mid ab (after resolving all intermediate states)
    [q0Z0q1]→a[q0Xq1]b[q_0 Z_0 q_1] \rightarrow a [q_0 X q_1] b

    Yields anbna^n b^n.


    Formal Statement

    Theorem: LL is accepted by a (nondeterministic) PDA iff LL is generated by a CFG.
    Corollary: CFL = languages accepted by NPDA. DPDA ⊂\subset CFL (strict).


    Final Answer

    Proof via Triplet Construction: Create CFG with variables [pAq][p A q] representing push-pop of AA from state pp to qq. Productions mirror PDA transitions. S→[q0Z0p]S \to [q_0 Z_0 p] for final pp. Induction on derivations/computations proves L(G)=L(P)L(G) = L(P). Hence every PDA language is a CFL.

  16. Q3b. Design an NPDA (empty stack and final state acceptance) for: L = \{\text{palindromes over } \{a,b\}\}20227m

    Module 2: Context-Free Languages (CFL) and PDA

    Design an NPDA (empty stack and final state acceptance) for:

    L={palindromes over {a,b}} L = \{\text{palindromes over } \{a,b\}\}

    View this question on its own page →
  17. Q3b. Eliminate \varepsilon, unit, and useless productions from the grammar: A → bA | Bba | aa B → aba | b | D C → CA | AC | B D → a | \varepsilon20237m

    Module 2: Context-Free Languages (CFL) and PDA

    Eliminate ε\varepsilon, unit, and useless productions from the grammar:

    A → bA | Bba | aa
    B → aba | b | D
    C → CA | AC | B
    D → a | ε\varepsilon

    View this question on its own page →
  18. Q3b. Construct a PDA that accepts: L = \{ u a w b : u,w \in \{a,b\}^*,\; |u| = |w| \}20217m

    Module 2: Context-Free Languages (CFL) and PDA

    Construct a PDA that accepts:

    L={uawb:u,w∈{a,b}∗,  ∣u∣=∣w∣} L = \{ u a w b : u,w \in \{a,b\}^*,\; |u| = |w| \}

    View this question on its own page →
  19. Q4a. Define Chomsky Normal Form. Convert the CFG: S → aSb | ab | Aa A → aab to CNF.20237m

    Module 2: Context-Free Languages (CFL) and PDA

    Define Chomsky Normal Form. Convert the CFG:

    S → aSb | ab | Aa
    A → aab

    to CNF.

    View this question on its own page →
  20. Q4b. Show whether the given grammar is ambiguous: S \to a \mid abSb \mid aAb \\ A \to bS \mid aAAb20227m

    Module 2: Context-Free Languages (CFL) and PDA

    Show whether the given grammar is ambiguous:

    S→a∣abSb∣aAbA→bS∣aAAb S \to a \mid abSb \mid aAb \\ A \to bS \mid aAAb

    View this question on its own page →
  21. Q5a. Using the CYK algorithm, determine whether baaba is in L(G) for the grammar: `` S → AB | BC A → BA | a B → CC | b C → AB | a ``202114m

    Module 2: Context-Free Languages (CFL) and PDA

    Using the CYK algorithm, determine whether baaba is in L(G)L(G) for the grammar:

    S → AB | BC
    A → BA | a
    B → CC | b
    C → AB | a
    
    View this question on its own page →
  22. Budget App•POCKETLY

    Pocketly on Android

    Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.

    Install Pocketly
  23. Q5a. Show that the following grammar is ambiguous: S \rightarrow SS | aSb | bSa | \epsilon20257m

    Module 2: Context-Free Languages (CFL) and PDA

    Show that the following grammar is ambiguous:
    S→SS∣aSb∣bSa∣ϵS \rightarrow SS | aSb | bSa | \epsilon

    View this question on its own page →
  24. Q5b. Suppose L is CFL and R is regular: (i) Is L - R always CFL? (ii) Is R - L always CFL?202114m

    Module 2: Context-Free Languages (CFL) and PDA

    Suppose LL is CFL and RR is regular:

    (i) Is L−RL - R always CFL?
    (ii) Is R−LR - L always CFL?

    View this question on its own page →
  25. Q5b. Write the difference between Pushdown Automata and Finite Automata.20247m

    Module 2: Context-Free Languages (CFL) and PDA

    Write the difference between Pushdown Automata and Finite Automata.

    View this question on its own page →
  26. Q6a. Construct a PDA for the language: L = \{ 0^m 1^n 0^k \mid k \ge 1, m \ge 1, m > n + 2 \}20247m

    Module 2: Context-Free Languages (CFL) and PDA

    Construct a PDA for the language:

    L={0m1n0k∣k≥1,m≥1,m>n+2}L = \{ 0^m 1^n 0^k \mid k \ge 1, m \ge 1, m > n + 2 \}

    View this question on its own page →
  27. Q6a. Construct a CFG for: \{ 0^i 1^j 2^k \mid i + j \ge 2k \}20227m

    Module 2: Context-Free Languages (CFL) and PDA

    Construct a CFG for:

    {0i1j2k∣i+j≥2k} \{ 0^i 1^j 2^k \mid i + j \ge 2k \}

    View this question on its own page →
  28. Q6b. Let G be a CFG in CNF with b variables. Show that if G derives any string using at least 2^b steps, then L(G) is infinite.20217m

    Module 2: Context-Free Languages (CFL) and PDA

    Let GG be a CFG in CNF with bb variables. Show that if GG derives any string using at least 2b2^b steps, then L(G)L(G) is infinite.

    View this question on its own page →
  29. Q6b. Convert the CFG to GNF: S \to AB \mid BC \\ A \to aB \mid bA \mid a \\ B \to bB \mid cC \mid b \\ C \to c20227m

    Module 2: Context-Free Languages (CFL) and PDA

    Convert the CFG to GNF:

    S→AB∣BCA→aB∣bA∣aB→bB∣cC∣bC→c S \to AB \mid BC \\ A \to aB \mid bA \mid a \\ B \to bB \mid cC \mid b \\ C \to c

    View this question on its own page →
  30. Q7a. Construct a PDA for the grammar: S \to 0BB B \to 0S \mid 1S \mid 0 Test whether 0104 is accepted by this PDA.20247m

    Module 2: Context-Free Languages (CFL) and PDA

    Construct a PDA for the grammar:

    S→0BBS \to 0BB
    B→0S∣1S∣0B \to 0S \mid 1S \mid 0

    Test whether 01040104 is accepted by this PDA.

    View this question on its own page →
  31. Q7a. Construct NPDA for the grammar: S \to \alpha ABB \mid \alpha AA \\ A \to \alpha BB \mid \alpha \\ B \to bBB \mid A20237m

    Module 2: Context-Free Languages (CFL) and PDA

    Construct NPDA for the grammar:

    S→αABB∣αAAA→αBB∣αB→bBB∣A S \to \alpha ABB \mid \alpha AA \\ A \to \alpha BB \mid \alpha \\ B \to bBB \mid A

    View this question on its own page →
  32. Q7b. Check whether the grammar is ambiguous: S \to a \mid abSb \mid aAb A \to bS \mid aAAb20247m

    Module 2: Context-Free Languages (CFL) and PDA

    Check whether the grammar is ambiguous:

    S→a∣abSb∣aAbS \to a \mid abSb \mid aAb
    A→bS∣aAAbA \to bS \mid aAAb

    View this question on its own page →
  33. Budget App•POCKETLY

    Pocketly on Android

    Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.

    Install Pocketly
  34. Q7b. Check whether the grammar: S \to aSa \mid bSb \mid a \mid b is ambiguous.20217m

    Module 2: Context-Free Languages (CFL) and PDA

    Check whether the grammar:

    S→aSa∣bSb∣a∣b S \to aSa \mid bSb \mid a \mid b

    is ambiguous.

    View this question on its own page →
  35. Q8a. Construct FOLLOW sets for the grammar: E \to T E' \\ E' \to +\,T\,E' \mid \varepsilon \\ T \to F T' \\ T' \to -\,F\,T' \mid \varepsilon \\ F \to (E) \mid id20227m

    Module 2: Context-Free Languages (CFL) and PDA

    Construct FOLLOW sets for the grammar:

    E→TE′E′→+ T E′∣εT→FT′T′→− F T′∣εF→(E)∣id E \to T E' \\ E' \to +\,T\,E' \mid \varepsilon \\ T \to F T' \\ T' \to -\,F\,T' \mid \varepsilon \\ F \to (E) \mid id

    View this question on its own page →
  36. Q8b. Given the regular expression r = (a+b)^* (i) Construct a context-free grammar that generates the language described by r. (ii) Explain why the grammar correctly represents the given regular expression.20257m

    Module 2: Context-Free Languages (CFL) and PDA

    Given the regular expression r=(a+b)∗r = (a+b)^*

    (i) Construct a context-free grammar that generates the language described by rr.
    (ii) Explain why the grammar correctly represents the given regular expression.

    View this question on its own page →
  37. Q8b. Construct a PDA accepting: L = \{ a^{2n}bc : n \ge 0 \}20217m

    Module 2: Context-Free Languages (CFL) and PDA

    Construct a PDA accepting:

    L={a2nbc:n≥0} L = \{ a^{2n}bc : n \ge 0 \}

    View this question on its own page →
  38. Q8b. Given the grammar: S \to aB \mid ba \\ B \to b \mid bs \mid aBB For the string aaabbabbba find: 1. Leftmost derivation 2. Rightmost derivation 3. Parse tree20237m

    Module 2: Context-Free Languages (CFL) and PDA

    Given the grammar:

    S→aB∣baB→b∣bs∣aBB S \to aB \mid ba \\ B \to b \mid bs \mid aBB

    For the string aaabbabbbaaaabbabbba find:

    1. Leftmost derivation
    2. Rightmost derivation
    3. Parse tree
    View this question on its own page →
  39. Q9b. Write a short note on Chomsky Normal Form.20213.5m

    Module 2: Context-Free Languages (CFL) and PDA

    Write a short note on Chomsky Normal Form.

    View this question on its own page →
  40. Q9b. Write a short note on Deterministic vs Nondeterministic PDA.20254.66m

    Module 2: Context-Free Languages (CFL) and PDA

    Write a short note on Deterministic vs Nondeterministic PDA.

    View this question on its own page →
  41. Q9b(i). Write a short note on Deterministic PDA vs. Non-deterministic PDA.20227m

    Module 2: Context-Free Languages (CFL) and PDA

    Write a short note on Deterministic PDA vs. Non-deterministic PDA.

    View this question on its own page →
  42. Q9b(ii). Write a short note on Chomsky Normal Form (CNF).20227m

    Module 2: Context-Free Languages (CFL) and PDA

    Write a short note on Chomsky Normal Form (CNF).

    View this question on its own page →
  43. Q9d. Write short notes on: Pumping lemma for CFL.20237m

    Module 2: Context-Free Languages (CFL) and PDA

    Write short notes on: Pumping lemma for CFL.

    View this question on its own page →
  44. Budget App•POCKETLY

    Pocketly on Android

    Log daily food, travel, and hostel expenses instantly. Set monthly spending limits and see where your money goes without complex spreadsheets.

    Install Pocketly
  45. Q9d. Write a short note on the Pumping Lemma for CFL.20213.5m

    Module 2: Context-Free Languages (CFL) and PDA

    Write a short note on the Pumping Lemma for CFL.

    View this question on its own page →
  46. Q9d. Write a short note on Greibach Normal Form (GNF).20254.66m

    Module 2: Context-Free Languages (CFL) and PDA

    Write a short note on Greibach Normal Form (GNF).

    View this question on its own page →