You are on page 1of 2

CSE 30151 Spring 2014 Homework 4 Due at 2:00pm on March 18, 2014

1. Context-free grammars. Give CFGs generating each of the following languages. In all cases, i, j, k 0. (a) {x {a, b} | x starts and ends with the same symbol} (b) {ai bj ck | i = j or j = k } (c) {ai bj ck | i = j + k } (d) {ai bj ck | i = j + k } (e) {ai bj ck | i < j or i > k } 2. Applications of CFGs. Find a CFG generating the language of all regular expressions over the alphabet = {0, 1} (including the R+ operation is not necessary): (a) if the denition of regular expression is interpreted strictly with regard to parentheses (fully parenthesized), for example, ((((01) ) 0)(0 )) (as specied on page 64 of the textbook); (b) if the denition is interpreted so as to allow regular expressions that are not fully parenthesized, for example, (01 111) 0. In this case, any grammar is sucient even if it is ambiguous and doesnt properly enforce the precedence order of the operations. Be careful to distinguish between -substitution rules and substitution rules whose right side is the symbol appearing in a regular expression; use E in the second case. 3. Normal forms. Convert the following grammars to Chomsky normal form: (a) S AB | (b) S aSa | B B bbC | bb C cC | 4. Properties of CFGs. (a) A variable A in a CFG G = (V, , R, S ) is reachable if a string that contains it can be derived from the start variable S . In other words, S uAw, where denotes multiple derivation steps and u, w are strings consisting of variables and terminals (i.e., u, w (V ) ). Give an algorithm for nding all reachable variables in G. (Hint: the algorithm is similar to computing reachable states in a DFA.) (b) A variable A in a CFG G = (V, , R, S ) is live if a string consisting of terminals only can be derived from it. In other words, A x for some x . Give an algorithm for nding all live variables in G. (c) Extra credit. A variable A in a CFG G = (V, , R, S ) is useful if for some string x , there is a derivation of x that takes the form S uAw x (i.e., it is both reachable and live). A variable that is not useful is useless. If a grammar has useless variables, it is possible to construct an equivalent grammar with no useless variables. A aASb | a B bS

Give an algorithm for doing so and nd equivalent CFGs with no useless variables for the grammars below: S ABC | BaB A aA | BaC | aaa A aAb | bAa | a B bBb | a C CA | AC


B bbA | aaB | AB

C abCa | aDb

D bD | aC