# Chapter 2: Context-Free Languages

## For Your Enjoyment

## 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

- What are the variables of G?
- What are the terminals of G?
- What is the start variable of G?
- Give three strings in L(G).
- Give three strings not in L(G).
- True or False. T ⇒ aba.
- True or False. T ⇒
^{*} aba.
- True or False. T ⇒ T.
- True or False. T ⇒
^{*} T.
- True or False. XXX ⇒
^{*} aba.
- True or False. X ⇒
^{*} aba.
- True or False. T ⇒
^{*} XX.
- True or False. T ⇒
^{*} XXX.
- True or False. S ⇒
^{*} ε.
- Give a description in English of L(G).

### Context-Free Grammar Definition

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

- V is a finite set called the
**variables**,
- Σ is a finite set, disjoint from V, called the
**terminals**,
- R is a finite set of
**rules**, with each rule being a variable and
a string of variables and terminals, and
- 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 u_{1}, u_{2}, ... u_{k} exists
for k ≥ 0 where u ⇒ u_{1} ⇒ u_{2} ...
u_{k} ⇒ 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.