chapter

    Context-free Grammars and Pushdown Automata Notes for Gate Cs

    Context-free Grammars and Pushdown Automata notes for GATE CS: 35 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice ques

    Context Free Grammars and Pushdown Automata notes

    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.

    The Grammar as a Counting Machine

    The Grammar as a Counting Machine

    A context-free grammar can be analyzed algebraically by tracking the net change in terminal counts for each production rule.

    Let , , and denote the number of occurrences of terminals , , and in a derived string .

    For any production rule, you can write a count equation that relates the counts of the left-hand side to the right-hand side.

    Example:

    Consider the rule . The count equations for this single step are:

    • The non-terminal will later expand, but this specific rule contributes exactly one and one to the final string.

    By summing these contributions across the entire derivation tree, you can find global invariants that hold for every string in the language.

    32 more cards in this chapter

    Try a question

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

    Question 1
    Level 1: Warm-up

    Consider a grammar where the only rules are and . What is the minimum number of applications of the recursive rule required to generate a string of length 4?

    Question 2
    Level 1: Warm-up

    Consider the grammar . Every time the rule is applied, it introduces one and one , and splits the derivation into two branches. If a string in this language has exactly 3 occurrences of , which of the following statements is TRUE regarding the number of times the base rule must have been applied?

    Question 3
    Level 1: Warm-up

    A Pushdown Automaton has the transition . What operation does this transition perform on the stack?

    Question 4
    Level 1: Warm-up

    A PDA has both and defined for the same state and stack top . This PDA is:

    Question 5
    Level 1: Warm-up

    Let be a context-free grammar strictly in Chomsky Normal Form. If a string of length 15 is derivable from the start symbol, what is the exact number of production rule applications required to derive ?

    Question 6
    Level 1: Warm-up

    A PDA transition is given as where . What is the net effect of this transition on the stack?

    Question 7
    Level 1: Warm-up

    A PDA has two distinct transitions and for the same state , input symbol , and stack top . What is the minimum number of these transitions that must be removed to satisfy the DPDA single-move constraint?

    Question 8
    Level 1: Warm-up
    Let be a pushdown automaton with transition function Which of the following is/are possible in a single transition of ?
    Question 9
    Level 1: Warm-up

    In the standard hierarchy of formal languages, Context-Free Grammars (CFGs) are used to generate context-free languages. Which of the following machine models is exactly equivalent in power to CFGs for recognizing these languages?

    Question 10
    Level 1: Warm-up

    A context-free grammar has the production rule . When this rule is applied, it replaces one non-terminal with the sequence . What is the maximum number of terminals added to the sentential form in a single application of this rule?

    Free preview ends here

    Login to view the complete notes

    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.

    Explore complete test series, official past papers, and chapter-wise study tools.

    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 Notes for GATE CS

    Context-free Grammars and Pushdown Automata notes for GATE CS: 35 study cards covering concepts, formulas, shortcuts and exam traps, plus solved practice questions.

    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.

    The Grammar as a Counting Machine

    The Grammar as a Counting Machine

    A context-free grammar can be analyzed algebraically by tracking the net change in terminal counts for each production rule.

    Let , , and denote the number of occurrences of terminals , , and in a derived string .

    For any production rule, you can write a count equation that relates the counts of the left-hand side to the right-hand side.

    Example:

    Consider the rule . The count equations for this single step are:

    • The non-terminal will later expand, but this specific rule contributes exactly one and one to the final string.

    By summing these contributions across the entire derivation tree, you can find global invariants that hold for every string in the language.

    Method: Tracking Symbol Counts via Equations

    Method: Tracking Symbol Counts

    1

    Identify Base Cases

    Find the rules that terminate the derivation (produce only terminals). Calculate the exact terminal counts for these base strings.

    2

    Analyze Recursive Steps

    For each recursive rule, determine the net change in terminal counts. Does the rule add one and one ? Does it add two 's for every one ?

    3

    Formulate the Invariant

    Combine the base case counts with the net changes from the recursive steps. Look for a linear relationship (e.g., ) that holds true after any number of derivations.

    4

    Verify with Edge Cases

    Test your invariant against the shortest possible strings and strings with maximum recursion to ensure the relationship never breaks.

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

    Question 1 · Theory of Computation MCQ

    Consider a grammar where the only rules are and . What is the minimum number of applications of the recursive rule required to generate a string of length 4?

    1. A.

      1

    2. B.

      2

    3. C.

      3

    4. D.

      4

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a minimum calculation question based on the length contribution of recursive rules.

    Step 1: Analyze the base rule . It adds 0 to the length of the string.

    Step 2: Analyze the recursive rule . Each application adds exactly 2 terminals ( and ) to the string.

    Step 3: We want a final string of length 4.

    Step 4: Since the base rule adds 0, the entire length of 4 must come from the recursive rule.

    Step 5: Divide the target length by the length added per recursive step: .

    Step 6: The minimum number of applications of the recursive rule is 2.

    Answer: 2.

    Question 2 · Theory of Computation MCQ

    Consider the grammar . Every time the rule is applied, it introduces one and one , and splits the derivation into two branches. If a string in this language has exactly 3 occurrences of , which of the following statements is TRUE regarding the number of times the base rule must have been applied?

    1. A.

      The base rule is applied exactly 3 times.

    2. B.

      The base rule is applied exactly 4 times.

    3. C.

      The base rule is applied exactly 5 times.

    4. D.

      The number of base rule applications cannot be determined from the given information.

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a bounding question about branching derivations. We must track the net change in the number of non-terminals (specifically ) to find how many base cases are required to terminate the derivation.

    Step 1: Start with 1 instance of the non-terminal .

    Step 2: Analyze the branching rule . It consumes 1 and produces 2 's. The net change in the number of 's is . This rule also produces exactly 1 .

    Step 3: Analyze the base rule . It consumes 1 and produces 0 's. The net change in the number of 's is .

    Step 4: Let be the number of times the branching rule is applied, and be the number of times the base rule is applied.

    Step 5: The total number of 's produced is exactly . We are given there are 3 's, so .

    Step 6: To terminate the derivation, we must end up with 0 instances of . The equation for the final count of is: .

    Step 7: Substitute : .

    Step 8: The base rule must be applied exactly 4 times.

    Answer: The base rule is applied exactly 4 times.

    Question 3 · Theory of Computation MCQ

    A Pushdown Automaton has the transition . What operation does this transition perform on the stack?

    1. A.

      Pop from the stack

    2. B.

      Push on top of

    3. C.

      Replace with

    4. D.

      Read without modifying the stack

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: The transition means: in state , reading input , with on top of the stack, move to state , pop , and push the string .

    Step 1: Identify in the given transition. Here, .

    Step 2: Understand what pushing means. When we pop and then push , the symbol is pushed first (going deeper), then is pushed on top. So ends up on top of where was.

    Step 3: The net effect is: is popped, then and are pushed back, with now on top. This is equivalent to pushing on top of the existing .

    Answer: B

    Common mistake: Students might think "replace with " (option C), but that would be , not . The key is that means both symbols are pushed, with on top.

    Question 4 · Theory of Computation MCQ

    A PDA has both and defined for the same state and stack top . This PDA is:

    1. A.

      Deterministic

    2. B.

      Non-deterministic

    3. C.

      Cannot be determined from this information

    4. D.

      Invalid PDA

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: A DPDA cannot have both an input transition and an -transition for the same state and stack top.

    Step 1: Recall the DPDA constraints. For a PDA to be deterministic: (1) at most one transition for each , and (2) if is defined for , then must be empty.

    Step 2: Check the given PDA. Here, both and are defined. This violates constraint (2).

    Step 3: Conclude. The PDA has a choice between reading or taking an -move, so it is non-deterministic.

    Answer: B

    Common mistake: Students might think "it's deterministic because each transition is well-defined" (option A), but determinism requires no choice between input and -moves.

    Question 5 · Theory of Computation NAT

    Let be a context-free grammar strictly in Chomsky Normal Form. If a string of length 15 is derivable from the start symbol, what is the exact number of production rule applications required to derive ?

    Correct Answer:

    29.00

    Step-by-Step Solution

    Key idea: In Chomsky Normal Form (CNF), the number of derivation steps for a string of length is strictly determined by a fixed formula.

    Step 1: Verify the grammar is in CNF (given in the problem).

    Step 2: Identify the length of the target string, .

    Step 3: Apply the CNF derivation length formula: .

    Step 4: Calculate: .

    Answer: 29.00

    Question 6 · Theory of Computation MCQ

    A PDA transition is given as where . What is the net effect of this transition on the stack?

    1. A.

      Push a new symbol on top of

    2. B.

      Pop from the stack

    3. C.

      Replace with a different symbol

    4. D.

      No change to the stack

    Correct Answer:

    D

    Step-by-Step Solution

    Key idea: This is a transition-decoding question. The second component of the output tuple tells you what gets pushed after popping the top.

    Step 1: Recall the transition semantics. means: pop , then push the string .

    Step 2: Identify in the given transition. Here , the same symbol that was popped.

    Step 3: Determine the net effect. Popping and immediately pushing back leaves the stack unchanged. This is the read-only operation.

    Answer: D

    Common trap: Students sometimes confuse (read-only) with where (replace). The key distinction is whether the pushed symbol equals the popped symbol.

    Verification: If the stack was before the transition, after popping it becomes , and after pushing back it becomes again — identical to the original.

    Question 7 · Theory of Computation MCQ

    A PDA has two distinct transitions and for the same state , input symbol , and stack top . What is the minimum number of these transitions that must be removed to satisfy the DPDA single-move constraint?

    1. A.

      0

    2. B.

      1

    3. C.

      2

    4. D.

      The PDA cannot be made deterministic by removing transitions

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a DPDA constraint question. The single-move constraint requires at most one transition for each triple.

    Step 1: Recall the single-move constraint. For a DPDA, for every state , input , and stack top . At most one transition is allowed.

    Step 2: Count current transitions. There are 2 transitions for the same : one going to and one going to .

    Step 3: Determine minimum removals. To satisfy , we need at most 1 transition. Currently there are 2, so we must remove at least .

    Answer: B

    Common trap: Students might think both transitions must be removed (option C) because the PDA is "too non-deterministic." But keeping one valid transition is perfectly fine for a DPDA.

    Verification: After removing one transition, . The single-move constraint is satisfied.

    Question 8 · Theory of Computation MSQ
    Let be a pushdown automaton with transition function Which of the following is/are possible in a single transition of ?
    1. A.

      Popping two symbols from the stack.

    2. B.

      Consuming no input symbol.

    3. C.

      Testing the top two symbols of the stack.

    4. D.

      Pushing two stack symbols in place of the symbol popped.

    Correct Answer:

    ["B","D"]

    Step-by-Step Solution

    Insight: the signature of fixes exactly what one move can see and do — one input symbol (or ), one stack top, and any string pushed.

    Exam route: read the arguments of : the third argument is a single symbol of , so only the top is inspected and only that one symbol is removed. The replacement string lies in , so it may be empty, one symbol, or several. The second argument may be .

    Learning route, step by step:

    1. A move is with . The machine names one stack symbol , so it can only test the top. Option C is impossible.
    2. Executing the move pops that single and pushes . Since only is removed, two symbols cannot be popped in one move. Option A is impossible.
    3. The input argument ranges over . Choosing makes the move consume nothing, so option B is possible.
    4. The pushed string ranges over . Taking removes one symbol and puts two in its place, so option D is possible; the stack height rises by one.
    5. Summarising the height arithmetic: pops, replaces, pushes one extra. In all cases exactly one symbol is removed.

    Tempting wrong path: think of the stack as a general memory and allow "pop two" or "look at two", selecting options A and C. The break is at the type of the third argument of : it is a single element of , not a string, so no second symbol is ever visible.

    Generalization: one PDA move sees one input symbol and one stack symbol, and rewrites that stack symbol by an arbitrary finite string.

    Verification: is legal by the signature and changes the stack from to , confirming B and D while leaving the top-only restriction intact.

    Answer: B and D.

    Question 9 · Theory of Computation MCQ

    In the standard hierarchy of formal languages, Context-Free Grammars (CFGs) are used to generate context-free languages. Which of the following machine models is exactly equivalent in power to CFGs for recognizing these languages?

    1. A.

      Deterministic Finite Automata

    2. B.

      Pushdown Automata

    3. C.

      Turing Machines

    4. D.

      Linear Bounded Automata

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a direct recall question about the Chomsky hierarchy and the equivalence between grammars and automata.

    Step 1: Recall that Regular Grammars correspond to Finite Automata.

    Step 2: Recall that Context-Free Grammars (CFGs) correspond to Pushdown Automata (PDAs), which augment finite automata with a stack.

    Step 3: Recall that Turing Machines correspond to Recursively Enumerable languages (Type 0), and Linear Bounded Automata correspond to Context-Sensitive languages (Type 1).

    Step 4: Match CFGs with Pushdown Automata.

    Answer: Pushdown Automata.

    Question 10 · Theory of Computation MCQ

    A context-free grammar has the production rule . When this rule is applied, it replaces one non-terminal with the sequence . What is the maximum number of terminals added to the sentential form in a single application of this rule?

    1. A.

      0

    2. B.

      1

    3. C.

      2

    4. D.

      3

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is an observation question about the exact symbols generated by a rule. We must distinguish between terminals and non-terminals.

    Step 1: Examine the right-hand side of the rule: .

    Step 2: Identify the terminals. Assuming lowercase letters are terminals and uppercase are non-terminals, the terminals are and .

    Step 3: Count the terminals. There is one and one , making a total of 2 terminals.

    Step 4: The symbols and are non-terminals and do not count towards the terminal addition.

    Step 5: The maximum number of terminals added in this single application is 2.

    Answer: 2.

    More notes in this unit