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.
A context-free grammar (CFG) is : variables , terminals , productions of the form with and , and a start symbol . The language is .
A leftmost derivation always replaces the leftmost variable; a rightmost derivation replaces the rightmost one.
A parse tree has 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. adds one and one around the middle, so . generates balanced strings (read as "(" and as ")"). generates all palindromes over .
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 : , , .
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 . The string has two trees: one with at the root and one with at the root. With only one operator, , a string of operands has parse trees, where is the Catalan number (1, 1, 2, 5, 14, …).
Removing ambiguity for expressions: one variable per precedence level, and left recursion for left associativity:
, , .
Key facts:
Checking whether an arbitrary CFG is ambiguous is undecidable.
Some CFLs are inherently ambiguous: every grammar for them is ambiguous. Example: .
Every DCFL has an unambiguous grammar. So an inherently ambiguous language is never a DCFL.
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:
Remove -productions (except if ): find nullable variables and add every version of each production with nullable symbols dropped.
Remove unit productions : copy 's non-unit productions to .
Remove useless symbols: first drop variables that derive no terminal string (non-generating), then drop symbols not reachable from . This order matters.
Chomsky normal form (CNF): every production is or (plus if needed). Every CFL without has a CNF grammar.
Fact about CNF
Value
Steps to derive a string of length
exactly ( of type , of type )
Shape of parse tree
binary above the last level
Parse tree of height (edges)
yield length at most
Greibach normal form (GNF): every production is with . A string of length is derived in exactly steps, since each step emits one terminal.
Membership (CYK). For a CNF grammar, the CYK algorithm decides whether in time for (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: . A move means: in state , reading (or ) with on top of the stack, go to and replace by the string (leftmost symbol on top). is a pop; pushes .
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 ;
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 shows again. For : push for each ; on the first switch to a popping state; pop one per ; on with on top move to the final state. Any after a 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.
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 at most one choice, and if an -move exists on then no input move exists on . Languages accepted by DPDAs (by final state) are the deterministic CFLs (DCFLs).
Unlike finite automata, nondeterminism adds power to PDAs.
Language
Class
Why
DCFL
switch from push to pop at the first
DCFL
the marker says where the middle is
CFL, not DCFL
the PDA must guess the middle
CFL, not DCFL
must guess how many to pop per
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 in Chomsky normal form
GNF derivation lengthEach step emits exactly one terminal
Parse trees of a chain with one binary operator operands under ; gives 1, 1, 2, 5, 14
CNF yield bound = longest root-to-leaf path in edges
CYK membershipFor a fixed CNF grammar and
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 , how many parse trees does have?
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 ?
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 , trace the configurations on input , then explain why is rejected.
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: , , .
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.
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.