Sub topic 1
Alphabets, strings, and languages
Prerequisites
This chapter assumes you know finite automata, regular expressions, basic set operations, and proof by contradiction. You should also know the pumping lemma for regular languages, which is recapped briefly below.
Alphabets, strings, and languages
An alphabet Σ is a finite, nonempty set of symbols, such as Σ = {a, b}. A string is a finite sequence of symbols from Σ. The empty string, written ε, has length zero. A language is any set of strings over Σ. For example, L = {ab, aabb} is a finite language over {a, b}.
Where regular languages stop
A regular language is one that a finite automaton recognises. Such an automaton has no memory beyond its current state, so it cannot count. It cannot recognise {a^n b^n | n ≥ 1}, because it would need unbounded memory to match the a's with the b's. Context-free grammars and pushdown automata add a stack, which provides exactly that memory.
Context-free grammars: the four-tuple and basic examples
A context-free grammar (CFG) is a four-tuple G = (V, Σ, P, S):
- V is a finite set of variables (nonterminals).
- Σ is the terminal alphabet.
- P is a finite set of productions of the form A → α, where A is a variable and α is a string over V ∪ Σ.
- S ∈ V is the start symbol.
For example, S → aSb | ε generates {a^n b^n | n ≥ 0}.
Leftmost and rightmost derivations and parse trees
A derivation rewrites the start symbol step by step until only terminals remain. A leftmost derivation always expands the leftmost variable. For S → aSb | ε, the leftmost derivation of aabb is:
S => aSb => aaSbb => aabbEach derivation corresponds to a parse tree. The tree for aabb is:
Ambiguous grammars and inherently ambiguous languages
A grammar is ambiguous if some string has two different parse trees. The grammar E → E + E | E E | id is ambiguous, because id + id id can be parsed with + or * at the root.
Simplifying grammars: useless symbols, epsilon and unit productions
Before converting a grammar, remove three kinds of clutter:
- Useless symbols: variables that cannot derive a terminal string, or that cannot be reached from S.
- Epsilon productions: rules of the form A → ε. Remove them, and add variants of other rules that skip A.
- Unit productions: rules of the form A → B. Replace them by copying B's productions into A.
Chomsky and Greibach normal forms
In Chomsky normal form (CNF), every production is either A → BC with B and C variables, or A → a with a a terminal. In Greibach normal form (GNF), every production has the form A → aα, where a is a terminal and α is a string of variables.
Exercise
Is S → aSb | ab already in Chomsky normal form? Explain.
Show answer
No. The production S → aSb has a terminal a and a variable S mixed in a right side of length three. A CNF version needs new variables, such as A → a, B → b, and S → A X, X → S B, and S → A B.