Table of Contents

11. Closure properties for Context-Free Languages

11.0. Lexer Spec

Given the following specs, construct the lexer DFA as presented in Lecture 14:

Separate the following input strings into lexemes:

a) 010101

Solution a)

Solution a)

Although the entire string is matched by PAIRS and NO_CONSEC_ONE, PAIRS is defined first, thus it will be the first picked.
PAIRS “010101”

b) 1010101011

Solution b)

Solution b)

First we have a maximal match on “101010101” for regex NO_CONSEC_ONE. The remaining string, “1”, is matched by both ONES and NO_CONSEC_ONE, but ONES is defined first.
NO_CONSEC_ONE “101010101”
ONES “1”

c) 01110101001

Solution c)

Solution c)

PAIRS “01”
ONES “11”
NO_CONSEC_ONES “0101001”

d) 01010111111001010

Solution d)

Solution d)

PAIRS “010101”
ONES “11111”
NO_CONSEC_ONES “001010”

e) 1101101001111100001010011001

Solution e)

Solution e)

ONES “11” PAIRS “01101001”
ONES “1111”
NO_CONSEC_ONES “0000101001”
PAIRS “1001”

11.1. Not Closed under CFLs

11.1.1. Intersection is not a closure property.

Solution 11.1.1

Solution 11.1.1

Take:

$ L_1 = \{ a^{n} b^{n} c^{m} \ | \: n, m \in \mathbb{N} \} $

$ L_2 = \{ a^{m} b^{n} c^{n} \ | \: n, m \in \mathbb{N} \} $

$ \Rightarrow L_1 \bigcap L_2 = \{ a^{n} b^{n} c^{n} \ | \: n \in \mathbb{N} \} $ is not context free

11.1.2. Complement is not a closure property.

Solution 11.1.2

Solution 11.1.2

Suppose Complement is a closure property $\Rightarrow$ complements of CFLs are CFLs, we will prove in 11.2.2 that Union is a closure property.

Take, $ L_1, L_2 \in CFL \Rightarrow \overline{\overline{L_1} \bigcup \overline{L_2}} = L_1 \bigcap L_2 \in CFL $

This would imply $ L_1 \bigcap L_2 $ is a CFL, and at 11.1.1 we have proven that intersection is not a closure property.

Contradiction!

11.1.3. Difference is not a closure property.

Solution 11.1.3

Solution 11.1.3

Suppose Difference is a closure property.

Take, $ \Sigma^*, L_1 \in CFL \Rightarrow \Sigma^* \backslash L_1 = \overline{L_1}\in CFL $

This would imply $ \overline{L_1} $ is a CFL, and at 11.2.1 we have proven that intersection is not a closure property.

Contradiction!

11.2. Closed under CFLs

11.2.1. Concat is a closure property.

Solution 11.2.1.

Solution 11.2.1.

Let \( A \) and \( B \) be context-free languages over an alphabet \( \Sigma \), and let \( G_A = (V_A, \Sigma, R_A, S_A) \) and \( G_B = (V_B, \Sigma, R_B, S_B) \) be context-free grammars that generate \( A \) and \( B \) respectively. By renaming the variables if necessary, we assume that \( V_A \) is disjoint from \( V_B \), and that neither variable set contains the variable \( S \). We now construct a new CFG by writing:

\[ G = (V_A \cup V_B \cup \{S\}, \Sigma, R_A \cup R_B \cup \{S \to S_AS_B\}, S) \]

11.2.2. Union is a closure property.

Solution 11.2.2.

Solution 11.2.2.

Let \( A \) and \( B \) be context-free languages over an alphabet \( \Sigma \), and let \( G_A = (V_A, \Sigma, R_A, S_A) \) and \( G_B = (V_B, \Sigma, R_B, S_B) \) be context-free grammars that generate \( A \) and \( B \) respectively. By renaming the variables if necessary, we assume that \( V_A \) is disjoint from \( V_B \), and that neither variable set contains the variable \( S \). We now construct a new CFG by writing:

\[ G = (V_A \cup V_B \cup \{S\}, \Sigma, R_A \cup R_B \cup \{S \to S_A | S_B\}, S) \]

11.2.3. Kleene Star is a closure property.

Solution 11.2.3.

Solution 11.2.3.

Let \( A \) be a context-free languages over an alphabet \( \Sigma \), and let \( G_A = (V_A, \Sigma, R_A, S_A) \) be the context-free grammar that generate \( A \). We now construct a new CFG by writing:

\[ G = (V_A \cup \{S\}, \Sigma, R_A \cup \{S \to SS | S_A | \epsilon\}, S) \]

11.2.4. Reverse is a closure property.

Solution 11.2.4

Solution 11.2.4

Reverse each production rule in the grammar for L, with $ L \in CFL $.

Debug visual Solution 11.2.4

Debug visual Solution 11.2.4

If \( G \) is a grammar, let \( H \) be its reverse, so for production \( A \to w \) in \( G \) we have \( A \to w^R \) in \( H \).

Then by induction we show that the original grammar \( G \) generates a string iff the reverse grammar \( H \) generates the reverse of the string. Formally: \( A \xRightarrow{*}_G w \iff A \Rightarrow{*}_H w^R \).

1. Basis:

In zero steps, we have $ \( A \Rightarrow{0}_G A \iff A \Rightarrow{0}_H A \) $.

2. Induction: Assuming $ \( A \Rightarrow{*}_G w_1 B w_2 \iff A \Rightarrow{*}_H w_2^R B w_1^R \) $,

we can apply any production $ \( B \to u \) $ in \( G \) (and in \( H \) in reverse) and obtain:

$ \( A \rightarrow^{*}_G w_1 u w_2 \) $

$ \( A \rightarrow^{*}_H w_2^R u^R w_1^R \) $, where indeed $ \( w_2^R u^R w_1^R \) $ is the reverse of $ \( w_1 u w_2 \) $.

11.2.5. Intersection with a regular language is a closure property.

Solution 11.2.5.

Solution 11.2.5.

Let \( L_1 \) be a context-free language and \( P = (K_1, \Sigma, \Gamma, \Delta_1, q^{1}_{0}, F_1) \) be its respective PDA.

Let \( L_2 \) be a regular language and \( A = (K_2, \Sigma, \delta, q_{20}, F_2) \) be its respective DFA.

We build the following PDA \( (K, \Sigma, \Gamma, \Delta, q_0, F) \) where:

- \( K = K_1 \times K_2 \)

- \( q_0 = (q^{1}_{0}, q^{2}_{0}) \)

Transition rules for \( \Delta \):

1. \( ((q_1, q_2), c, \alpha, (q_1', q_2'), \beta) \in \Delta \) iff \( (q_1, c, \alpha, q_1', \beta) \in \Delta_1 \) and \( \delta(q_2, c) = q_2' \).

2. \( ((q_1, q_2), \epsilon, \alpha, (q_1', q_2), \beta) \in \Delta \) iff \( (q_1, \epsilon, \alpha, q_1', \beta) \in \Delta_1 \).

- \( F = F_1 \times F_2 \)

The PDA accepts \( L(P) \cap L(A) \).

11.2.6. Difference with a regular language is a closure property.

Solution 11.2.6.

Solution 11.2.6.

Let \( L_1 \) be a context-free language and \( L_2 \) a regular language.

$ \overline{L_2} $ is regular, as complement is a closure property for regular languages.

From 11.2.5, we know that the intersection is closed between CFLs and Regular Languages, and write $ L_1 \setminus L_2 = L_1 \cap \overline{L_2} $.