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.2. Regular Grammars ===== **11.2.1** Give an example of a regular grammar that generates $ L(0^*1^*) $. ===== 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|}} ===== 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.4.** 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.5.** 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>