Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
lfa:2025:lab10 [2026/01/18 17:41] cata_chiru |
lfa:2025:lab10 [2026/01/20 11:49] (current) cata_chiru |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== 11. Closure properties for Context-Free Languages ====== | + | ====== 10. Closure properties for Context-Free Languages ====== |
| - | ===== 11.0. Lexer Spec ===== | + | Analogy with Subtraction, Division between $ \mathbb{Z} $ and $ \mathbb{N} $. |
| + | |||
| + | ===== 10.0. Lexer Spec ===== | ||
| Given the following specs, construct the lexer DFA as presented in Lecture 14: | Given the following specs, construct the lexer DFA as presented in Lecture 14: | ||
| * PAIRS: $ (10 | 01)* $ | * PAIRS: $ (10 | 01)* $ | ||
| Line 52: | Line 54: | ||
| - | ===== 11.1. Not Closed under CFLs ===== | + | ===== 10.1. Not Closed under CFLs ===== |
| - | **11.1.1.** Intersection is not a closure property. | + | **10.1.1.** Intersection is not a closure property. |
| <hidden Solution 11.1.1 > | <hidden Solution 11.1.1 > | ||
| Line 67: | Line 69: | ||
| </hidden> | </hidden> | ||
| - | **11.1.2.** Complement is not a closure property. | + | **10.1.2.** Complement is not a closure property. |
| - | <hidden Solution 11.1.2 > | + | <hidden Solution 10.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. | 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. | ||
| Line 80: | Line 82: | ||
| </hidden> | </hidden> | ||
| - | **11.1.3.** Difference is not a closure property. | + | **10.1.3.** Difference is not a closure property. |
| - | <hidden Solution 11.1.3 > | + | <hidden Solution 10.1.3 > |
| Suppose Difference is a closure property. | Suppose Difference is a closure property. | ||
| Line 94: | Line 96: | ||
| - | ===== 11.2. Closed under CFLs ===== | + | ===== 10.2. Closed under CFLs ===== |
| - | **11.2.1.** Concat is a closure property. | + | **10.2.1.** Concat is a closure property. |
| - | <hidden Solution 11.2.1.> | + | <hidden Solution 10.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: | 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: | ||
| Line 109: | Line 111: | ||
| - | **11.2.2.** Union is a closure property. | + | **10.2.2.** Union is a closure property. |
| - | <hidden Solution 11.2.2.> | + | <hidden Solution 10.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: | 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: | ||
| Line 120: | Line 122: | ||
| </hidden> | </hidden> | ||
| - | **11.2.3.** Kleene Star is a closure property. | + | **10.2.3.** Kleene Star is a closure property. |
| - | <hidden Solution 11.2.3.> | + | <hidden Solution 10.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: | 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: | ||
| Line 132: | Line 134: | ||
| - | **11.2.4.** Reverse is a closure property. | + | **10.2.4.** Reverse is a closure property. |
| - | <hidden Solution 11.2.4> | + | <hidden Solution 10.2.4> |
| + | |||
| + | __**Intuition**__ | ||
| Reverse each production rule in the grammar for L, with $ L \in CFL $. | Reverse each production rule in the grammar for L, with $ L \in CFL $. | ||
| - | </hidden> | + | __**Formal solution - induction**__ |
| - | <hidden 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 \). |
| - | If $\left( G \right$ 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: | + | 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 \) $. | + | \( A \to^{*}_G w \iff A \to^{*}_H w^R \). |
| 1. **Basis**: | 1. **Basis**: | ||
| - | In zero steps, we have $ \( A \Rightarrow{0}_G A \iff A \Rightarrow{0}_H A \) $. | + | In zero steps, we have \( A \to^{0}_G A \iff A \to^{0}_H A \). |
| 2. **Induction**: | 2. **Induction**: | ||
| - | Assuming $ \( A \Rightarrow{*}_G w_1 B w_2 \iff A \Rightarrow{*}_H w_2^R B w_1^R \) $, | + | Assuming \( A \to^{*}_G w_1 B w_2 \iff A \to^{*}_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: | + | 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 \to^{*}_G w_1 u w_2 \) |
| + | |||
| + | \( A \to^{*}_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 \). | ||
| - | $ \( 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 \) $. | ||
| </hidden> | </hidden> | ||
| + | **10.2.5.** Intersection with a regular language is a closure property. | ||
| - | + | <hidden Solution 10.2.5.> | |
| - | + | ||
| - | **11.2.5.** Intersection with a regular language is a closure property. | + | |
| - | + | ||
| - | <hidden 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) \) | 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. | be its respective PDA. | ||
| Line 190: | Line 191: | ||
| </hidden> | </hidden> | ||
| - | **11.2.6.** Difference with a regular language is a closure property. | + | **10.2.6.** Difference with a regular language is a closure property. |
| - | <hidden Solution 11.2.6.> | + | <hidden Solution 10.2.6.> |
| Let \( L_1 \) be a context-free language and \( L_2 \) a regular language. | 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. | $ \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} $. | + | From 10.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} $. |
| </hidden> | </hidden> | ||