Docsity

BNF Homework Solutions: Derivation Trees and Grammar Equivalence, Assignments of Programming Languages

Toggle sidebar
bg1
22C:185 — Fall 2004
Homework I
1. [15 points]
Taking S = {0,1} and start = S in the BNF
S ::= 0B | 1A
A ::= 0 | 0S | 1AA
B ::= 1 | 1S | 0BB
show derivation trees for each of the following
(a) 001011
(b) 10111001
(c) 10011100
2. [10 points]
Taking S = {a,b} and start = S in the BNF below, show two distinct derivation trees for the
string aab.
S ::= aSB | e
B ::= bB | e
3. [10 points]
Consider the two grammars G1 (start=W) and G2 (start=Y) below with S = {a,b,c}. Determine
whether or not they are equivalent (i.e., L(G1) = L(G2) and prove your answer.
G1
G2
W ::= Wc | X
Y ::= aY | Z
X ::= aXb | e
Z ::= bZc | e
4. [20 points]
Determine whether each of the following identities is true for all languages L1,L2 Õ S*, and
justify your answers.
(a) (L1 » L2)* = L1
* » L2
*
(b) (L1•L2)* = L1
* • L2
*
(c) ∅* = ∅
(d) (L1 « L2)* = L1
* « L2
*

Partial preview of the text

Download BNF Homework Solutions: Derivation Trees and Grammar Equivalence and more Assignments Programming Languages in PDF only on Docsity!

22C:185 — Fall 2004

Homework I

1. [15 points]

Taking S = {0,1} and start = S in the BNF

S ::= 0B | 1A

A ::= 0 | 0S | 1AA

B ::= 1 | 1S | 0BB

show derivation trees for each of the following

(a) 001011

(b) 10111001

(c) 10011100

2. [10 points]

Taking S = {a,b} and start = S in the BNF below, show two distinct derivation trees for the

string aab.

S ::= aSB | e

B ::= bB | e

3. [10 points]

Consider the two grammars G 1

(start=W) and G 2

(start=Y) below with S = {a,b,c}. Determine

whether or not they are equivalent (i.e., L(G 1

) = L(G

2

) and prove your answer.

G

1

G

2

W ::= Wc | X Y ::= aY | Z

X ::= aXb | e Z ::= bZc | e

4. [20 points]

Determine whether each of the following identities is true for all languages L 1

,L

2

Õ S

, and

justify your answers.

(a) (L 1

» L

2

= L

» L

(b) (L 1

•L

2

= L

• L

(c) ∅

(d) (L 1

« L

2

= L

« L