# Chapter 2: Context-Free Languages

## Chapter 2.1, Context-Free Grammars

### Problem 2.3 on page 155

Answer each part for the following context-free grammar G.

```R → XRX | S
S → aTb | bTa
T → XTX | X | ε
X → a | b
```
1. What are the variables of G?
2. What are the terminals of G?
3. What is the start variable of G?
4. Give three strings in L(G).
5. Give three strings not in L(G).
6. True or False. T ⇒ aba.
7. True or False. T ⇒ * aba.
8. True or False. T ⇒ T.
9. True or False. T ⇒ * T.
10. True or False. XXX ⇒ * aba.
11. True or False. X ⇒ * aba.
12. True or False. T ⇒ * XX.
13. True or False. T ⇒ * XXX.
14. True or False. S ⇒ * ε.
15. Give a description in English of L(G).

### Context-Free Grammar Definition

A context-free grammar is a 4-tuple (V, Σ, R, S) where

1. V is a finite set called the variables,
2. Σ is a finite set, disjoint from V, called the terminals,
3. R is a finite set of rules, with each rule being a variable and a string of variables and terminals, and
4. S ∈ V is the start variable.

### Yields Definition

uAv yields uwv, written uAv ⇒ uwv, if A → w is a rule of the grammar.

### Derives Definition

u derives v, written u ⇒* v, (1) if u = v or (2) if a sequence u1, u2, ... uk exists for k ≥ 0 where u ⇒ u1 ⇒ u2 ... uk ⇒ v.

### Language of the Grammar Definition

L(G) = {w ∈ Σ* | S ⇒ * w}

### Active Learning Problem

• Produce a DFA that recognizes A = {w | w contains either zero or one 0} over the binary alphabet.
• Construct a context-free grammar for language A, using the states of the DFA as the variables.

### Ambiguous Definition

A string w is derived ambiguously in context-free grammar G if it has two or more different leftmost derivations. Grammar G is ambiguous if it generates some string ambiguously.

### Active Learning Problem

Show that the following context-free grammar is ambiguous for the string aabbcc.

```S → WX | YZ
W → ε | aW
X → bXc | ε
Y → aYb | ε
Z → ε | cZ
```

### Chomsky Normal Form

On your own, understand how to convert a context-free grammar to Chomsky Normal Form. The procedure is described on pages 109-111.