4.6.8 · D1Theory of Computation

Foundations — Context-free grammars (CFG) — productions, derivations, parse trees

1,970 words9 min readBack to topic

This page assumes nothing. If you have never seen a set, an arrow, or a star before, start here and every symbol used in the parent topic will be earned before it appears.


0. The picture behind everything: letters vs. placeholders

Imagine two buckets of tokens on a table.

Figure — Context-free grammars (CFG) — productions, derivations, parse trees
  • The left bucket holds finished letters — things like (, ), a, +. Once one of these is placed, it never changes again. Call these terminals.
  • The right bucket holds placeholders — labels like , , . A placeholder is a promise: "something will go here later, but not yet." Call these variables (or non-terminals).

Every idea below is about the rules for turning right-bucket tokens into left-bucket tokens.


1. A set and the symbol

Why the topic needs it. The parent writes and . These are just saying " is one of the placeholder tokens" and " is one of them too." No membership symbol, no way to say which tokens are legal.

The symbol means the empty set — a bag with nothing inside. The parent's rule (the two buckets share no token) uses it.


2. The alphabet and strings

Why and not just "letters"? We need a named, finite collection so a rule can say "your finished string may only use these characters." For matched parentheses, and nothing else.


3. The star (Kleene star) and

Why this tool and not just "a string"? The parent needs to talk about every possible finished string at once — the language is a subset of . The star is the compact way to say "all strings over this alphabet, any length." We could not write without it.

The mixed version (used in the definition of a production) means "all strings you can build from placeholders AND letters mixed together" — because a half-finished line like contains both.


4. The empty string

Why the topic needs it. The grammar relies on to end the recursion — without a rule that makes a placeholder disappear, you could never finish a string.


5. The arrow : a rewrite rule (production)

Figure — Context-free grammars (CFG) — productions, derivations, parse trees

The pipe | is pure shorthand: stacks two swap cards with the same left side onto one line.


6. The double arrow : one derivation step

Why two different arrows? is a rule that exists in your rulebook . is an act of using one rule right now on your current line. One is potential, the other is an event.

Figure — Context-free grammars (CFG) — productions, derivations, parse trees

7. The starred double arrow : many steps

Why the topic needs it. The language is : "all finished strings you can reach from the start placeholder by any number of swaps." Without you could only speak of one swap at a time and never define the full language.


8. The 4-tuple

Now every piece is earned, so the whole grammar-object makes sense:


9. Trees and yield (needed for parse trees)

Why the topic needs it. A derivation is a list of steps; a parse tree keeps only who-is-whose-child, discarding the order. You cannot understand "structure vs. order" or "ambiguity = two trees" without the tree picture first.


Prerequisite map

Sets and membership in

Alphabet Sigma

Kleene star and Sigma-star

Empty string epsilon

Variables V and start S

Productions with arrow

Derivation step double-arrow

Multi-step star double-arrow

Language L of G

4-tuple G

Trees and yield

Parse trees and ambiguity

This map feeds directly into the parent CFG topic. It also connects forward to Pushdown automata (the machine that recognises what CFGs generate), Chomsky hierarchy (where CFGs sit), and Compilers — parsing (where parse trees get used). Contrast with Regular expressions and DFAs — the weaker tools that cannot count nesting.


Equipment checklist

Try to answer each before revealing:

What does mean in plain words?
" is one of the items inside the bag (set) ."
What is the empty set symbol and its meaning?
— a bag with nothing inside it.
What is a string over an alphabet ?
A finite row of letters taken from , order mattering.
What does the star in collect?
Every string over of any length, including the empty string.
What is ?
The string of length zero — a blank string, not "no string."
What is the difference between and ?
is a rule that exists in ; is one act of applying such a rule to the current line.
Why can and be ignored in ?
They are untouched neighbours (context); only the single variable is rewritten.
What does allow that does not?
Zero or more steps chained, including doing nothing (so ).
Name the four parts of .
Variables, terminals (alphabet), productions (rules), start symbol.
What is the yield of a parse tree?
The string read off the leaves from left to right.
Why must the left side of a CFG rule be a single variable?
That single-variable left side is exactly what "context-free" means — applicability never depends on neighbours.