This is an old revision of the document!


11. Context-Free Grammars AGAIN

11.1.1 Write the following grammars in CNF:


  1. $ S \leftarrow 0SA \mid ASB \\ A \leftarrow 0BA \mid 1S \mid 0A \\ B \leftarrow B1 \mid 0B \mid 1 \mid 0 $

  2. $ 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 $ L(b^*(a \cup c)^+b^+)$ . Un pic prea stufos si cu prea multa munca manuala. Eu l-as scoate.

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.1 For each of the following DFAs, algorithmically create a regular grammar that generates the same language.

Un pic prea stufos si cu prea multa munca manuala. As scoate ultimele 2 exercitii.

11.4.1 Can a regular Grammar be in Chomsky Normal Form?

Click to display ⇲

Click to hide ⇱

No.


11.4.2 Write an algorithm that verifies whether or not a Regular Grammar generates an infinite language.

Click to display ⇲

Click to hide ⇱

Create a graph $ G=(V,E)$ where the nodes are terminals and non-terminals. For each rule of the form $ X \leftarrow aY$ create a directed edge $ (X,Y)$ . For each rule $ X \leftarrow a$ , also generate an edge $ (X,a)$ . - First, we need to check first if the grammar generates a language different from $ \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.

11.4.3. Prove that any DFA can be converted to a regular grammar.

Click to display ⇲

Click to hide ⇱

See lecture

11.4.5. Is there a decidable algorithm to remove ambiguity from regular grammars?

Click to display ⇲

Click to hide ⇱

A regular grammar $ G$ is ambiguous iff there exists some word $ w$ which is obtained by two different left-most derivation which are necessarily of the form $ S \implies \alpha_1 X_1 \implies \alpha_2 X_2 \ldots \implies \alpha_n$ where each $ X_i$ is a non-terminal and $ \alpha_i$ are characters or $ \epsilon$ . In the corresponding NFA for $ G$ this amounts to having two different configuration chains that start from $ (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.

11.4.6. Show the following grammar is ambiguous: $ S \leftarrow aSbS $ . Write a non-ambiguous equivalent.

Click to display ⇲

Click to hide ⇱

The grammar is indeed ambiguous: $ S \leftarrow aSbS \leftarrow abS \leftarrow abaSbS \leftarrow^* abab$ . And also $ S \leftarrow aSbS \leftarrow abSaSbS \leftarrow^* abba$ This grammar generates $ \{w\in \{0,1\}^* \mid #_A(w) = #_B(w)\}$ . See the solution from the previous lab. See also this for more details.

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?

Click to display ⇲

Click to hide ⇱

When the PDA accepts a regular language


11.4.4 Given a language $ L $ generated by a regular grammar, can we convert it into a regular expression that accepts $ \overline{L} $ ?

Click to display ⇲

Click to hide ⇱

Yes.
regular grammar → NFA → DFA → complement(DFA) → regular grammar


11.4.5 Let $ e $ be a regular expression. Is it possible to create a context-free grammar that generates $ L(\overline{e}) $?

Click to display ⇲

Click to hide ⇱

Yes.
regular grammar → DFA → complement(DFA) → regular grammar ⇒ context-free