Edit this page Backlinks This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== 11. Context-Free Grammars AGAIN ====== ===== 11.1. Chomsky Normal Form ===== **11.1.1** Write the following grammars in CNF: - \\ $math[S \leftarrow 0SA \mid ASB \\ A \leftarrow 0BA \mid 1S \mid 0A \\ B \leftarrow B1 \mid 0B \mid 1 \mid 0 ] - \\ $ S \leftarrow ABC \\ A \leftarrow aAb \mid \epsilon \\ B \leftarrow bBc \mid bc \\ C \leftarrow cC \mid c $ **11.1.2** Write a CNF grammar for $math[L(b^*(a \cup c)^+b^+)]. Un pic prea stufos si cu prea multa munca //manuala//. Eu l-as scoate. ===== 11.2. Regular Grammars ===== **11.2.1** Give an example of a regular grammar that generates $ L(0^*1^*) $. **11.2.2** Give an example of a regular grammar that generates $ L((0 \cup 10(1^*00 \cup \epsilon)1(01 \cup 0^+)^*10^+)) $. Un pic prea stufos si cu prea multa munca //manuala//. Eu l-as scoate. ===== 11.3. DFA to Regular Grammar ===== **11.3.1** For each of the following DFAs, algorithmically create a regular grammar that generates the same language. - {{:lfa:2022:lfa2022_lab2_ex4.png?200|}} - {{:lfa:2022:lfa2022_lab6_mindfa2_1.png?200|}} - {{:lfa:2022:lfa2022_lab5_ex2_4_cerinta.png?250|}} Un pic prea stufos si cu prea multa munca //manuala//. As scoate ultimele 2 exercitii. ===== 11.4. Short Exercises ===== **11.4.1** Can a regular Grammar be in Chomsky Normal Form? \\ <hidden>No.</hidden> \\ **11.4.2** Write an algorithm that verifies whether or not a Regular Grammar generates an infinite language. \\ <hidden> Create a graph $math[G=(V,E)] where the nodes are **terminals and non-terminals**. For each rule of the form $math[X \leftarrow aY] create a directed edge $math[(X,Y)]. For each rule $math[X \leftarrow a], also generate an edge $math[(X,a)]. - First, we need to check first if the grammar generates a language different from $math[\emptyset]. So we check if there is a path from the start symbol to some terminal. - Second, we need to check if there are //loops//. It suffices to check if the graph is a tree. </hidden> **11.4.3.** Prove that any DFA can be converted to a regular grammar. <hidden> See lecture</hidden> **11.4.5.** Is there a decidable algorithm to remove ambiguity from regular grammars? <hidden> A regular grammar $math[G] is ambiguous iff there exists some word $math[w] which is obtained by two different left-most derivation which are necessarily of the form $math[S \implies \alpha_1 X_1 \implies \alpha_2 X_2 \ldots \implies \alpha_n] where each $math[X_i] is a non-terminal and $math[\alpha_i] are characters or $math[\epsilon]. In the corresponding NFA for $math[G] this amounts to having two different configuration //chains// that start from $math[(q_0,w)] and end up in a final state, //eating// the word w. We can determinize the NFA. In the resulting DFA (in any DFA for that matter), it is not possible to derive the same word using two different transition sequences (each state and character uniquely determines the next-state). Hence, if we convert this DFA back to a regular grammar, we will get an unambiguous one. </hidden> **11.4.6.** Show the following grammar is ambiguous: $math[S \leftarrow aSbS | bSaS | epsilon]. Write a non-ambiguous equivalent. <hidden> The grammar is indeed ambiguous: $math[S \Rightarrow aSbS \Rightarrow abS \Rightarrow abaSbS \Rightarrow ^* abab]. And also $math[S \Rightarrow aSbS \Rightarrow abSaSbS \Rightarrow^* abba] This grammar generates $math[\{ w \in \{0,1\}^* \mid \#_A(w) = \#_B(w) \}]. See the solution from the previous lab. See also [[https://cs.stackexchange.com/questions/64569/unambiguous-grammar-that-produce-equal-number-of-a-and-b | this]] for more details. </hidden> **//The exercises below may be a little to basic for this stage of the lecture. I would remove them//** **11.4.3** When can a PDA be turned into a regular grammar? \\ <hidden>When the PDA accepts a regular language</hidden> \\ **11.4.4** Given a language $ L $ generated by a regular grammar, can we convert it into a regular expression that accepts $ \overline{L} $ ? \\ <hidden>Yes. \\ regular grammar -> NFA -> DFA -> complement(DFA) -> regular grammar</hidden> \\ **11.4.5** Let $ e $ be a regular expression. Is it possible to create a context-free grammar that generates $ L(\overline{e}) $? \\ <hidden>Yes. \\ regular grammar -> DFA -> complement(DFA) -> regular grammar => context-free</hidden>