CS Atlas GATE 2027
TOC course
0 / 91 complete

TOC/Phase 3 · CFG, PDA & Context-Free Languages

Lesson 3.1.1 · Item 47 of 91

Context-free grammars, derivations and parse trees

Read at your own pace. Try the questions before opening the answers.

On this page

Recall warm-up

  1. Is { aⁿbᵐ : n ≡ m (mod 3) } regular?
    Stuck? Peek at 2.4.4 Drill: regular or not?
    • Bounded fact (mod, flag, last k, capped count, bounded difference) ⇒ regular.
    • Exact comparison of two unbounded counts, ww, wwᴿ, squares, primes, 2ⁿ ⇒ not regular.
    • Finite ⇒ regular. Unary: regular ⇔ lengths eventually periodic.
    • wxwᴿ, wxw (x ∈ Σ⁺) regular. #ab = #ba regular. ww over one letter regular.

    Read lesson 2.4.4

  2. What does L(G) mean for a grammar G?
    Stuck? Peek at 2.3.1 What a grammar is
    • G = (V, T, P, S); A → x | y; sentential form; ⇒, ⇒*; L(G) = {w ∈ T* : S ⇒* w}.
    • S → aSb | ε gives aⁿbⁿ (not regular) — general grammars exceed DFAs.
    • Palindromes: S → aSa | bSb | a | b | ε.

    Read lesson 2.3.1

  3. Why is S → aA, A → Sb | b not a regular grammar?
    Stuck? Peek at 2.3.2 Right-linear and left-linear grammars
    • Right-linear: A → wB | w. Left-linear: A → Bw | w. Regular grammar = one of the two (not mixed).
    • DFA → grammar: δ(A,a)=B ⇒ A → aB; A accepting ⇒ A → ε.
    • Grammar → NFA: A → aB is an arrow; A → a goes to a new accepting state.
    • Mixed linear can give aⁿbⁿ. Non-regular grammar does not imply non-regular language.

    Read lesson 2.3.2

The Hook

Every compiler you have used starts by checking that your code is grammatical: that if (x) { y = (a + b) * c; } has matching brackets, operators between operands, and so on. The rules in the language manual look like Expr → Expr + Term | Term. Those are context-free grammars (CFGs). They generate exactly the structures finite automata could not: nested, matched, recursive. This lesson makes them precise and shows the tree that every derivation builds.

Predict First

Grammar: S → aSb | ε. Draw (in your head) the tree of rule applications for aabb. How many times is the rule S → aSb used?

Core Concept

Prediction check

Twice, then S → ε once. The tree has S at the root, an a–S–b layer, another a–S–b layer, and an ε leaf in the middle. Read the leaves left to right: a a ε b b = aabb.

steps illustration
The parse tree of aabb, grown in derivation order. Press Play.

Definition

Context-free grammar G = (V, T, P, S): every production has the form A → α where A is a single variable and α ∈ (V ∪ T)* is any string of variables and terminals.

“Context-free” means a variable can be rewritten regardless of what surrounds it. Right-linear grammars are a special case, so every regular language is context-free. A language is context-free (a CFL) if some CFG generates it.

context-free languages generated by CFGs
regular languages right-linear CFGs are a special case
Regular ⊂ CFL, and the inclusion is strict: aⁿbⁿ is in the outer ring only.

Leftmost and rightmost derivations

When a sentential form has several variables, you choose which to rewrite.

  • Leftmost derivation (⇒ₗₘ): always rewrite the leftmost variable.
  • Rightmost derivation (⇒ᵣₘ): always the rightmost.

Example G: S → AB, A → aA | a, B → bB | b, w = aab.
Leftmost: S ⇒ AB ⇒ aAB ⇒ aaB ⇒ aab.
Rightmost: S ⇒ AB ⇒ Ab ⇒ aAb ⇒ aab.
Different orders, the same tree.

stepleftmostrightmost
0SS
1ABAB
2aABAb
3aaBaAb
4aabaab
Bold = the variable rewritten next. Both orders build the same parse tree.

Parse trees

Parse tree (derivation tree): root = S; each internal node is a variable A whose children, left to right, are the symbols of the body of a rule A → α (a leaf ε for A → ε); leaves read left to right give the yield.

Key fact: for every string, parse trees ↔ leftmost derivations ↔ rightmost derivations, one-to-one. A tree forgets only the order in which variables were rewritten.

SAaAaBb
Parse tree of aab for S → AB, A → aA | a, B → b…: the yield (leaves, left to right) is aab.

Language of a CFG

L(G) = { w ∈ T* | S ⇒* w }. To show w ∈ L(G), give a derivation or tree. To show L(G) equals some described set, prove both directions (every derived string has the property; every string with the property can be derived) — usually by induction on derivation length or string length.

G
S → aSb | ε
derivations
S ⇒ aSb ⇒ aaSbb ⇒ …
L(G)
{ aⁿbⁿ : n ≥ 0 }
From rules to language.

Putting it together

  • A CFG rewrites one variable at a time, regardless of context.
  • Each derivation builds a parse tree; leftmost and rightmost orders correspond one-to-one with trees.
  • CFLs include every regular language and also aⁿbⁿ.

Visual

G:  E → E + T | T        T → T * F | F        F → (E) | id
parse tree of id + id * id (this grammar forces * below +):

                 E
               / | \
              E  +  T
              |   / | \
              T  T  *  F
              |  |     |
              F  F     id
              |  |
              id id
leftmost derivation: E ⇒ E+T ⇒ T+T ⇒ F+T ⇒ id+T ⇒ id+T*F ⇒ id+F*F ⇒ id+id*F ⇒ id+id*id

Worked Examples

Easy

G: S → aS | bS | c. Give a leftmost derivation of abc.

Show the full solution

S ⇒ aS ⇒ abS ⇒ abc. (Only one variable at a time, so leftmost = rightmost here.) L(G) = (a+b)*c.

Medium

G: S → SS | (S) | ε. Give a parse tree for (())().

Show the full solution

S → SS; left S → (S) with inner S → (S) with inner S → ε gives (()); right S → (S) with S → ε gives (). Yield: (())() ✓.

GATE-level

For G: S → aSa | bSb | a | b, how many steps does the derivation of a string of length 7 take, and what is L(G)?

Show the full solution

Each aSa / bSb step adds 2 symbols; the last step (a or b) adds 1. Length 7 = 2·3 + 1 ⇒ 3 + 1 = 4 steps. L(G) = odd-length palindromes over {a, b}.

GATE Traps

  • Leftmost and rightmost derivations of the same string can differ while the parse tree is the same — different derivations do not mean ambiguity.
  • The yield reads leaves left to right, skipping ε leaves.
  • Every regular language is context-free, not the other way around.

Memory Hook

A CFG is a family tree: every variable has its children written right under it, and the leaves along the bottom spell the sentence.

Connect the Dots

CFGs can generate what finite automata cannot. Next: the toolkit for writing CFGs for the standard languages — aⁿbⁿ, palindromes, balanced brackets, equal counts, unequal counts.

Self-Test

  1. MCQ

    Which production is NOT allowed in a CFG?

    1. S → ε
    2. S → aSbS
    3. aS → b
    4. A → BCD
  2. MSQ

    For a fixed string w and grammar G, which are in one-to-one correspondence?

    1. parse trees of w
    2. leftmost derivations of w
    3. rightmost derivations of w
    4. all derivations of w
  3. NAT

    With S → aSb | ε, how many derivation steps produce a⁵b⁵?

    Write your answer before opening the key.

  4. MCQ

    L(S → aSa | bSb | ε) is:

    1. all palindromes
    2. even-length palindromes
    3. odd-length palindromes
    4. (a+b)*

Rule Card

  • CFG: A → α, single variable on the left. CFL = L(G) for some CFG. Regular ⊂ CFL.
  • Leftmost / rightmost derivations; parse tree with yield = leaves left to right.
  • Trees ↔ leftmost derivations ↔ rightmost derivations (one-to-one per string).

Concept Ledger

  • context-free grammar (CFG)
  • context-free language (CFL)
  • leftmost derivation
  • rightmost derivation
  • parse tree (derivation tree)
  • yield

Answer Vault

Reveal answers when you’re ready

Warm-up answers

  1. Yes — track (n − m) mod 3.
  2. All terminal strings derivable from the start symbol.
  3. It mixes right-linear and left-linear rules; it generates aⁿbⁿ.

Self-test answers

  1. C — The left side must be a single variable.
    • A: Allowed.
    • B: Allowed.
    • D: Allowed.
  2. ABC — A tree fixes the rules; leftmost/rightmost fix the order.
    • D: Arbitrary orders give many derivations per tree.
  3. 6 — 5 × (S → aSb) + 1 × (S → ε).
  4. B — No odd middle symbol.
    • A: Misses odd lengths.
    • C: No middle a/b rule.
    • D: Too many.

Flashcards

  1. What makes a grammar context-free?
    Reveal answerEvery production has a single variable on the left: A → α.
  2. Parse trees vs leftmost derivations?
    Reveal answerOne-to-one for each string.
  3. Is every regular language context-free?
    Reveal answerYes (right-linear grammars are CFGs).

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.