17.01.2015 Views

Course Notes on Formal Languages and Compilers

Course Notes on Formal Languages and Compilers

Course Notes on Formal Languages and Compilers

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

10<br />

5<br />

bX→Xb<br />

aX→aab<br />

abcd→ε<br />

bc→XbcY<br />

Yc→cY<br />

Yd→cdd<br />

10<br />

15<br />

We shall not prove that L(G 1 ) = { a n b n c n d n : n≥0} but will give examples of<br />

derivati<strong>on</strong>s of ε <strong>and</strong> a 3 b 3 c 3 d 3 from the start set. Incidentally abcd∈L(G 1 )<br />

because abcd∈T 1 * <strong>and</strong> abcd∈S 1 .<br />

As S 1 has <strong>on</strong>ly <strong>on</strong>e element that is, abcd, all derivati<strong>on</strong>s must start with<br />

abcd. In the following derivati<strong>on</strong>s we shall underline the part of the string<br />

which is being replaced at each derivati<strong>on</strong> step.<br />

Derivati<strong>on</strong> of ε: abcd ⇒ ε directly by a rule<br />

Derivati<strong>on</strong> of a 3 b 3 c 3 d 3 :<br />

20<br />

25<br />

30<br />

35<br />

abcd ⇒ aXbcYd ⇒ aabbcYd ⇒ aabbccdd<br />

⇒ aabXbcYcdd ⇒ aaXbbcYcdd<br />

⇒ aaXbbccYdd ⇒ aaabbbccYdd<br />

⇒ aaabbbcccddd = a 3 b 3 c 3 d 3<br />

Exercise: C<strong>on</strong>tinue the above derivati<strong>on</strong> to derive a 4 b 4 c 4 d 4 .<br />

Example of a C<strong>on</strong>text Sensitive Grammar: The previous grammar G 1 is not<br />

a c<strong>on</strong>text sensitive grammar. An equivalent c<strong>on</strong>text sensitive grammar is<br />

G 2 = (N 2 , T 2 , P 2 , S 2 ) where N 2 ={X, Y} as before T 2 = {a,b,c,d} as before,<br />

S 2 ={abcd,ε} <strong>and</strong> P 2 c<strong>on</strong>sists of the rules<br />

bX→Xb<br />

aX→aab<br />

bc→XbcY<br />

Yc→cY<br />

Yd→cdd<br />

What we have d<strong>on</strong>e is to eliminate the rule abcd→ε which is not permitted<br />

in a c<strong>on</strong>text sensitive grammar <strong>and</strong> add ε to the starting set of G 2 .<br />

Exercises<br />

40<br />

45<br />

1) Is G 2 a phrase structure grammar too<br />

2) Write a c<strong>on</strong>text sensitive grammar for the language {a 2n b n c n d 2n : n≥1}.<br />

Present a derivati<strong>on</strong> of "aaaabbccdddd" from the starting set of the<br />

grammar.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!