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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

7. SIMPLIFYING THE PROCESS OF BUILDING A COMPILER<br />

44<br />

5<br />

10<br />

A great deal of effort is needed to build a compiler, <strong>and</strong> we shall present two<br />

methods for saving much effort.<br />

Notati<strong>on</strong><br />

Let us use the notati<strong>on</strong> L C M A to denote a compiler which translates from<br />

language L to language M <strong>and</strong> is written in language A. (Yes - writing a<br />

compiler may involve you working with three languages.)<br />

When this compiler is executing <strong>and</strong> translating a program P L in language L<br />

to the same program P M but in language M we shall write<br />

15<br />

P L → L C M A →P M<br />

Bootstrapping a compiler<br />

(The boxed item is being executed.)<br />

20<br />

25<br />

Suppose we wish to build the compiler L C A A from high level language L to<br />

assembly or machine language A.<br />

The following approach will save much effort.<br />

(i) Identify a subset S of the language L which is easier to compile.<br />

(ii) Write the compiler S C A A .<br />

(iii) Write the compiler L C A S .<br />

(iv) Execute the following. .<br />

L C A S → S C A A → L C A A<br />

Cross compilati<strong>on</strong><br />

30<br />

35<br />

Here are two uses of this technique.<br />

1) If the machine (e.g. microc<strong>on</strong>troller) with language M does not have<br />

the resources to do the compilati<strong>on</strong> itself then we can use another<br />

machine with language A <strong>and</strong> run the compiler <strong>on</strong> machine with<br />

language A <strong>and</strong> just transfer the translated program <strong>on</strong>to the machine<br />

with language M. For example when L C M A runs <strong>on</strong> machine with<br />

language A then execute<br />

40<br />

45<br />

P L → L C M A → P M<br />

<strong>and</strong> transfer P M to run <strong>on</strong> machine with language M. This technique<br />

allows the use of high level languages <strong>on</strong> machines with very limited<br />

resources by using an auxiliary computer to do the compilati<strong>on</strong>.<br />

2) Another use of this c<strong>on</strong>cept is to create a compiler for a new machine<br />

by using a compiler for an existing machine. Suppose a machine<br />

exists which can run L C A A where L is a high level language. Suppose

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

Saved successfully!

Ooh no, something went wrong!