9. Context-Free Grammars

$ G = \{\Sigma, V, R, S \} $

S - Starting symbol

V - Set of terminals and non-terminals

R - production rules ($ R \subseteq (V \setminus \Sigma) \times V^n $)

Property: Chomsky Normal Form (CNF) - A grammar in CNF is context-free.

  1. all transitions are of the form:
    • $ X \leftarrow YZ $
    • $ X \leftarrow a $
    • $ S \leftarrow \epsilon $

S is the starting symbol.

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).

9.1.1. $ L = \{\: w \in \{a,b\}^* \ | \:w \text{ is a palindrome}\} $.

Solution 9.1.1

Solution 9.1.1

$ S \leftarrow aSa\ |\ bSb \ |\ a \ |\ b \ |\ \epsilon $

9.1.2. $ L = \{ a^{m} b^{m+n} c^{n} \ | \: n, m \geq 0 \} $

Solution 9.1.2

Solution 9.1.2

$ S \leftarrow AB $
$ A \leftarrow aAb\ |\ \ \epsilon $
$ B \leftarrow bBc\ |\ \ \epsilon $

9.1.3. $ L = \{w \in \{a, b\}^* | \#_a(w) = \#_b(w) \} $

Solution 9.1.3

Solution 9.1.3

$ S \leftarrow aBS\ |\ bAS\ |\ \epsilon $
$ A \leftarrow a\ |\ bAA $
$ B \leftarrow b\ |\ aBB $

9.1.4. $ L = \{w \in \{a, b\}^* | \#_a(w) \neq \#_b(w) \} $

9.1.5. $ L = \{a^ib^jc^k | i = j \lor j = k \} $

Solution 9.1.5

Solution 9.1.5

$ S \leftarrow C\ |\ A $
$ C \leftarrow Cc\ |\ B $
$ B \leftarrow aBb\ |\ \ \epsilon $
$ A \leftarrow aA\ |\ D $
$ D \leftarrow bDc\ |\ \ \epsilon $

Which of the following grammars are ambiguous? Justify each answer. Modify the grammar to remove ambiguity, wherever the case.

9.2.1.

$ S \leftarrow aA\ |\ A $
$ A \leftarrow aA\ |\ B $
$ B \leftarrow bB\ |\ \epsilon $

Solution 9.2.1

Solution 9.2.1

$ S \leftarrow A $
$ A \leftarrow aA\ |\ B $
$ B \leftarrow bB\ |\ \epsilon $

9.2.2.

$ S \leftarrow AS\ |\ \epsilon $
$ A \leftarrow 0A1\ |\ 01\ |\ B $
$ B \leftarrow B1\ |\ \epsilon $

Solution 9.2.2

Solution 9.2.2

$ S \leftarrow AS\ |\ A $
$ A \leftarrow 0A1\ |\ B $
$ B \leftarrow B1\ |\ \epsilon $

9.2.3.

$ S \leftarrow ASB\ |\ BSA\ |\ \epsilon $
$ A \leftarrow aA\ |\ \epsilon $
$ B \leftarrow bB\ |\ \epsilon $

Solution 9.2.3

Solution 9.2.3

$ S \leftarrow aS\ |\ bS\ |\ \epsilon $

9.2.4. Write an ambiguous grammar for $ L(a^*) $.

Solution 9.2.4

Solution 9.2.4

$ S \leftarrow aS \ |\ A\ |\ \epsilon $
$ A \leftarrow aA\ |\ \epsilon $

Definition: A regular grammar is a CF grammar where the production rules follow one of these 2 patterns:

  1. all of them are of the form:
    • $ X \leftarrow aY $
    • $ X \leftarrow Y $
    • $ X \leftarrow a $
    • $ X \leftarrow \epsilon $
  2. 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:

$ X \leftarrow 0X\ |\ 1Y $

$ Y \leftarrow 1Y\ |\ 0\ |\ \epsilon $

Solution 9.3.1

Solution 9.3.1

Y produces $ 1^* (0 | \epsilon) $

X produces $ 0^* 1Y \rightarrow 0^* 1^+ (0 | \epsilon) $

9.4.1. Prove that the languages accepted by regular grammars are exactly the regular languages

  • 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) $
  • 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) $
  • 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;
  • Hint 2: use induction in the proofs