1 THEORY OF COMPILERS Instructor: Dr. Nariman A. Khaliel Books ◼ Three good books: ◼ Aho, Lam, Sethi, Ullman, “ Dragon Book ” , 2 nd ed (but 1 st ed is also fine) ◼ Appel, Modern Compiler Implementation in Java , 2 nd ed. ◼ Cooper & Torczon, Engineering a Compiler ◼ Dragon book is the “ official ” text, but all would work & we ’ ll draw on all three (and more) ◼ If we put these on reserve in the engineering library, would anyone notice? 3 4 LECTURE 1 Introduction Language Processing System Preprocessor ◼ A pre - processor produces input to compilers. They may perform the following functions. ◼ 1 . Macro processing : A pre - processor may allow a user to define macros that are short hands for longer constructs. ◼ 2 File inclusion : A pre - processor may include header files in the program text. ◼ 3 Rational pre - processor : these pre - processors augment older languages with more modern flow - of - control and data structuring facilities. ◼ 4 Language Extensions : These pre - processors attempt to add capabilities to the language by certain amounts to build - in macro 7 Compilers ◼ A program that translates a program from one language (the source ) to another (the target ) ◼ E.g. for a C compiler, the source language is C while the target language may be assembly language. ◼ the meaning of the translated text should be the same as the meaning of the source text 8 ◼ In order to develop a compiler, we need a precise definition of both the source and the target language This means that both the source and target language must be formal ◼ A language has two aspects : syntax and semantics The syntax prescribes which texts are grammatically correct and the semantics specifies how to derive the meaning from a syntactically correct text Compiler 10 11 12 Structure of a Compiler ◼ First approximation: ◼ Front end: analysis ◼ Read the source program and understand its structure and meaning ◼ Back end: synthesis ◼ Generate equivalent target language program Source Target Front End Back End 14 15 16 17 18 Implications ◼ Need some sort of Intermediate Representation(s) (IR) ◼ Front end maps source into IR ◼ Back - end maps IR to target machine code ◼ Often multiple IRs – higher level at first, lower level in later phases Source Target Front End Back End 20