A context-free grammar (CFG) is a formal grammar G=(V,Σ,R,S) in which each production rule is of the form A→w, where A∈V and w∈(V∪Σ)∗.
Let G=(V,Σ,R,S) be a CFG.
If u,v,w∈(V∪Σ)∗ and A→w is a rule, then we say that uAvyieldsuwv, written uAv⇒uwv
The language L(G)={w∈Σ∗∣S⇒∗w} (which is the set of all strings generated by G) is called the language generated by G and is said to be a context-free language (CFL).
Given a string w∈Σ∗:
A derivation of w in G is a sequence of substitutions of the form:
S⇒u1⇒u2⇒⋯⇒un=w, where each ui is in (V∪Σ)∗
If such a derivation for w exists in G, we say that Ggeneratesw (or Sderivesw) and write S⇒∗w
w is said to be derived ambiguously in G if it has two or more different leftmost derivations.
A derivation of w is a leftmost derivation if at every step the leftmost remaining variable is the one replaced.
The following are equivalent:
w∈L(G)
S⇒∗w
There exists a parse tree for w with respect to G
A parse tree for w with respect to G is a rooted ordered tree in which:
The root is labeled by the start variable S
The leaves are labeled by the symbols of w
Each internal node is labeled by a variable A and has children labeled X1,…,Xn if A→X1…Xn is a rule in G
There is a bijection between the set of left-most derivations of w and the set of parse trees for w with respect to G.
G is ambiguous if it generates at least one string ambiguously.
A CFG is ambiguous iff it generates some string with two different parse trees.
A CFG is in Chomsky normal form if every rule is of the form A→BC, A→a, or S→ε, where A,B,C∈V, a∈Σ, and S is the start variable, and B and C are not S.
If G is a CFG in Chomsky normal form, and w∈L(G), then ∣w∣≤2∣h∣−1, where h is the height of the parse tree for w.
(problem 2.26) If G is a CFG in Chomsky normal form, and w∈L(G), where ∣w∣≥1, then every derivation of w requires exactly 2∣w∣−1 steps.
(2.9) Every CFL is generated by a CFG in Chomsky normal form.
Two CFGs G1 and G2 are said to be equivalent if L(G1)=L(G2).
Remark: There can be multiple CFGs that generate the same language, moreoever, some of these may be ambiguous while others are unambiguous.
(2.34, Pumping lemma) If L is a CFL, then there exists a number p (the pumping length) such that any string s∈L with ∣s∣≥p can be written as s=uvxyz, where:
∀i≥0,uvixyiz∈L
∣vxy∣≤p
∣vy∣>0
The class of CFLs is closed under: union, concatenation, Kleene star, reversal, and intersection with regular languages. However, it is not closed under intersection or complementation.
Pushdown Automata (PDA)
A pushdown automaton (PDA) is a 6-tuple M=(Q,Σ,Γ,δ,q0,F), where Q, Σ, Γ, and F are all finite sets, and
Q is the set of states
Σ is the input alphabet
Γ is the stack alphabet
δ:Q×Σε×Γε⟶P(Q×Γε) is the transition function
q0∈Q is the start state
F⊆Q is the set of accept states
A pushdown automaton M=(Q,Σ,Γ,δ,q0,F)accepts a string w∈Σ∗ if there exists a sequence of states r0,r1,…,rm∈Q and strings s0,,s1,…,sm∈Γ∗ such that:
r0=q0 and s0=ε
For i=0,1,…,m−1, we have (ri,b)∈δ(ri,wi+1,a), where si=at and si+1=bt for some a,b∈Γε and t∈Γ∗.
rm∈F
The set of strings accepted by M is the language denoted by L(M), and is called the language recognized by M.
A PDA can be represented using a state diagram, where each transition is labeled by the notation "a,b→c" to denote that the PDA:
Reads the symbol a from the input (or read nothing if a=ε)
Pops the symbol b from the stack (or pops nothing if b=ε)
Pushes c onto the stack (or pushes nothing if c=ε)
CFL to CFG
Given a CFL L, we can construct a CFG G such that L(G)=L.todo
CFL to PDA
Given a CFL L, we can construct a PDA M such that L(M)=L.todo
CFG to CNF
(2.9) Given a CFG G=(V,Σ,R,S) we can construct an equivalent CFG G′ in Chomsky normal form
Add new start variable S0 and new rule S0→S
For each an ε-rule, A→ε, where A=S0
Remove A→ε
For every rule R→u1Au2Au3⋯un−1Aun, add the rules:
R→u1u2Au3⋯un−1Aun
R→u1Au2u3⋯un−1Aun
…
R→u1Au2Au3⋯un−1un
For each unit rule A→B
Remove A→B
For each rule B→u, where u∈(V∪Σ)∗
Add the rule A→u, unless this was a unit rule previously removed
For each rule A→u1u2⋯uk, where k≥3 and ui∈V∪Σ,
Replace A→u1u2⋯uk with the rules, A→u1A1,A1→u2A2,…,Ak−2→uk−1uk, where Ai‘s are new variables.
Repalce each terminal ui in the previous rule(s) with the new variable Ui and add the rule Ui→ui
DFA to CFG
Let M=(Q,Σ,δ,q0,F) be a DFA. We can convert M to a CFG G=(V,Σ,R,S) as follows:
Make a variable Ri for each state qi∈Q
Add a rule Ri→aRj to the CFG if δ(qi,a)=qj
Add a rule Ri→ε if qi∈F
Make R0 the start variable of the CFG, where q0 is the start state of M