Sie sind auf Seite 1von 9

Department of Computing, FEST, Indus University, Karachi

Theory of Automata(CS-313)
Assignment # 3

1. Give a Regular Expression and DFA for:

L = {x ∈ {0, 1}* | x ends with 1 and does not contain the substring 00}

Ans:

The R.E will be:

R.E = (1 + 01)+

The DFA will be:

DFA

2. Give a RE for: L = {0i1j | i is even and j is odd }

Ans:

The R.E of this language will be:

R.E = (00)* 1 (11)*

Asra
2323-2017 Page 1
Department of Computing, FEST, Indus University, Karachi

3. Given the NFA for below for 0*(01)*0*, construct a DFA:

Ans:

The DFA for the given NFA is:

4. Give a RE and a DFA/NFA for the language of all strings over {0, 1}∗ that do not end in 01.

Ans:

The R.E for this language is:

R.E = ^ + 0 + 1 + (0+1)* (00+ 10+11)

DFA:

5. Give a RE and a CFG for:

Asra
2323-2017 Page 2
Department of Computing, FEST, Indus University, Karachi

L = {x ∈ {0, 1}* | x starts and ends with different symbols }

Ans:

The R.E for the language is:

R.E = [0(0+1)*1 + 1(0+1)*0]

The CFG for this language is:

S  aXb | bXa

A  aX | bX | ^

6. Give a CFG for:

L = {x ∈ {0, 1}∗ | symbol at position i is same as symbol at position i+2 and | x |≥ 2}

Ans:

The CFG for this language is:

SA|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:

The CFG for this language will be:

S  0S0 | 1S1 | D

D  1A0 | 0A1

A  ^ | 0A | 1A

8. Give a CFG for:

L = {0i1j0k | j > i + k} So, 001111100 is in the string.

Asra
2323-2017 Page 3
Department of Computing, FEST, Indus University, Karachi

Hint, the concatenation of two(or more) context-free languages is context-free.

Ans:

The CFG will be:

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:

1. Eliminate left recursion from:

S → Aa | b

A → Ac | Sd | ε

Ans:

The CFG is:

S  Aa | b

A  A` | bdA`

A`  cA` | adA` | ^

2. Give a CFG for L = {aibici | i ≥ 1}.

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:

We will derive a string aacbc therefore,

S  aSbS

 aaSbS

 aacbc

Asra
2323-2017 Page 5
Department of Computing, FEST, Indus University, Karachi

S  aS

 aaSbS

 aacbc

To remove the ambiguity we will modify our CFG

SX|Y

X  aXbX | c

Y  aS | aXbY

Therefore, the string aacbc will be now derived as:

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:

The R.E is:

R.E = (ac* b+) | (aabc)

The NFA is:

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

The decimal expansion of an irrational numbet never terminates and


never repeats, so it is not a string. Languages
are sets of strings so no language can include such numbers.

You could try to name them, as in L = { PI, e, 2*PI, ... }


but there are uncountably infinite so they can't be enumerated by
a computer program.

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

To remove ambiguity we will modify our CFG, therefore:

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

Das könnte Ihnen auch gefallen