Assignment 4
Assignment 4
Assignment # 4
2. Find a CFG that generates the language: L(G) = { a n bm cm d2n | n ≥ 0, m > 0}.
Ans: S-> bSc | aSD D->dd
b. {w | |w| is odd}
Ans: S -> aSa | bSb | a | b
Ans: The grammar is ambiguous because we can find strings which have multiple
derivations:
Ans:
S=> 0A=> 00AA=> 001S1=> 0011B1=> 001101
S=> 0A=> 00AA=> 0011S=> 00110A=> 001101
Ans: E ⇒ E + T ⇒ T + T ⇒ F + T ⇒ a + T ⇒ a + F ⇒ a + a
a. (a+a)
Ans: E ⇒ E + T ⇒ E + T + T ⇒ T + T + T ⇒ F + T + T ⇒ a + T + T ⇒ a + F + T ⇒ a + a + T
b. a+a+a
⇒a+a+F⇒a+a+a
c. ((a))
Ans: E ⇒ T ⇒ F ⇒ (E) ⇒ (T) ⇒ (F) ⇒ ((E)) ⇒ ((T)) ⇒ ((F)) ⇒ ((a))
9. Convert the following CFG into an equivalent CFG in Chomsky normal form,
A BAB | B | e B 00 | e
First introduce new start variable S0 and the new rule S0 → A, which gives
S0 → A
A → BAB | B | ε
B → 00 | ε
Then we replaced ill-placed terminals 0 by variable U with new rule U → 0, which gives
S0 → BAB | BA | B | UU | BB | ε
A → BAB | BA | AB | UU | BB
B → UU
U→0
Then we shorten rules with a long RHS to a sequence of RHS’s with only 2 variables
each. So the rule S0 → BAB is replaced by the 2 rules S0 → BA1 and A1→ AB. Also the rule
A→ BAB is replaced by the 2 rules A → BA2 and A2 → AB. Thus, our final CFG in Chomsky
normal form is
S0 → BA1 | BA | AB | UU | BB | ε
A → BA2 | BA | AB | UU | BB
B → UU
U→0
A1 → AB
A2 → AB
10. A program in a real programming language, such as C of Java, consists of statements, where
each statement is one of several types:
i. assignment statement, of the form id = E, where E is any arithmetic expression.
ii. conditional statement, of the form, say, if E < E then statement, or a while statement
of the form while E < E do statement.
iii. goto statement, furthermore, each statement could be preceded by a label.
iv. Compound statement, that is, many statements preceded by a begin, followed by an
end, and separated by a “;”.
Give a context free grammar that generates all possible statements in the simplified
programming language described above.
Ans: Since we already have a grammar for expressions (E), we'll just use E in this grammar
and treat it as though itwere a terminal symbol. Of course, what we really have to do is to
combine this grammar with the one for E. As we did in our grammar for E, we'll use the
terminal string id to stand for any identifier. G = (V, Σ, R, S), where V = {S, U, C, L, T, E, :, = <,
>, ;, a-z, id}, Σ = { :, = <, >, ;, a-z, id}, and
R= {
S→LU
S→U
U → id := E
U → if E T E then S
U → while E T E do S
U → goto L
U → begin S;
S end
L → id
T→ < | > | =
}
There's one problem that hasn't addressed here. It is not guaranteed that every label that
appears after a goto statement actually appears in the program. In general, this cannot be
done with a context-free grammar.