Context Free Grammar
A context-free grammar is a 4-tuple
a finite set of variables a finite set disjoint from of terminals a set of rules, consisting of a variable and a string of variables and terminals is the start variable
Given strings
The language of the grammar is
Any regular language can be turned into a context free language. For arrow from state
A derivation is a left-most derivation if at every step, the leftmost terminal is expanded first. A grammar is ambiguous if multiple left-most derivations can generate the same string
Chomsky Normal Form
A CFG is in Chomsky normal form if every rule is of the following form, where
Every CFG can be written in Chomsky normal form
- Create a new start variable
- For all
, remove this rule. Then for all , add (note that can contain ). Iterate - For all
, remove this rule. Whenever appears, add unless this was a previously removed unit rule. - For all
with , add rules , and . Replace terminals with and add
Pushdown Automata
A pushdown automata is a 6-tuple
, a finite set of states , a finite input alphabet , a finite stack alphabet a transition function the start state the set of accept states
A pushdown automata accepts input
CFLs are PDAs
Given a CFG
By this construction, writing
Take
- If the stack has a variable,
- If the stack has a terminal,
- If the stack is the empty
,
PDAs are CFLs
Let PDA
- For
, , and , if contains and contains , add the rule - For
, add - For
, add the rule
Pumping Lemma for CFL
If
- for each
, , and
Given a derivation