P. 1
hw4

# hw4

|Views: 15|Likes:
hw4
hw4

See more
See less

03/17/2014

pdf

text

original

# 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 deﬁnition of regular expression is interpreted strictly with regard to parentheses (fully parenthesized), for example, ((((01)∗ ) ∪ 0)(0 ∪ ε)) (as speciﬁed on page 64 of the textbook); (b) if the deﬁnition is interpreted so as to allow regular expressions that are not fully parenthesized, for example, (01 ∪ 111)∗ ∪ 0. In this case, any grammar is suﬃcient even if it is ambiguous and doesn’t 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

1

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 S → AB | AC B → bbA | aaB | AB C → abCa | aDb D → bD | aC 2 .

scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->