chapter
    Context-Free Grammars and Pushdown Automata PYQs for GATE CS

    Solve 10+ Context-Free Grammars and Pushdown Automata previous year questions for GATE CS with answers and detailed solutions. Free sample questions below.

    Try a question

    Answer it here to see how it works. Nothing is recorded until you sign in.

    Question 1
    2026 Slot Set2 PYQ
    Level 3: Exam Standard

    Which of the following grammars is/are ambiguous?

    Question 2
    2026 Slot Set1 PYQ
    Consider the following context-free grammar .




    In the above grammar, is the start symbol, and are terminal symbols, and and are non-terminal symbols.

    Let be the language generated by the grammar . For a string , let be the number of ’s in and be the number of ’s in .

    Which of the following statements is/are true?
    Question 3
    2026 Slot Set1 PYQ
    Level 3: Exam Standard
    Consider the following grammar where is the start symbol, and and are terminal symbols.


    Which of the following statements is/are true?
    Question 4
    2025 Slot Set2 PYQ

    Which ONE of the following languages is accepted by a deterministic pushdown automaton?

    Question 5
    2025 Slot Set1 PYQ
    Consider the following context-free grammar , where , , and are the variables (non-terminals), and are the terminal symbols, is the start variable, and the rules of are described as:


    Which ONE of the languages is accepted by ?
    Question 6
    2024 Slot Set2 PYQ

    Consider a context-free grammar with the following 3 rules.

    Let . Let , , denote the number of times occur in , respectively. Which of the following statements is/are TRUE?

    Question 7
    2024 Slot Set1 PYQ
    Level 3: Exam Standard

    Let be a context-free grammar in Chomsky Normal Form with and containing 10 variable symbols including the start symbol . The string is derivable from . The number of steps (application of rules) in the derivation is _________

    Question 8
    2023 PYQ
    Consider the pushdown automaton (PDA) below, which runs on the input alphabet , has stack alphabet , and has three states , with being the start state. A transition from state to state , labelled , where is an input symbol or , is a stack symbol, and is a string of stack symbols, represents the fact that in state , the PDA can read from the input, with on the top of its stack, pop from the stack, push in the string on the stack, and go to state . In the initial configuration, the stack has only the symbol in it. The PDA accepts by empty stack.

    spqa/⊥/A⊥a/A/AAb/A/εb/A/εε/A/εε/A/εε/A/εε/⊥/ε

    Which one of the following options correctly describes the language accepted by ?
    Question 9
    2023 PYQ
    Level 3: Exam Standard
    Consider the context-free grammar below


    where and are non-terminals, and and are terminal symbols. The starting non-terminal is .

    Which one of the following statements is CORRECT?
    Question 10
    2021 Slot Set1 PYQ
    Level 3: Exam Standard
    In a pushdown automaton , a transition of the form,

    p q a, X → Y
    where , , and , represents


    Consider the following pushdown automaton over the input alphabet and stack alphabet .

    start q₀ q₁ q₂ q₃ ε, ε → # ε, ε → ε ε, A → A a, ε → A b, A → ε
    The number of strings of length 100 accepted by the above pushdown automaton is __________.
    Free preview ends here

    Login to view the complete previous-year questions and solutions

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Context-Free Grammars and Pushdown Automata PYQs for GATE CS

    Solve 10+ Context-Free Grammars and Pushdown Automata previous year questions for GATE CS with answers and detailed solutions. Free sample questions below.

    Chapter Roadmap: Context-Free Grammars and Pushdown Automata

    Chapter Journey

    1. CFG Language Generation and Symbol-Count Properties
    Master deriving strings, tracking terminal counts, and finding invariants in production rules.
    High Priority
    2. Chomsky Normal Form and Derivation Length
    Standardize grammars to bound derivation steps and prove properties about string length.
    Support Concept
    3. Grammar Ambiguity and Multiple Derivations
    Identify when a single string has multiple leftmost derivations or parse trees.
    Moderate Priority
    4. Pushdown Automata, Transitions and Accepted Languages
    Design stack-based machines to recognize context-free languages and match them to grammars.
    High Priority

    CFG Language Generation and Symbol-Count Properties

    Context-Free Grammars and Pushdown Automata › Topic 1

    CFG Language Generation and Symbol-Count Properties

    Learn to read a grammar as a system of mathematical equations to predict exact symbol counts and language boundaries.

    Derivation Trees Symbol Invariants Recursive Unrolling
    • Translate production rules into algebraic equations for terminal counts.
    • Identify global invariants that hold for all generated strings.
    • Unroll recursive rules to determine the exact structure of the generated language.
    • Avoid the hidden dependency trap when analyzing complex, multi-variable grammars.

    Context-Free Grammars and Pushdown Automata: Solved Questions with Step-by-Step Explanations (10 Problems)

    Question 1 · Theory of Computation · 2026_Set2 MSQ

    Which of the following grammars is/are ambiguous?

    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    ["B","C"]

    Step-by-Step Solution

    Key idea: This is a grammar ambiguity identification question. We need to check each grammar to see if any string has multiple distinct derivations.

    Step 1: Check Grammar A: S → aSb | ε

    This generates {a^n b^n | n ≥ 0}.

    For any string a^n b^n, there's only ONE way to derive it:

    • Must apply S → aSb exactly n times
    • Then apply S → ε once
    • Order is forced (always leftmost or always rightmost)

    Grammar A is UNAMBIGUOUS.

    Step 2: Check Grammar B: E → E+E | E*E | id

    This is the classic expression grammar.

    For string "id+id*id":

    Derivation 1: E ⇒ E+E ⇒ id+E ⇒ id+EE ⇒ id+idE ⇒ id+idid (groups as id + (id id))

    Derivation 2: E ⇒ EE ⇒ E+EE ⇒ id+EE ⇒ id+idE ⇒ id+idid (groups as (id + id) id)

    Two different parse trees ⇒ Grammar B is AMBIGUOUS.

    Step 3: Check Grammar C: S → aS | Sa | ε

    For string "aa":

    Derivation 1: S ⇒ aS ⇒ aaS ⇒ aa (using S → aS twice, then S → ε)

    Derivation 2: S ⇒ Sa ⇒ aSa ⇒ aa (using S → Sa, then S → aS for the first S, then S → ε)

    These are two distinct leftmost derivations for "aa".

    Grammar C is AMBIGUOUS.

    Step 4: Check Grammar D: S → aS | ε

    This generates a* (any number of a's).

    For string "aaa", there is only one leftmost derivation:

    S ⇒ aS ⇒ aaS ⇒ aaaS ⇒ aaa

    Grammar D is UNAMBIGUOUS.

    Answer: Grammars B and C are ambiguous.

    Question 2 · Theory of Computation · 2026_Set1 MSQ
    Consider the following context-free grammar .




    In the above grammar, is the start symbol, and are terminal symbols, and and are non-terminal symbols.

    Let be the language generated by the grammar . For a string , let be the number of ’s in and be the number of ’s in .

    Which of the following statements is/are true?
    1. A.

      There is a string such that

    2. B.

      For every string ,

    3. C.

      There is a string such that

    4. D.

      For every string ,

    Question 3 · Theory of Computation · 2026_Set1 MSQ
    Consider the following grammar where is the start symbol, and and are terminal symbols.


    Which of the following statements is/are true?
    1. A.

      The grammar is ambiguous

    2. B.

      The string has two distinct derivations in this grammar

    3. C.

      The string has only one rightmost derivation

    4. D.

      The language generated by the grammar is undecidable

    Correct Answer:

    ["A","B"]

    Step-by-Step Solution

    Key idea: This is a grammar ambiguity question. To prove a grammar is ambiguous, we must find at least one string with multiple distinct leftmost derivations.

    Step 1: Check string "abb".

    Derivation 1 (Leftmost):

    S ⇒ aSbS

    ⇒ abSbS (using S → bS for the first S)

    ⇒ abbS (using S → ε for the second S)

    ⇒ abb (using S → ε for the third S)

    Derivation 2 (Leftmost):

    S ⇒ aSbS

    ⇒ abS (using S → ε for the first S)

    ⇒ abbS (using S → bS for the remaining S)

    ⇒ abb (using S → ε for the remaining S)

    These are two distinct leftmost derivations for "abb", so the grammar is ambiguous.

    Step 2: Evaluate options.

    A: True (proven above).

    B: True (proven above).

    C: False. The string "abab" has multiple rightmost derivations. For example:

    RMD 1: S ⇒ aSbS ⇒ aSbaSbS ⇒ aSbaSb ⇒ aSbab ⇒ abab

    RMD 2: S ⇒ aSbS ⇒ aSb ⇒ a(aSbS)b ⇒ a(aSb)b ⇒ a(ab)b ⇒ abab

    D: False. All context-free languages are decidable (e.g., via CYK algorithm).

    Answer: Options A and B are true.

    Question 4 · Theory of Computation · 2025_Set2 MCQ

    Which ONE of the following languages is accepted by a deterministic pushdown automaton?

    1. A.

      Any regular language.

    2. B.

      Any context-free language.

    3. C.

      Any language accepted by a non-deterministic pushdown automaton.

    4. D.

      Any decidable language.

    Question 5 · Theory of Computation · 2025_Set1 MCQ
    Consider the following context-free grammar , where , , and are the variables (non-terminals), and are the terminal symbols, is the start variable, and the rules of are described as:


    Which ONE of the languages is accepted by ?
    1. A.

    2. B.

    3. C.

    4. D.

    Question 6 · Theory of Computation · 2024_Set2 MSQ

    Consider a context-free grammar with the following 3 rules.

    Let . Let , , denote the number of times occur in , respectively. Which of the following statements is/are TRUE?

    1. A.

    2. B.

    3. C.

    4. D.

    Question 7 · Theory of Computation · 2024_Set1 NAT

    Let be a context-free grammar in Chomsky Normal Form with and containing 10 variable symbols including the start symbol . The string is derivable from . The number of steps (application of rules) in the derivation is _________

    Correct Answer:

    179

    Step-by-Step Solution

    Key idea: This is a Chomsky Normal Form derivation length question. In CNF, there's a direct formula relating string length to the number of derivation steps.

    Step 1: Recall the CNF derivation length theorem.

    For a grammar in Chomsky Normal Form (CNF):

    • If a string w has length n (where n ≥ 1)
    • Then any derivation of w requires exactly 2n - 1 steps

    Step 2: Calculate the length of the given string.

    w = a^30 b^30 c^30

    Length n = 30 + 30 + 30 = 90

    Step 3: Apply the formula.

    Number of steps = 2n - 1

    Number of steps = 2(90) - 1

    Number of steps = 180 - 1 = 179

    Step 4: Verify understanding.

    Why does this formula work?

    • In CNF, each production is either A → BC (2 non-terminals) or A → a (1 terminal)
    • To generate n terminals, we need:
    • (n - 1) productions of type A → BC to build the structure
    • n productions of type A → a to generate terminals
    • Total: (n - 1) + n = 2n - 1 steps

    Note: The number of variables (10) is irrelevant - it's a distractor!

    Answer: 179

    Question 8 · Theory of Computation · 2023 MCQ
    Consider the pushdown automaton (PDA) below, which runs on the input alphabet , has stack alphabet , and has three states , with being the start state. A transition from state to state , labelled , where is an input symbol or , is a stack symbol, and is a string of stack symbols, represents the fact that in state , the PDA can read from the input, with on the top of its stack, pop from the stack, push in the string on the stack, and go to state . In the initial configuration, the stack has only the symbol in it. The PDA accepts by empty stack.

    spqa/⊥/A⊥a/A/AAb/A/εb/A/εε/A/εε/A/εε/A/εε/⊥/ε

    Which one of the following options correctly describes the language accepted by ?
    1. A.

      \{a^mb^n\mid 1\leq m\text{ and }n<m\}

    2. B.

    3. C.

    4. D.

    Question 9 · Theory of Computation · 2023 MCQ
    Consider the context-free grammar below


    where and are non-terminals, and and are terminal symbols. The starting non-terminal is .

    Which one of the following statements is CORRECT?
    1. A.

      The language generated by is

    2. B.

      The language generated by is

    3. C.

      The language generated by is

    4. D.

      The language generated by is not a regular language

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a CFG language identification question. We need to analyze what strings the grammar can generate by understanding the role of each non-terminal.

    Step 1: Analyze the grammar structure.

    Grammar:

    S → aSb | X

    X → aX | Xb | a | b

    Step 2: Understand what X generates.

    X can produce 'a' or 'b' directly.

    Recursive rules X → aX and X → Xb allow adding 'a' to the left or 'b' to the right.

    This means X can never generate a 'b' followed by an 'a'.

    Thus, L(X) = { a^i b^j | i + j >= 1 } = ab - {ε}.

    Step 3: Understand what S generates.

    S → X allows S to produce anything X produces.

    S → aSb wraps matching pairs of 'a' and 'b' around whatever S produces.

    So S generates a^n w b^n, where w ∈ L(X) and n >= 0.

    Let w = a^i b^j (with i + j >= 1).

    Then the string is a^n a^i b^j b^n = a^{n+i} b^{j+n}.

    Let N = n + i and M = j + n.

    Since i + j >= 1, we have N + M = 2n + i + j >= 1.

    Also, N >= 0 and M >= 0.

    Thus, S generates exactly { a^N b^M | N + M >= 1 } = ab - {ε}.

    Step 4: Match with options.

    Option A: (a+b)* includes "ba" and ε, which are not in L(S).

    Option B: a(a+b)b generates strings with zero or more 'a's, exactly one 'a' or 'b', and zero or more 'b's. This is exactly (a^+ b) ∪ (a b^+) = ab - {ε}. This matches L(S).

    Option C: ab(a+b) can generate "ba" (e.g., a^0 b^1 a), which is not in L(S).

    Option D: The language is regular, so this is false.

    Answer: Option B is correct.

    Question 10 · Theory of Computation · 2021_Set1 NAT
    In a pushdown automaton , a transition of the form,

    p q a, X → Y
    where , , and , represents


    Consider the following pushdown automaton over the input alphabet and stack alphabet .

    start q₀ q₁ q₂ q₃ ε, ε → # ε, ε → ε ε, A → A a, ε → A b, A → ε
    The number of strings of length 100 accepted by the above pushdown automaton is __________.
    Correct Answer:

    50

    Step-by-Step Solution

    Key idea: This is a PDA language analysis question. We need to trace the PDA transitions to understand the accepted language, then count valid strings of length 100.

    Step 1: Analyze transitions.

    • q0 → q1: ε, ε → # (pushes bottom marker)
    • q1 → q1: a, ε → A (pushes A for each 'a')
    • q1 → q2: ε, ε → ε (moves to popping state)
    • q2 → q2: b, A → ε (pops A for each 'b')
    • q2 → q3: ε, A → A (moves to final state if A is on top)

    Step 2: Determine accepted language.

    To reach the final state q3, the PDA must be in q2 with 'A' on top of the stack.

    This means the number of 'A's pushed (n) must be strictly greater than the number of 'A's popped (m).

    So, n > m.

    The input string must be of the form a^n b^m, where n > m >= 0.

    Step 3: Count strings of length 100.

    We need n + m = 100 and n > m >= 0.

    Substitute m = 100 - n:

    n > 100 - n => 2n > 100 => n > 50.

    Since n <= 100 (because m >= 0), the possible values for n are 51, 52, ..., 100.

    The number of such values is 100 - 51 + 1 = 50.

    Answer: 50

    More previous year questions (pyqs) in this unit