# Chapter 2: Context-Free Languages

## For Your Enjoyment

## Chapter 2.2, Pushdown Automata

### Theorem 2.20

A language is context free if and only if some pushdown
automaton recognizes it.

### CFG → PDA Construction

#### Step One: Create a 3-State PDA

- The states are q
_{start}, q_{loop} and q_{accept}.
- q
_{accept} is an accept state.
- q
_{start} transitions to q_{loop}
with the rule ε, ε → S$
- q
_{loop} transitions to itself with the rules
ε, A → w (for each rule A → w) and
a, a → ε (for each terminal a).
- q
_{loop} transitions to q_{accept} with
the rule ε, $ → ε

#### Step Two: Add States to Push One Symbol at a Time

For example, instead of q_{start} transitioning to
q_{loop} directly on rule ε, ε → S$,
it transitions to an intermediate state on rule ε, ε
→ $ and the intermediate state transitions to q_{loop}
on rule ε, ε → S.

#### Active Learning Problem

Convert the following CFG to a PDA using the procedure above.

E → E + T | T
T → T * F | F
F → (E) | a

### PDA → CFG Construction

#### Preliminary Construction

Convert the original PDA to a PDA with the following characteristics.

- The PDA should have a single accept state.
- The PDA should empty its stack before accepting.
- Each transition should either push a symbol onto the stack or
pop one symbol off. It should not do both at the same time.

#### Active Learning

Convert the following transition
δ(r, 0, 0) → (s, 1) to meet the criteria above.

#### Construction

Construct G from P = (Q, Σ, Γ, δ, q_{0},
{q_{accept}}). The variables of G are {A_{pq} |
p, q ∈ Q} and the start variable is A_{q0,
qaccept}.

- For each p, q, r, s ∈ Q, u ∈ Γ and a,b ∈
Σ
_{ε}, if δ(p, a, ε) contains
(r, u) and δ(s, b, u) contains (q, ε), add
the following rule to G: A_{pq} → aA_{rs}b
- For each p, q, r, s ∈ Q, add the following rule to G:
A
_{pq} → A_{pr}A_{rq}
- For each p ∈ Q, add the following rule to G: A
_{pp} →
ε

#### Active Learning

- Consider the PDA in Figure 2.18. Show at least one resulting rule for each
of the three bullets above.