A (nondeterministic) pushdown automaton (PDA) is a -tuple , where , , , and are all finite sets, and
is the set of states,
is the input alphabet,
is the stack alphabet,
is the transition function,
is the start state, and
is the set of accept states.
A language is called context-free if a PDA recognizes it.
PDAs are often described with two different acceptance modes: by final state and by empty stack. We assume acceptance by final state. The two are shown to be equivalent.
Context-Free Grammar
A context-free grammar (CFG) is a -tuple where
is a finite set called the variables (or nonterminals).
is a finite set, disjoint from , called the terminals.
is a finite set of rules, with each rule being a variable to a string of variables/terminals.
is the start variable.
They are equivalent to PDAs in their descriptive power.
If , , and are strings of variables and terminals, and , we say that yields (denoted ). We say derives (denoted ), if or if a sequence exists for and
A derivation of is a sequence of substitution steps, beginning with the start variable, that produces . A derivation of a string in a grammar is a leftmost derivation if at every step the leftmost remaining variable is the one replaced.
Ambiguity
A string is derived ambiguously in context-free grammar if it has two or more different leftmost derivations. Grammar is ambiguous if it generates some string ambiguously.
A context-free language that can be generated only from an ambiguous grammar is called inherently ambiguous.
Reductions
A reduce step is a reversed substitution, whereby the string of terminals and variables on the RHS of a rule is replaced by the variable on the corresponding LHS. If and be strings of variables and terminals, write to mean that can be obtained from by a reduce step. A reduction from to is a sequence
We say that is reducible to , written , whenever . The string being replaced is called the reducing string.
A reduction from is a reduction from to the start variable. A leftmost reduction is a reduction in which each reducing string is reduced only after all other reducing strings to its left.
Valid Strings
Let be a CFG with terminals . A string that appears in a leftmost reduction of some string in is called a valid string.
Let be a valid string. In the reduce step , suppose rule was applied in reverse. That is, and for some strings and . Then , along with reducing rule , is called a handle of .
We say that a handle of a valid string is a forced handle if is the unique handle in every valid string where . We say is forced by lookahead if is the unique handle of every valid string where agrees with on their first symbols. (If either or is shorter than , the strings must agree up to the length of the shorter one).
Chomsky Normal Form
A context-free grammar is in Chomsky normal form if every rule is of the form
where is any terminal, and , , and are variables distinct from start variable . This is typically done in stages:
Introduce a new start variable with rule .
This ensures the start state is never on the RHS.
Eliminate all -rules (except for that of the start variable).
These are the rules that map a variable to .
Eliminate all unit rules.
These are the rules that map a variable to another variable.
Introduce new rules to match forms above.
Closure Operations
Let and be context free languages over alphabet . Such languages are closed under the following:
Union:
Reversal:
Concatenation:
Kleene star:
Furthermore, the intersection of a CFL with a regular language is context-free.
Pumping Lemma
Let be a context-free language. Then there exists a pumping constant where, if is any string in of at least length , then can be written as such that
for any integer ,
, and
.
The stronger variant of this lemma allows one to strengthen condition 2 as follows: