IAE Academy

GATE CS

Context-free grammars and push-down automata

A context-free grammar generates strings by rewriting variables, and a push-down automaton (a finite automaton with a stack) accepts exactly the same languages. These notes cover derivations and parse trees, ambiguity, simplification and normal forms, the step count in Chomsky normal form, designing and tracing a PDA, conversions between grammars and PDAs, and which languages need non-determinism.

9 previous-year questions · 8 solved · 5 practice questions

Context-free grammars and derivations

A context-free grammar (CFG) is a 4-tuple : is the finite set of variables (non-terminals), the terminals (the input alphabet), the productions and the start variable. Every production has the form with and , so the left side is always a single variable. Convention: is the empty string, is the length of , and .

A derivation step replaces one variable using one production . In a leftmost derivation the leftmost variable is replaced at every step; in a rightmost derivation the rightmost one. A string of variables and terminals reached from is a sentential form; if it has only terminals it is a sentence of .

Grammar

Language generated

(even-length palindromes)

all palindromes over

balanced parentheses

Finding the language of a grammar

  1. List the shortest strings derivable and look for the pattern.

  2. Name what each variable generates on its own (for example gives ).

  3. Combine the pieces following the right-hand sides, then test the guess on two or three strings.

  4. Check the boundary case: whether is generated.

EXAM TIP

A language is context-free if some CFG generates it. Every regular language is context-free, but not conversely: is context-free and not regular. A grammar in which every production has the form or (with ) is right-linear and generates a regular language; for example generates .

Parse trees and ambiguity

A parse tree has the start variable at the root, each internal node labelled by a variable whose children are the symbols on the right side of the production used, and leaves labelled by terminals, or by when is the only child (the production ). The yield is the leaves read left to right. A terminal string is the yield of some parse tree with root exactly when .

Many derivations can give the same tree (they differ only in the order of replacement), but each parse tree has exactly one leftmost derivation and exactly one rightmost derivation. So for a string : number of parse trees = number of leftmost derivations = number of rightmost derivations.

A grammar is ambiguous if some string in has two or more different parse trees (equivalently two leftmost derivations, or two rightmost derivations). To show a grammar ambiguous, exhibit one string with two different parse trees. A language is inherently ambiguous if every grammar generating it is ambiguous, for example .

WORKED EXAMPLE

Show that is ambiguous and count the parse trees of .

Solution
  1. The only productions are and , so every tree for has the root .

  2. Tree 1: the left child splits again, giving : .

  3. Tree 2: the right child splits again, giving : .

  4. There are no others: the root's two children must yield and , or and , and has exactly one tree (, each child ).

  5. The two trees differ, so has two leftmost derivations and the grammar is ambiguous.

Answer: Ambiguous; has exactly 2 parse trees.

EXAM TIP

Ambiguity is a property of a grammar; a language is inherently ambiguous only when every grammar for it is ambiguous. Rewriting a grammar can often remove ambiguity: generates the same language as the unambiguous , and the dangling-else grammar can be rewritten to fix which if each else belongs to. No algorithm decides whether an arbitrary CFG is ambiguous.

Simplifying grammars

Simplification removes -productions, unit productions () and useless symbols without changing the language, except that is handled separately. Apply the steps in this order: -productions, then unit productions, then useless symbols (non-generating first, then unreachable). Removing -productions can create unit productions and useless symbols, and removing unit productions can leave useless symbols, so another order can leave work undone.

Procedure

  1. Nullable variables: is nullable if . Start with variables having ; add if some has every already nullable.

  2. Remove -productions: replace each production by every version obtained by deleting any subset of the nullable symbols in ; drop every and never create one. The new grammar generates ; if , add a new start variable with .

  3. Unit productions : find every unit pair with using unit productions only (including and pairs reached through a chain); for each pair, give every non-unit production of ; then delete all unit productions.

  4. Non-generating variables: keep those that can derive a terminal string (start with variables having a terminal-only right side and grow). Delete the others and every production using them.

  5. Unreachable variables: keep those reachable from in the dependency graph. Delete the others. Do this after the generating test.

WORKED EXAMPLE

Simplify , , , , .

Solution
  1. Nullable: only . Remove -productions: and .

  2. Unit pairs (besides ): , , , , , . Replace: , , , .

  3. Generating variables: (through ), (through ), , . is non-generating (it only produces ), so remove it and every rule using it: , , , .

  4. Reachable from : and only; and are unreachable and are removed.

Answer: , , generating .

Normal forms (CNF and GNF)

Chomsky normal form (CNF): every production is (two variables) or (one terminal), with the extra rule allowed only when and never appears on a right side. For every CFL , the language has a CNF grammar.

Converting to CNF

  1. Simplify first: remove -productions, unit productions and useless symbols.

  2. For each terminal in a right side of length 2 or more, add a variable and use instead of .

  3. Break right sides longer than 2: becomes , , , .

DERIVATION LENGTH IN CHOMSKY NORMAL FORM

For and any derivation of from in a CNF grammar: steps use and steps use . In the parse tree there are variable nodes with two variable children and variable nodes whose only child is a terminal leaf, so variable nodes, one per step.

Greibach normal form (GNF): every production is with a terminal and (possibly empty). For every CFL , the language has a GNF grammar. Every step adds exactly one terminal, so any derivation of a string of length takes exactly steps. From a GNF grammar one can build a one-state PDA with no -moves: start with on the stack and, for each , add ; it accepts by empty stack.

DERIVATION LENGTH IN GREIBACH NORMAL FORM

For ; each step adds exactly one terminal.

EXAM TIP

Check the conditions before using a count: needs a CNF grammar and . In an arbitrary grammar the length of a derivation of a string of length is not fixed.

Push-down automata

A push-down automaton (PDA) is a finite automaton with an unbounded stack. Formally : states , input alphabet , stack alphabet , start state , initial stack symbol and accepting states . The transition function is finite subsets of . A move reads (or nothing when ), pops , pushes the string (leftmost symbol on top) and goes to . A configuration (instantaneous description) is written : current state, input still unread, stack contents with the top on the left; the move above takes to .

Acceptance mode

Input is accepted if

Final state

after reading all of some computation can be in a state of (stack contents do not matter)

Empty stack

after reading all of some computation can have an empty stack (the set is not used)

For non-deterministic PDAs the two modes accept exactly the same class of languages, and each can be converted to the other. A PDA is deterministic (DPDA) if for each there is at most one move, and if is non-empty then is empty for every . A DPDA may still have -moves; the rule only forbids a choice between an -move and an input move. Convention in these notes: a missing transition means that computation halts and rejects.

State

Input

Top of stack

Move

→

stay in , replace by (push )

stay in , replace by (push )

go to , pop

stay in , pop

go to , leave

—

—

no moves (accepting)

The table is a DPDA for by final state: push one per , pop one per , and accept when the stack is back to . Traces: ends in (accepted); ends in with (rejected); reaches with a unread and halts (rejected); stays in (rejected). For the machine pushes , then must guess the middle and pop while matching . That guess is the non-determinism.

Designing a PDA

  1. Decide what the stack should count or remember (a count of 's, or the first half of the string).

  2. Use the finite control for the phase of the input (reading 's, then 's).

  3. Choose the acceptance mode (empty stack is often the simplest for matching).

  4. Where the middle or the phase change cannot be known in advance, use a non-deterministic -move.

  5. Trace the shortest accepted string, one string that is rejected for the wrong count, and .

EXAM TIP

and are DCFLs. is not context-free (proved with the pumping lemma for CFLs); the intuition is that one stack cannot compare one count against two others. A PDA with two stacks can simulate a Turing machine, and a PDA whose stack height is bounded by a constant accepts only regular languages.

CFGs, PDAs and determinism

EQUIVALENCE OF CFGS AND PDAS

Every CFG has an equivalent NPDA and every NPDA has an equivalent CFG, with either acceptance mode.

CFG to PDA (empty stack)

  1. Use one state , stack alphabet , and the start variable as the initial stack symbol.

  2. For each production add the move : pop the variable and push its right side.

  3. For each terminal add : match the input against the stack top and pop.

  4. The PDA simulates a leftmost derivation: it accepts by empty stack exactly when .

PDA to CFG: start from a PDA that accepts by empty stack and pushes at most two symbols per move (any PDA can be converted to such a form). Use variables , meaning 'starting in with on top, the machine can reach having popped and consumed exactly the string generated'. The start variable has for every state . For a move with input : gives ; gives for every state ; gives for all states . The grammar has at most variables.

DPDA

NPDA

Languages accepted

deterministic CFLs (DCFLs)

all CFLs

Final state vs empty stack

final state is more powerful: empty-stack DPDAs accept exactly the DCFLs with the prefix property (no string of the language is a proper prefix of another)

same power

Examples

, , (all DCFLs)

, palindromes, (CFLs that are not DCFLs)

Closed under complement

yes

no

Closed under union, intersection

no for both

union yes, intersection no

Closed under concatenation, Kleene star

no for both

yes for both

Closed under reversal, homomorphism

no for both

yes for both

Closed under intersection with a regular language

yes

yes

Question about CFGs

Decidable?

Membership: is ?

yes (for example the CYK algorithm on a CNF grammar, time for a fixed grammar)

Emptiness: is ?

yes (test whether is generating)

Finiteness: is finite?

yes

Is ambiguous?

no

Is ?

no

Is ?

no

Is ?

no

Is regular?

no

EXAM TIP

Regular DCFL CFL, both inclusions proper. and the palindromes are context-free but not deterministic. is context-free but not deterministic, because it is inherently ambiguous and every DCFL has an unambiguous grammar. The converse fails: a language with an unambiguous grammar need not be a DCFL ( has the unambiguous grammar ).

PREVIOUS-YEAR QUESTIONS

9 questions from the CS papers

Try each question first, then open the answer. We placed each question under a topic ourselves; the exam paper does not label topics.

See these papers in full: GATE CSE 2026 Set 1 · GATE CSE 2026 Set 2 · GATE CSE 2025 Set 1 · GATE CSE 2025 Set 2 · GATE CSE 2024 Set 1 · GATE CSE 2024 Set 2 · GATE CSE 2023

GATE 2026 CS1 Q.25

GATE 2026 · CS1Q.25 · 1 mark · 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

Answer and solution
See the question in the paper
GATE 2026 CS1 Q.25: answer and solution

GATE 2026 CS1 Q.52

GATE 2026 · CS1Q.52 · 2 marks · 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 ,

Answer and solution
See the question in the paper
GATE 2026 CS1 Q.52: answer and solution

GATE 2025 CS1 Q.19

GATE 2025 · CS1Q.19 · 1 mark · 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)

Official answer
See the question in the paper
GATE 2025 CS1 Q.19: official answer

GATE 2024 CS2 Q.52

GATE 2024 · CS2Q.52 · 2 marks · 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)

Answer and solution
See the question in the paper
GATE 2024 CS2 Q.52: answer and solution

GATE 2023 CS Q.39

GATE 2023 · CSQ.39 · 2 marks · 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

Answer and solution
See the question in the paper
GATE 2023 CS Q.39: answer and solution

GATE 2023 CS Q.40

GATE 2023 · CSQ.40 · 2 marks · 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. Which one of the following options correctly describes the language accepted by ?

GATE 2023 CS question 40: State diagram with three states s, p, q.Tap to open full size
Figure from the GATE 2023 CS paper. It is part of the question.
Figure description

State diagram with three states s, p, q. s is the start state (arrow entering from the left). Self-loop on s with two labels: a/⊥/A⊥ and a/A/AA. Self-loop on p with label b/A/ε. Self-loop on q with two labels: ε/A/ε and ε/⊥/ε. Edge s to p labelled b/A/ε. Edge s to q labelled ε/A/ε. Edge p to q labelled ε/A/ε. No state is marked accepting (acceptance is by empty stack).

  1. (A)

  2. (B)

  3. (C)

  4. (D)

Answer and solution
GATE 2023 CS Q.40: answer and solution

OTHER EXAMS

ISRO: 6 questions

From ISRO's Scientist/Engineer written test: ISRO CS 2025, ISRO CS 2023. They are not counted in the GATE numbers on this page.

ISRO CS 2025 Q.32

ISRO 2025 · CSQ.32 · 1 mark · MCQ

32. For the same input string, which of the following options is true in the case of ambiguous grammar?
(i) two different parse trees
(ii) two leftmost derivations
(iii) two rightmost derivations
(iv) leftmost and rightmost derivations are different
Which one is correct?

  1. (A)

    (i), (ii), (iii) are true; (iv) is false

  2. (B)

    (i) is true; (ii), (iii), (iv) are false

  3. (C)

    (i), (iv) are true; (ii), (iii) are false

  4. (D)

    (i) is false; (ii), (iii), (iv) are true

Answer and solution
See the question in the paper
ISRO CS 2025 Q.32: answer and solution

ISRO CS 2023 Q.8

ISRO 2023 · CSQ.8 · 1 mark · MCQ

Consider the context-free grammar G below for arithmetic expressions:
E → E + E | E × E | id
Which of the following statements is TRUE:

  1. (A)

    The string “id + id × id” has no parse tree according to G

  2. (B)

    The string “id + id × id” has only one parse tree according to G

  3. (C)

    The string “id + id × id” has exactly two parse trees according to G

  4. (D)

    The string “id + id × id” has more than two parse trees according to G

Answer and solution
See the question in the paper
ISRO CS 2023 Q.8: answer and solution

ISRO CS 2023 Q.60

ISRO 2023 · CSQ.60 · 1 mark · MCQ

Consider the context-free grammar G below. Here S is the starting non-terminal symbol, while a and b are terminal symbols.

S → aaSb | T
T → Tb | a

Which of the following statements is true about the language L(G) generated by G?

  1. (A)

    aabbaabb belongs to L(G) but aabb does not

  2. (B)

    aaaaabbb belongs to L(G) but aaaabb does not

  3. (C)

    aaaabb belongs to L(G) but aabbaabb does not

  4. (D)

    aaabb belongs to L(G) but aaaaabbb does not

Answer and solution
See the question in the paper
ISRO CS 2023 Q.60: answer and solution

PRACTICE

5 questions to test yourself

PRACTICE 11 mark · NAT

A grammar in Chomsky normal form generates a string of length 9. How many derivation steps (applications of a production) does any derivation of from the start variable take?

Answer and solution

Answer:

  1. In CNF, steps use and steps use .

  2. For this is , that is .

PRACTICE 21 mark · MCQ

Which of the following languages over (with a separator where shown) is context-free but is NOT accepted by any deterministic push-down automaton?

  1. (A)

  2. (B)

  3. (C)

  4. (D)

Answer and solution

Answer: (C)

  1. A is accepted by a DPDA: push for , pop for .

  2. B has a marker showing where the middle is, so a DPDA pushes , then pops while reading .

  3. D is a DCFL: a DPDA pushes one per and pops one per , accepting by final state if 's remain when the input ends (), or if a is read with on top and only 's follow ().

  4. In C the middle is not marked, so the machine has to guess where ends; no deterministic machine can do this. Answer C.

PRACTICE 32 marks · MSQ

Consider the grammar . Which of the following statements are true?

  1. (A)

  2. (B)

    is unambiguous

  3. (C)

    is in Chomsky normal form

  4. (D)

    The derivation of takes exactly 3 steps

Answer and solution

Answer: (A), (B), (D)

  1. Each use of adds one and one around , and ends it, so . A is true.

  2. Each string has exactly one derivation (use times then ), so G is unambiguous. B is true.

  3. has three symbols on the right and has two terminals, so G is not in CNF. C is false.

  4. is 3 steps. D is true.

PRACTICE 42 marks · NAT

For the grammar , how many distinct leftmost derivations does the string have?

Answer and solution

Answer:

  1. Leftmost derivations correspond one-to-one with parse trees.

  2. Ignoring the leaves , every tree for is a full binary tree of -nodes with leaves, so the number of trees is the Catalan number .

  3. For : .

PRACTICE 52 marks · MCQ

A PDA with one state accepts by empty stack. Its moves are , , and , with no other moves. Which string is accepted?

  1. (A)

  2. (B)

  3. (C)

  4. (D)

Answer and solution

Answer: (B)

  1. Each adds two 's (on it pushes ; on it replaces one by ). Each pops one and needs an on top. The stack can be emptied only by popping , which needs no above it, and after that no move is possible.

  2. So is accepted exactly when the number of 's is twice the number of 's and no prefix has more 's than twice its 's (for example , and also ).

  3. A: leaves two 's on the stack, and the -move cannot pop under them. C: leaves two 's. D: empties the 's after and then has no move for on .

  4. B: gives after , the four 's pop the 's, and the -move pops . Answer B.

OFFICIAL GATE 2027 SYLLABUS WORDING

Copied from the GATE 2027 syllabus; see all 2027 syllabi.

GATE CS

Context-free grammars and push-down automata

Preparing in Kolkata? IAE Academy's course for GATE CSE.