Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
|
lfa:2025:lab10 [2026/01/12 08:16] cata_chiru created |
lfa:2025:lab10 [2026/01/20 11:49] (current) cata_chiru |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== 9. Context-Free Grammars ====== | + | ====== 10. Closure properties for Context-Free Languages ====== |
| - | ===== 9.1. Generating a CF language ===== | + | Analogy with Subtraction, Division between $ \mathbb{Z} $ and $ \mathbb{N} $. |
| - | Write CF grammar for the following languages. For each grammar, make sure it is not ambiguous. (Start with any CF grammar that accepts L. Then write another non-ambiguous grammar for the same language). | + | ===== 10.0. Lexer Spec ===== |
| + | Given the following specs, construct the lexer DFA as presented in Lecture 14: | ||
| + | * PAIRS: $ (10 | 01)* $ | ||
| + | * ONES: $ 1+ $ | ||
| + | * NO_CONSEC_ONE: $ (1 | \epsilon)(01 | 0)* $ | ||
| + | Separate the following input strings into lexemes: | ||
| + | a) 010101 | ||
| - | **9.1.1.** $ L = \{\: w \in \{a,b\}^* \ | \:w \text{ is a palindrome}\} $. | + | <hidden 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" | ||
| - | <hidden Solution 9.1.1> | + | </hidden> |
| - | $ S \leftarrow aSa\ |\ bSb \ |\ a \ |\ b \ |\ \epsilon $ \\ | + | b) 1010101011 |
| + | <hidden 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" | ||
| </hidden> | </hidden> | ||
| - | **9.1.2.** $ L = \{ a^{m} b^{m+n} c^{n} \ | \: n, m \geq 0 \} $ | ||
| - | |||
| - | <hidden Solution 9.1.2> | ||
| - | $ S \leftarrow AB $ \\ | + | c) 01110101001 |
| - | $ A \leftarrow aAb\ |\ \ \epsilon $ \\ | + | |
| - | $ B \leftarrow bBc\ |\ \ \epsilon $ \\ | + | |
| + | <hidden Solution c)> | ||
| + | PAIRS "01" \\ | ||
| + | ONES "11" \\ | ||
| + | NO_CONSEC_ONES "0101001" | ||
| </hidden> | </hidden> | ||
| - | **9.1.3.** $ L = \{w \in \{a, b\}^* | \#_a(w) = \#_b(w) \} $ | ||
| - | <hidden Solution 9.1.3> | + | d) 01010111111001010 |
| - | $ S \leftarrow aBS\ |\ bAS\ |\ \epsilon $ \\ | + | <hidden Solution d)> |
| - | $ A \leftarrow a\ |\ bAA $ \\ | + | PAIRS "010101" \\ |
| - | $ B \leftarrow b\ |\ aBB $ \\ | + | ONES "11111" \\ |
| + | NO_CONSEC_ONES "001010" | ||
| + | </hidden> | ||
| + | |||
| + | e) 1101101001111100001010011001 | ||
| + | | ||
| + | <hidden Solution e)> | ||
| + | ONES "11" | ||
| + | PAIRS "01101001" \\ | ||
| + | ONES "1111" \\ | ||
| + | NO_CONSEC_ONES "0000101001" \\ | ||
| + | PAIRS "1001" | ||
| </hidden> | </hidden> | ||
| - | **9.1.4.** $ L = \{w \in \{a, b\}^* | \#_a(w) \neq \#_b(w) \} $ | + | ===== 10.1. Not Closed under CFLs ===== |
| + | **10.1.1.** Intersection is not a closure property. | ||
| - | **9.1.5.** $ L = \{a^ib^jc^k | i = j \lor j = k \} $ \\ | + | <hidden Solution 11.1.1 > |
| + | Take: | ||
| - | <hidden Solution 9.1.5> | + | $ L_1 = \{ a^{n} b^{n} c^{m} \ | \: n, m \in \mathbb{N} \} $ |
| - | $ S \leftarrow C\ |\ A $ \\ | + | $ L_2 = \{ a^{m} b^{n} c^{n} \ | \: n, m \in \mathbb{N} \} $ |
| - | $ C \leftarrow Cc\ |\ B $ \\ | + | |
| - | $ B \leftarrow aBb\ |\ \ \epsilon $ \\ | + | $ \Rightarrow L_1 \bigcap L_2 = \{ a^{n} b^{n} c^{n} \ | \: n \in \mathbb{N} \} $ is not context free |
| - | $ A \leftarrow aA\ |\ D $ \\ | + | |
| - | $ D \leftarrow bDc\ |\ \ \epsilon $ \\ | + | |
| </hidden> | </hidden> | ||
| + | **10.1.2.** Complement is not a closure property. | ||
| - | ===== 9.2. Ambiguity ===== | + | <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. | ||
| - | Which of the following grammars are ambiguous? Justify each answer. Modify the grammar to remove ambiguity, wherever the case. | + | Take, |
| + | $ L_1, L_2 \in CFL \Rightarrow \overline{\overline{L_1} \bigcup \overline{L_2}} = L_1 \bigcap L_2 \in CFL $ | ||
| - | **9.2.1.** | + | 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. |
| - | $ S \leftarrow aA\ |\ A $ \\ | + | ** Contradiction! ** |
| - | $ A \leftarrow aA\ |\ B $ \\ | + | </hidden> |
| - | $ B \leftarrow bB\ |\ \epsilon $ | + | |
| + | **10.1.3.** Difference is not a closure property. | ||
| + | |||
| + | <hidden Solution 10.1.3 > | ||
| + | Suppose Difference is a closure property. | ||
| - | <hidden Solution 9.2.1> | + | Take, |
| + | $ \Sigma^*, L_1 \in CFL \Rightarrow \Sigma^* \backslash L_1 = \overline{L_1}\in CFL $ | ||
| - | $ S \leftarrow A $ \\ | + | This would imply $ \overline{L_1} $ is a CFL, and at 11.2.1 we have proven that intersection is not a closure property. |
| - | $ A \leftarrow aA\ |\ B $ \\ | + | |
| - | $ B \leftarrow bB\ |\ \epsilon $ | + | |
| + | ** Contradiction! ** | ||
| </hidden> | </hidden> | ||
| - | **9.2.2.** | ||
| - | $ S \leftarrow AS\ |\ \epsilon $ \\ | + | ===== 10.2. Closed under CFLs ===== |
| - | $ A \leftarrow 0A1\ |\ 01\ |\ B $\\ | + | |
| - | $ B \leftarrow B1\ |\ \epsilon $ | + | |
| - | <hidden Solution 9.2.2> | + | **10.2.1.** Concat is a closure property. |
| - | $ S \leftarrow AS\ |\ A $ \\ | + | <hidden Solution 10.2.1.> |
| - | $ A \leftarrow 0A1\ |\ B $\\ | + | 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: |
| - | $ B \leftarrow B1\ |\ \epsilon $ | + | |
| + | \[ | ||
| + | G = (V_A \cup V_B \cup \{S\}, \Sigma, R_A \cup R_B \cup \{S \to S_AS_B\}, S) | ||
| + | \] | ||
| </hidden> | </hidden> | ||
| - | **9.2.3.** | ||
| - | $ S \leftarrow ASB\ |\ BSA\ |\ \epsilon $\\ | + | **10.2.2.** Union is a closure property. |
| - | $ A \leftarrow aA\ |\ \epsilon $\\ | + | |
| - | $ B \leftarrow bB\ |\ \epsilon $ | + | |
| - | <hidden Solution 9.2.3> | + | <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: | ||
| - | $ S \leftarrow aS\ |\ bS\ |\ \epsilon $ \\ | + | \[ |
| + | G = (V_A \cup V_B \cup \{S\}, \Sigma, R_A \cup R_B \cup \{S \to S_A | S_B\}, S) | ||
| + | \] | ||
| </hidden> | </hidden> | ||
| + | **10.2.3.** Kleene Star is a closure property. | ||
| + | <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: | ||
| - | **9.2.4.** Write an ambiguous grammar for $ L(a^*) $. | + | \[ |
| + | G = (V_A \cup \{S\}, \Sigma, R_A \cup \{S \to SS | S_A | \epsilon\}, S) | ||
| + | \] | ||
| - | <hidden Solution 9.2.4> | + | </hidden> |
| + | |||
| + | |||
| + | **10.2.4.** Reverse is a closure property. | ||
| + | |||
| + | <hidden Solution 10.2.4> | ||
| + | |||
| + | __**Intuition**__ | ||
| + | |||
| + | Reverse each production rule in the grammar for L, with $ L \in CFL $. | ||
| + | |||
| + | __**Formal solution - induction**__ | ||
| + | |||
| + | 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 \to^{*}_G w \iff A \to^{*}_H w^R \). | ||
| + | |||
| + | 1. **Basis**: | ||
| + | |||
| + | In zero steps, we have \( A \to^{0}_G A \iff A \to^{0}_H A \). | ||
| + | |||
| + | 2. **Induction**: | ||
| + | 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: | ||
| + | |||
| + | \( A \to^{*}_G w_1 u w_2 \) | ||
| - | $ S \leftarrow aS \ |\ A\ |\ \epsilon $\\ | + | \( 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 \leftarrow aA\ |\ \epsilon $\\ | + | |
| </hidden> | </hidden> | ||
| - | ===== 9.3 Regular Grammars ===== | ||
| - | **Definition:** A regular grammar is a CF grammar where the production rules follow one of these 2 patterns: | + | **10.2.5.** Intersection with a regular language is a closure property. |
| - | - all of them are of the form: | + | |
| - | * $ X \leftarrow aY $ | + | |
| - | * $ X \leftarrow Y $ | + | |
| - | * $ X \leftarrow a $ | + | |
| - | * $ X \leftarrow \epsilon $ | + | |
| - | - OR all of them are of the form: | + | |
| - | * $ X \leftarrow Ya $ | + | |
| - | * $ X \leftarrow Y $ | + | |
| - | * $ X \leftarrow a $ | + | |
| - | * $ X \leftarrow \epsilon $ | + | |
| - | **9.3.1.** (warmup) Find a DFA or an NFA that accepts the language generated by the following regular grammar: | + | <hidden Solution 10.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. | ||
| - | $ X \leftarrow 0X\ |\ 1Y $ | + | Let \( L_2 \) be a regular language and \( A = (K_2, \Sigma, \delta, q_{20}, F_2) \) be its respective DFA. |
| - | $ Y \leftarrow 1Y\ |\ 0\ |\ \epsilon $ | + | We build the following PDA \( (K, \Sigma, \Gamma, \Delta, q_0, F) \) where: |
| - | <hidden Solution 9.3.1> | + | - \( K = K_1 \times K_2 \) |
| - | Y produces $ 1^* (0 | \epsilon) $ | + | - \( q_0 = (q^{1}_{0}, q^{2}_{0}) \) |
| - | X produces $ 0^* 1Y \rightarrow 0^* 1^+ (0 | \epsilon) $ | + | 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) \). | ||
| </hidden> | </hidden> | ||
| - | **9.4.1.** Prove that the languages accepted by regular grammars are exactly the regular languages | + | **10.2.6.** Difference with a regular language is a closure property. |
| - | * prove that any regular grammar generates a regular language: **start** from a regular grammar G and **construct** an NFA $ N $ such that $ L(N) = L(G) $ | + | <hidden Solution 10.2.6.> |
| - | * prove that any regular language is generated by a regular grammar: **start** from a DFA $ A $ and **construct** a regular grammar such that $ L(G) = L(A) $ | + | Let \( L_1 \) be a context-free language and \( L_2 \) a regular language. |
| - | * write the proofs for both definitions (the one with $ X \leftarrow aY $ and the one with $ X \leftarrow Ya $). | + | |
| - | * **Hint**: for the first definition, think top-down; for the second one, think bottom-up; | + | $ \overline{L_2} $ is regular, as complement is a closure property for regular languages. |
| - | * **Hint 2**: use induction in the proofs | + | |
| + | 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> | ||