Lecture no.
1
COMPILER CONSTRUCTION.
Table of contents
Why Study Compilers
Introduction of language processing system
Preprocessor
Compilers
Properties of compilers
Assembler
Interpreter
Linker
Loader
The Context of a Compiler
Assignment
Why Study Compilers
Reason #1: understand compilers and languages.
Understand the code structure.
Understand language semantics.
Understand relation between source code and generated machine code.
Allow to become a better programmer and increase programmer productivity and increase portability
Reason #2: nice balance of theory and practice.
Theory:
Mathematical models: regular expressions, automata, grammars, graphs.
Algorithms that use these models.
Practice:
Apply theoretical notions to build a real compiler.
Reason #3: programming experience.
Creating a compiler entails writing a large computer program which manipulates complex data structures and
implement sophisticated algorithm
Increasing programming capability
Introduction of language processing
system
Preprocessor
A preprocessor produce input to compilers. They may perform the following
functions.
Macro processing: A preprocessor may allow a user to define macros that are short
hands for longer constructs.
1. File inclusion: A preprocessor may include header files into the program text.
2. Rational preprocessor: these preprocessors augment older languages with
more modern flow-of control and data structuring facilities.
3. Language Extensions: These preprocessor attempts to add capabilities to the
language by certain amounts to build-in macro.
Compilers
A Compiler is a program (system software) that reads a program written in one
language – the source language – and translates it into an equivalent program in
another language – the target language.
During translation process, the compiler reports to its user the presence of errors
in the source program.
Compilers
Source language can be any high level computer programming language ranging
from traditional programming language such as Fortran, C, Java etc to specialized
language that have been written for a specific area of computer application such
as LISP for AI etc.
Target language may be another programming language (assembly language) or
the machine language of a computer, depending upon the compiler. High High--
level source code level source code
Compilation Process
It takes the whole program at a time and either displays all of the possible errors
in the program or creates an object program.
The time at which the conversion of a source program to an object program
occurs is called compile time.
The object program is executed at run time.
Properties of compilers
It must generate a correct executable code
The input program and the output program must be equivalent
The compiler should preserve the meaning of the input program
Output program should run fast
Compiler itself should be fast i.e., low compilation time
Compiler should provide good diagnostics for programming errors
Properties of compilers
Compiler should support separate compilation
Compiler should work well with debuggers
Compile time should be maximally proportional to the code size
Assembler
Assembler is a translator (software) that particularly converts a program written
in assembly language into machine language.
Assembly language is called low-level language.
Because there is one to one correspondence between the assembly language
statements and machine language statements.
Symbolic form of the machine language, makes it easy to translate
Compiler generates assembly language as its target language and assembler
translate it into object code
Interpreter
Interpreter is a system software that is used for the translation of high level
language programs.
Directly execute the statements of source program rather than generating object
code
It is different from the compilers in a sense that:
It translates a program by taking one instruction at a time and produces the results
before taking the next instruction.
It can identify only one error at a time.
It does not produces the object program.
Needs retranslation
Makes it slow than compilers by a factor of 10 times
Interpreter
Linker
Separate program often part of operating system
Collects code in object file(s) into a file that is directly executable
Object code produced by a compiler or assembler --- machine code that has not
yet been linked
Linker creates executable machine code
Connects an object program to the code of standard library functions and to
resources supplied by operating system
For example, memory allocation and input and output devices etc.
Loader
The loader reads the reloadable machine code
Alters its addresses by adding the starting position of main memory block to them
and loads the code into main memory
The Context of a Compiler
In addition to compiler, several other programs may be required to create an
executable target program.
A source program may be divided into modules stored in separate files. The task
of collecting the source program is the responsibility of another program called
preprocessor.
The target program created by the compiler may require further processing before
it can be run.
The Context of a Compiler
The compiler creates the assembly code that is translated by an assembler into
machine code.
The linker together the machine code with some library routines into the code that
actually run on the machine.
Analysis and Synthesis Model
The structure of compiler consists of two parts:
Analysis part
Synthesis part
Analysis part
Analysis part breaks the source program into constituent pieces and imposes a
grammatical structure on them which further uses this structure to create an
intermediate representation of the source program
It is also termed as front end of compiler
Information about the source program is collected and stored in a data structure
called symbol table
Synthesis part
Synthesis part takes the intermediate representation as input and transforms it to
the target program
It is also termed as back end of compiler
Analysis and Synthesis Model
ASSIGNMENT NO .1
List of compiler construction tools.
Format of submission: Hand written or in printed form
Dead line: 24 November