Sunteți pe pagina 1din 16

Computer Science Department

CS314: FORMAL LANGUAGES


AND AUTOMATA THEORY
TA. HANOUF ALJLAYL
Chapter 2 - Tutorial

Exercise 2.1
2

Recall the CFG G4 that we gave in Example 2.4. For convenience, let's
rename its variables with single letters as follows.

Give parse trees and derivations for each string.

Computer Science Department

Solution 2.1
3

a. The derivation is .

b. The derivation is E E + T T + T F + T
a + T a + F a + a.

Computer Science Department

Solution 2.1
4

c. The derivation is +
+ + + + +
+ + + + +
+ + + + + +
.

d. The derivation is () ()
() (()) (()) (()) (()).

Computer Science Department

Exercise 2.3
5

Answer each part for the following context-free grammar G.

Computer Science Department

Solution 2.3
6

a.

b.

c.
d.
e.
f.
g.
h.

The variables of G
are , , , .
The terminals of G are , .
The start variable is R.
Strings in G are ab, ba, and aab.
Strings not in G are a, b, and .
False.
True.
False.

i.

j.
k.
l.
m.

n.
o.

True.
True.
False.
True.
True.
False.
L(G) is the language of all
strings w over {a, b} such
that w is not palindrome.

Computer Science Department

Exercise 2.4
7

Give context-free grammars that generate the following languages. In


all parts the alphabet is {0,1}.

Computer Science Department

Solution 2.4
8

a. 111
0 1
b. 00 11
0 1
c. 0 1 00 01 10 | 11
d. 0 | 00 01 10 | 11

Computer Science Department

Exercise 2.6
9

Give context-free grammars generating the following languages.

Solution

a. |

Computer Science Department

Exercise 2.9
10

Give a context-free grammar that generates the language

Is your grammar ambiguous? Why or why not?

Computer Science Department

Solution 2.9
11

Let = (, , , ), where = {, , , , }, = {, , } and R


consists of the following rules.
|
|
|
|
|

The context-free grammar G is ambiguous. Because we have two


different derivations. and

Computer Science Department

Exercise 2.13
12

a.

Let G = (V, Z, R, S) be the following grammar. V = {S, T, U}; Y {0, #}; and R
is the set of rules:

Describe L(G) in English.


Solution

L(G) is a set of strings of 0s and #s that either contain exactly 2 #s and any
number of 0s, or contain exactly 1 # and the number of 0s to the right of the # is
twice the of 0s to the left.

Computer Science Department

Exercise 2.14
13

Convert the following CFG into an equivalent CFG in Chomsky normal


form, using the procedure given in Theorem 2.9.

Computer Science Department

Solution 2.14
14

The equivalent CFG in Chomsky normal form is:


0 BA1 | AB | BA | B1 B1 | BB |
BA1 | AB | BA | B1 B1 | BB
B1 B1
B1 0
A1

Computer Science Department

Solution 2.14 Details


15

Step 1: add new start symbol.

0
BAB | B |
00 |

Step 2: remove from right-hand-side of rules.

S0 A
A BAB | AB | BA A B |
B 00

S0 A |
A BAB | AB | BA A B |
B 00

Computer Science Department

Solution 2.14 Details


16

Step 3: remove the unit rules.

S0 A |
A BAB | AB | BA A 00 |
B 00

S0 BAB | AB | BA A 00 | |
A BAB | AB | BA A 00 |
B 00

Step 4: convert it to a proper form.

0 BA1 | AB | BA | B1 B1 | BB |
BA1 | AB | BA | B1 B1 | BB
B1 B1
B1 0
A1

Computer Science Department

S-ar putea să vă placă și