0% found this document useful (0 votes)
24 views

Assignment 4

Theory of automata assignment questions solved

Uploaded by

Bakhtawar Tariq
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Assignment 4

Theory of automata assignment questions solved

Uploaded by

Bakhtawar Tariq
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Advance Theory of Computation

Dr. Abdul Salam

Assignment # 4

1. Which language generates the grammar G given by the following productions?


S → aSa | aBa B → bB | b
Ans: L = {an bk}

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

3. Find a CFG that generates the language L(G) = { a n bm | 0 ≤ n ≤ m ≤ 2n}.


Ans: S -> A | ε A -> aAb | aAB B -> bB | b

4. Consider the grammar S → abScB | λ B → bB | b What language does it generate?


Ans: L = {an bm cn }

5. Construct context free grammars to accept the following languages.


a. {w | w starts and ends with the same symbol}
Ans: S -> aX | bX X -> aS | bS| ε

b. {w | |w| is odd}
Ans: S -> aSa | bSb | a | b

Ans: S→0 ∣ 0S0 ∣ 0S1 ∣ 1S0 ∣ 1S1


c. {w | |w| is odd and its middle symbol is 0}

d. {w#x | wR is a substring of x, where w, x ∈ {a, b}*}


Ans: S → CB C → 0C0 | 1C1 | #B B → AB | ε A → 0 | 1

e. {an bn | n>0} U {an b2n | n>0}


Ans: S → X | Y X → aXb | ab Y → aYB | aB B → bb

f. Binary strings with twice as many 1s as 0s.


Ans: S -> 0SB B->11 S-> ε
6. Explain why the grammar below is ambiguous.
S → 0A | 1B
A → 0AA | 1S | 1
B → 1BB | 0S | 0

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

7. Given the following ambiguous context free grammar S → Ab | aaB A → a | Aa B → b


a. Find the string w generated by the grammar that has two leftmost derivations. Show
the derivations.
Ans: for the string aab we can have two left most derivations
S=>Ab=>Aab=>aab
S=>aaB=>aab
b. Show the two derivation trees for the strings.

c. Find an equivalent unambiguous context-free grammar.


Ans: S->Ab A->Aa A-> a
d. Give the unique leftmost derivation and derivation tree for the string s generated from
the unambiguous grammar above
Ans: S=>Ab=>Aab=>aaB
8. Consider the following CFG
EE+T|T
TTxF|F
F (E) | a
Give parse trees and derivations for each string.

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 remove ε rules: B → ε and A → ε:


S0 → A | ε
A → BAB | BA | AB | A | B | BB
B → 00

Then we remove unit rules: A → A, A → B and S0 → A


S0 → BAB | BA | AB | 00 | BB | ε
A → BAB | BA | AB | 00 | BB
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.

You might also like