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.

32<br />

5. REGULAR EXPRESSIONS<br />

These expressi<strong>on</strong>s denote sets <strong>and</strong> are defined as follows:<br />

5<br />

10<br />

(i)<br />

(ii)<br />

(iii)<br />

(iv)<br />

∅ is a regular expressi<strong>on</strong>:<br />

{ε} is a regular expressi<strong>on</strong><br />

{a} is a regular expressi<strong>on</strong> where "a" is a terminal symbol.<br />

If R <strong>and</strong> S are regular expressi<strong>on</strong>s then the following are also regular<br />

expressi<strong>on</strong>s.<br />

(a) R|S<br />

(b) RS<br />

(c) R* (also S*)<br />

- the uni<strong>on</strong><br />

- the c<strong>on</strong>catenati<strong>on</strong><br />

- the transitive closures<br />

15<br />

20<br />

In practice we will write ε <strong>and</strong> not {ε}, "a" <strong>and</strong> not {a}. Of course the set<br />

{a,b,c,d} is then written (a|b|c|d) <strong>and</strong> of course {a,b} ∪ {c,d} would be written<br />

(a|b) | (c|d) as regular expressi<strong>on</strong>s.<br />

Incidentally, ∅* = ε in regular expressi<strong>on</strong>s.<br />

Regular expressi<strong>on</strong>s are useful for defining the structure of reserved words,<br />

identifiers, numbers occuring in programming languages but are not suitable<br />

for defining the structure of blocks, expressi<strong>on</strong>s, statements etc.<br />

Examples of Regular Expressi<strong>on</strong>s:<br />

25<br />

30<br />

35<br />

40<br />

1) Unsigned Integers:<br />

(0|1|2|...|9)(0|1|2...|9)*<br />

2) Integers with an opti<strong>on</strong>al sign<br />

(ε|+|-)(0|1|2|...|9)(0|1|2|...9)*<br />

3) Identifiers:<br />

(a|b|c|...|z)(a|b|c|...|z|0|1|2...|9)*<br />

Exercises: 1) Write a regular expressi<strong>on</strong> defining the structure of a real<br />

number which may include a sign, a decimal point <strong>and</strong> an<br />

exp<strong>on</strong>ent which are opti<strong>on</strong>al.<br />

2) Change the definiti<strong>on</strong> of the first example above so as to<br />

exclude numbers with unneccessary leading zeroes, i.e. to<br />

exclude numbers such as 000, 00156, etc.<br />

Equivalence between regular expressi<strong>on</strong>s <strong>and</strong> regular grammars<br />

45<br />

We shall now show that the sets defined by regular expressi<strong>on</strong>s <strong>and</strong> regular<br />

grammars are equivalent.

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

Saved successfully!

Ooh no, something went wrong!