Context-free grammar
A finite collection of productions A→α, where A is one nonterminal and α is a string of terminals and nonterminals, with a designated start symbol.
REFERENCE SHEET
A stack adds unbounded nesting to finite-state control.
Before this Finite automata & regular languages
A finite collection of productions A→α, where A is one nonterminal and α is a string of terminals and nonterminals, with a designated start symbol.
A derivation applies productions to generate a terminal string. A parse tree records the hierarchical production structure.
A grammar is ambiguous if some terminal string has two distinct parse trees. This is a property of a grammar; some languages have no unambiguous context-free grammar.
A language is context-free exactly when a nondeterministic pushdown automaton recognizes it. Deterministic pushdown automata recognize a strictly smaller class.
Context-free languages are closed under union, concatenation, star, and intersection with a regular language, but not in general under intersection or complement.
For a grammar in Chomsky normal form and a nonempty length-n input, CYK decides membership in O(|P|n³) time, where P is the production set. Treat ε separately.
Grammars describe nested expressions and syntactic structure in compilers.
Balanced delimiters and recursive tree-shaped formats require more memory than a finite automaton in general.
01 / Derive
Expand the trailing S once more.
S⇒(S)S⇒()S⇒()(S)S⇒()()S⇒()().
02 / Explain
Consider id+id*id.
The string can parse as (id+id)*id or id+(id*id). Both trees use the productions, so the grammar does not enforce multiplication precedence.
Further reading and full-length treatments: