Skip to content
GATEiit

GATE CSE · Theory of Computation

Context-free grammars and push-down automata

Context-free grammars, derivations, parse trees and ambiguity, Chomsky normal form, and push-down automata (DPDA and NPDA), with the grammar and PDA design and counting questions GATE asks.

Before you start

  • Finite automata →

    A PDA is a finite automaton with a stack; its states and transitions are read the same way.

  • Stacks →

    Every PDA move is a push, a pop or a replace on the top of the stack.

  • Trees →

    A derivation is drawn as a parse tree; CNF parse trees are binary trees.

This topic comes before: Lexical analysis, parsing and syntax-directed translation, Regular and context-free languages; pumping lemma, Turing machines and undecidability

Concept notes

1. Grammars, derivations and parse trees

A context-free grammar (CFG) is G=(V,T,P,S): variables V, terminals T, productions of the form A→α with A∈V and α∈(V∪T)∗, and a start symbol S. The language is L(G)={w∈T∗:S⇒∗w}.

  • A leftmost derivation always replaces the leftmost variable; a rightmost derivation replaces the rightmost one.
  • A parse tree has S at the root, terminals (or ε) at the leaves, and each internal node with its children forms a production. Reading the leaves left to right gives the yield.
  • Each parse tree corresponds to exactly one leftmost and one rightmost derivation.

Reading a grammar. Find what each production adds. S→aSb∣ε adds one a and one b around the middle, so L={anbn:n≥0}. S→aSb∣SS∣ε generates balanced strings (read a as "(" and b as ")"). S→aSa∣bSb∣a∣b∣ε generates all palindromes over {a,b}.

Designing a grammar. Match the counts that must be equal by producing them together in one production, and generate free parts with separate variables. For {aibjcj:i,j≥0}: S→AB, A→aA∣ε, B→bBc∣ε.

Before answering "which language does this grammar generate", always test the shortest strings (ε, length 1, length 2). This rules out most wrong options quickly.

2. Ambiguity

A grammar is ambiguous if some string has two or more parse trees (equivalently, two leftmost derivations, or two rightmost derivations). Two different derivations with the same tree do not make a grammar ambiguous.

The standard example is E→E+E∣E∗E∣id. The string id+id∗id has two trees: one with + at the root and one with ∗ at the root. With only one operator, E→E+E∣id, a string of n operands has Cn−1 parse trees, where Ck=1k+1(2kk) is the Catalan number (1, 1, 2, 5, 14, …).

Removing ambiguity for expressions: one variable per precedence level, and left recursion for left associativity:

E→E+T∣T,  T→T∗F∣F,  F→(E)∣id.

Key facts:

  • Checking whether an arbitrary CFG is ambiguous is undecidable.
  • Some CFLs are inherently ambiguous: every grammar for them is ambiguous. Example: {aibjck:i=j or j=k}.
  • Every DCFL has an unambiguous grammar. So an inherently ambiguous language is never a DCFL.
EE+EidE*EididEE*EE+Eididid+ at the root: id + (id * id)* at the root: (id + id) * id
Two parse trees for id + id * id under E → E + E | E * E | id, so the grammar is ambiguous

3. Simplification and Chomsky normal form

Before converting to a normal form, clean the grammar in this order:

  1. Remove ε-productions (except S→ε if ε∈L): find nullable variables and add every version of each production with nullable symbols dropped.
  2. Remove unit productions A→B: copy B's non-unit productions to A.
  3. Remove useless symbols: first drop variables that derive no terminal string (non-generating), then drop symbols not reachable from S. This order matters.

Chomsky normal form (CNF): every production is A→BC or A→a (plus S→ε if needed). Every CFL without ε has a CNF grammar.

Fact about CNF Value
Steps to derive a string of length n≥1 exactly 2n−1 (n−1 of type A→BC, n of type A→a)
Shape of parse tree binary above the last level
Parse tree of height h (edges) yield length at most 2h−1

Greibach normal form (GNF): every production is A→aα with α∈V∗. A string of length n is derived in exactly n steps, since each step emits one terminal.

Membership (CYK). For a CNF grammar, the CYK algorithm decides whether w∈L(G) in O(n3) time for ∣w∣=n (grammar size taken as constant), by filling a triangular table of variables for every substring. So membership in a CFL is decidable.

4. Push-down automata

A push-down automaton (PDA) is a finite automaton with a stack: P=(Q,Σ,Γ,δ,q0,Z0,F). A move δ(q,a,X)∋(p,γ) means: in state q, reading a (or ε) with X on top of the stack, go to p and replace X by the string γ (leftmost symbol on top). γ=ε is a pop; γ=YX pushes Y.

A configuration (instantaneous description) is (state, remaining input, stack). A PDA can accept by:

  • final state: input fully read and the PDA is in a state of F;
  • empty stack: input fully read and the stack is empty.

For nondeterministic PDAs both modes accept exactly the CFLs, and a PDA of one kind converts to the other.

Design pattern. Push for the first part, pop for the matching part, and accept when the bottom marker Z0 shows again. For {anbn:n≥1}: push A for each a; on the first b switch to a popping state; pop one A per b; on ε with Z0 on top move to the final state. Any a after a b has no move, so the PDA rejects.

CFG ⇔ PDA. From a grammar build a one-state PDA: expand the variable on top of the stack using a production (an ε-move), and pop a terminal on top when it matches the input. The reverse construction also exists, so NPDAs accept exactly the CFLs.

q0q1q2a, Z / AZa, A / AAb, A / εb, A / εε, Z / ZLabel "x, X / γ": read x with X on top, replace X by γ
PDA accepting { aⁿbⁿ : n ≥ 1 } by final state; Z is the bottom-of-stack marker

5. Deterministic vs nondeterministic PDA

A PDA is deterministic (DPDA) if from every configuration at most one move is possible: for each (q,a,X) at most one choice, and if an ε-move exists on (q,X) then no input move exists on (q,a,X). Languages accepted by DPDAs (by final state) are the deterministic CFLs (DCFLs).

Regular⊊DCFL⊊CFL

Unlike finite automata, nondeterminism adds power to PDAs.

Language Class Why
{anbn} DCFL switch from push to pop at the first b
{wcwR} DCFL the marker c says where the middle is
{wwR} CFL, not DCFL the PDA must guess the middle
{anbn}∪{anb2n} CFL, not DCFL must guess how many to pop per b
{anbncn} not CFL one stack cannot check three counts

Facts GATE tests:

  • For DPDAs, empty-stack acceptance is weaker than final-state acceptance: it accepts only DCFLs with the prefix property (no string in the language is a proper prefix of another). For NPDAs the two modes are equal.
  • DCFLs are closed under complement, but not under union or intersection. CFLs are closed under union but not under complement or intersection.
  • Every DCFL has an unambiguous grammar (an LR(1) grammar). This is why parsers for programming languages work with deterministic grammars.

Formulas to remember

CNF derivation lengthString of length n≥1 in Chomsky normal form
steps=2n−1
GNF derivation lengthEach step emits exactly one terminal
steps=n
Parse trees of a chain with one binary operatorn operands under E→E∘E∣id; gives 1, 1, 2, 5, 14
Cn−1=1n(2n−2n−1)
CNF yield boundh = longest root-to-leaf path in edges
∣w∣≤2h−1
CYK membershipFor a fixed CNF grammar and ∣w∣=n
O(n3)

Common mistakes

  • Calling a grammar ambiguous because a string has two different derivations; ambiguity needs two different parse trees (or two leftmost derivations).
  • Forgetting that for a deterministic PDA, empty-stack acceptance is weaker than final-state acceptance; the two are equal only for nondeterministic PDAs.
  • Removing unreachable symbols before non-generating ones; drop non-generating variables first, then unreachable symbols.
  • Assuming nondeterminism adds nothing, as with finite automata; for PDAs it does, and ww^R is the standard example.
  • Choosing a 'language of the grammar' option without testing the shortest strings, which catches missing or extra ε.

Worked examples

Try each step yourself, then reveal the working.

Example 1: How many parse trees?

numerical

For the grammar E→E+E∣id, how many parse trees does id+id+id+id have?

  1. Step 1: Pick the operator used at the root.

    Try it on paper first.

Example 2: Steps in a CNF derivation

numerical

A grammar is in Chomsky normal form. How many derivation steps produce a string of length 6, and how many of those steps use a production of the form A→BC?

  1. Step 1: Count the $A \to a$ steps.

    Try it on paper first.

Example 3: Tracing a PDA on aabb

conceptual

Using the PDA in the diagram for {anbn:n≥1}, trace the configurations on input aabb, then explain why aab is rejected.

  1. Step 1: Read the two a's.

    Try it on paper first.

Example 4: Does this language need nondeterminism?

conceptual

Classify each as DCFL or CFL-but-not-DCFL: L1={wcwR:w∈{a,b}∗}, L2={wwR:w∈{a,b}∗}, L3={ambn:m≠n}.

  1. Step 1: Ask: can the PDA tell when to switch from pushing to popping?

    Try it on paper first.

Previous year questions

17 official GATE CSE questions on this topic, newest first.

Practise these 17 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.