CMPS 260 Spring 2024
HW #5: Context-free Grammars/Languages; Chomsky Normal Form
Due: April 5

1. Let G1 be the following context-free grammar:

S aS  |  aHb    (1) (2)
H aaHb  |  a    (3) (4)

(a) Offer a precise description of L(G1), the language generated by G1.

(b) Provide some evidence for the claim that G1 is unambiguous. To do so, choose a "generic" member of L(G1), describe its derivation from S, and argue that there is no other way to derive it from S.

(c) Present an alternative CFG G'1 that generates the same language as G1 but whose only nonterminal symbol is S. Demonstrate that your grammar is ambiguous.


2. Let G2 be the following context-free grammar:

S HS  |  K    (1) (2)
H aHb  |  c    (3) (4)
K cK  |  c    (5) (6)

Give a precise description of L(G2), the language generated by G2.


3. Present a context-free grammar G3 that generates the language { aj bk c2k bj  |  j≥1, k≥0 }

Small Bonus: Present a context-free grammar G'3 that generates the language { aj bk c2k d* bj  |  j≥1, k≥0 }

The star/asterisk superscript means, in effect, zero or more occurrences, just as in regular expressions.


4. Present a context-free grammar G4 that generates the language { aj bj+k ak  |  j≥1, k≥0 }

Small Bonus: Present a context-free grammar G'4 that generates the language { aj c* bj+k ak  |  j≥1, k≥0 }

The star/asterisk superscript means, in effect, zero or more occurrences, just as in regular expressions.


5. Consider the following Chomsky Normal Form grammar G5:

S HS  |  a  |  c    (1) (2)
H KH  |  b    (3) (4)
K KS  |  b    (5) (6)

For each of the strings bbaba and babab, use the CYK algorithm to determine whether it is generated by G5.

Specifically, for each string, fill in the cells of the matrix pictured below so that the cell in row i and column j contains Vi,j = { X ∈ {S,H,K} : X ⟹+ wi,j }, where wi,j is the substring of w (i.e., the string of interest) beginning with its i-th symbol and ending with its j-th symbol (inclusive). (Your answers are expected to include not only the answer to the question "Is w ∈ L(G)?" but also the correctly filled in table.)

Recall that, for i satisfying 1≤i≤|w|, X ∈ Vi,i iff X → wi,i is a production in the grammar. Meanwhile, for i and j satisfying 1≤i<j≤|w|, X ∈ Vi,j iff there exists k, where i≤k<j, and nonterminals Y and Z such that Y ∈ Vi,k, Z ∈ Vk+1,j, and X → YZ is a production in the grammar.

   1      2      3      4      5
+------+------+------+------+------+
|      |      |      |      |      |
|      |      |      |      |      | 1
|      |      |      |      |      |
+------+------+------+------+------+
       |      |      |      |      |
       |      |      |      |      | 2
       |      |      |      |      |
       +------+------+------+------+
              |      |      |      |
              |      |      |      | 3
              |      |      |      |
              +------+------+------+
                     |      |      |
                     |      |      | 4
                     |      |      |
                     +------+------+
                            |      |
                            |      | 5
                            |      |
                            +------+

If you prefer it, you can turn the matrix upside down so that the column numbers decrease as you go down the page.


6. Use the standard construction to obtain a Chomsky Normal Form grammar that is equivalent to (i.e., generates the same language as) the context-free grammar shown below. Present the resultant CNF grammar.

S bHaH  |  c
H SaH | baa


7. A linear grammar is a CFG in which every production's right-hand side contains at most one nonterminal symbol. A right-linear grammar is a linear grammar in which any such occurrence of a nonterminal symbol must be the last (i.e., rightmost) symbol of that right-hand side.

(a) Describe a construction that, given a right-linear grammar GR and a linear grammar GLin, produces a linear grammar G such that L(G) = L(GR) · L(GLin).

(b) Apply your construction to these particular grammars and show the resulting linear grammar.

SRaaK | ca
H ⟶cK | λ
K ⟶abSR | bH
SLincaAdc | Ab
A ⟶dcSLind | a
GR GLin