Pushdown Automata and CFG
Chapter overview: Pushdown automata extend finite-state machines with a stack. Context-free grammars provide a compact way to describe recursive and nested structures such as balanced parentheses and programming-language syntax.
3.1 Pushdown Automata
Introduction / Definition
A Pushdown Automaton (PDA) is a finite-state machine equipped with a stack. The stack provides unbounded but structured memory and allows PDAs to recognize context-free languages. A PDA can push symbols onto the stack, pop symbols, or leave the stack unchanged while reading input.
Syntax / Structure / Formula
δ(q, a, X) → (p, γ)
where q,p are states, a is an input symbol (or ε), X is stack top, and γ replaces X.
Detailed Explanation
For L={0^n1^n | n≥0}, the PDA pushes one marker for each 0. When 1s begin, it pops one marker for each 1. Acceptance occurs when the counts match and the input is consumed.
Example
Input: 000111
Read 0: push X three times
Read 1: pop X three times
Output / Result
All input is consumed and the stack returns to its expected base condition, so the string is accepted.
Important Points / Notes
- PDA = finite control + stack.
- PDAs recognize context-free languages.
- Stack supports nested structures.
- PDAs are stronger than FSMs for many counting/nesting tasks.
Mini Practice Question
How could a PDA recognize balanced parentheses?
3.2 Context-Free Grammars
Introduction / Definition
A Context-Free Grammar (CFG) is a formal grammar G=(V,Σ,P,S), where V is the set of variables/nonterminals, Σ is the terminal alphabet, P is the set of productions, and S is the start symbol. Each production has a single nonterminal on its left side. CFGs are useful for describing recursive structures.
Syntax / Structure / Formula
G = (V, Σ, P, S)
Example: S → 0S1 | ε
Detailed Explanation
The grammar S → 0S1 | ε generates equal numbers of 0s followed by 1s. Derivations replace a nonterminal with the right side of a production until only terminals remain.
Example
S ⇒ 0S1 ⇒ 00S11 ⇒ 000111
Output / Result
The string 000111 is generated by the grammar.
Important Points / Notes
- CFGs can describe nesting and recursion.
- A grammar can be ambiguous.
- Parse trees show derivation structure.
- CFGs are central to syntax analysis.
Mini Practice Question
Construct a CFG for balanced parentheses.
3.3 Relationship between CFGs and Programming Languages
Introduction / Definition
Programming languages contain nested and recursive structures such as expressions, blocks, function calls, and conditional statements. CFGs provide a formal way to describe much of this syntax. Lexical rules are often handled by regular languages, while hierarchical syntax is commonly modeled using context-free grammars.
Syntax / Structure / Formula
- Tokens → CFG → Parse Tree → Semantic Analysis
Detailed Explanation
For an arithmetic expression grammar, rules can define how expressions contain terms and operators. A parser uses these grammar rules to build a parse tree or abstract syntax tree.
Example
E → E + T | T
T → T * F | F
F → (E) | id
Output / Result
The grammar describes nested arithmetic expressions such as id + id * id.
Important Points / Notes
- Lexers commonly use regular-language techniques.
- Parsers commonly use CFG-based methods.
- Real programming languages may require additional context-sensitive semantic rules.
- ASTs are practical representations of parsed structure.
Mini Practice Question
Why are CFGs useful for representing nested blocks and expressions?