Beruflich Dokumente
Kultur Dokumente
Theory of Automata(CS-313)
Assignment # 3
L = {x ∈ {0, 1}* | x ends with 1 and does not contain the substring 00}
Ans:
R.E = (1 + 01)+
DFA
Ans:
Asra
2323-2017 Page 1
Department of Computing, FEST, Indus University, Karachi
Ans:
4. Give a RE and a DFA/NFA for the language of all strings over {0, 1}∗ that do not end in 01.
Ans:
DFA:
Asra
2323-2017 Page 2
Department of Computing, FEST, Indus University, Karachi
Ans:
S aXb | bXa
A aX | bX | ^
Ans:
SA|B|C|D
A 00A | 00
B 11B | 11
C 10C | 10
D 01D | 01
7. Give a CFG for the language of all non-palindromes over {0, 1}*.
Ans:
S 0S0 | 1S1 | D
D 1A0 | 0A1
A ^ | 0A | 1A
Asra
2323-2017 Page 3
Department of Computing, FEST, Indus University, Karachi
Ans:
S ABC
A 0A1 | ^
B 1B | 1
C 1C0 | ^
Asra
2323-2017 Page 4
Department of Computing, FEST, Indus University, Karachi
Theory of Automata(CS313)
Assignment # 4
Deadline:
S → Aa | b
A → Ac | Sd | ε
Ans:
S Aa | b
A A` | bdA`
A` cA` | adA` | ^
Ans:
The CFG cannot be formed, because the language is not context-free language.
3. Is this grammar ambiguous? If so, prove it and construct a non-ambiguous grammar that
derives the same language.
S → aS | aSbS | c
Ans:
S aSbS
aaSbS
aacbc
Asra
2323-2017 Page 5
Department of Computing, FEST, Indus University, Karachi
S aS
aaSbS
aacbc
SX|Y
X aXbX | c
Y aS | aXbY
Asra
2323-2017 Page 6
Department of Computing, FEST, Indus University, Karachi
4.Write a NFA using the regular expression (ac*b+)|(aabc?) where ∑ = {a, b, c}.
Ans:
5. Write CFG from the above (Q12) regular expression (or NFA).
6. Write one benefit of having intermediate code. Do not write more than one sentence.What
are the difficulties associated with constructing a grammar whose generated strings are
decimal representations of irrationalnumbers? How can these difficulties be resolved?
Ans:
The intermediate code makes back-end and front-end independent from each other or it
makes language and machine independent code optimizations possible.
Asra
2323-2017 Page 7
Department of Computing, FEST, Indus University, Karachi
7. Show all distinct derivations (trees) of a + a + a using the grammar and remove ambiguity.
E -> E + E | a
Ans:
E E+E | a
E E+E +E
a+a+a
E E+E | a
E E+E +E
a+a+a
Asra
2323-2017 Page 8
Department of Computing, FEST, Indus University, Karachi
E E+E | a
We will replace it by
E E+a | a
And then
E E+a | a
E E+a
E+a +a
a+a +a
Asra
2323-2017 Page 9