compiler construction course - introduction

17
Air University, Multan Campus A Faderally Charted Public Sector University Tutor: Dr. Muhammad Sanaullah [email protected] Introduction Course: Compiler Construction

Upload: muhammad-sanaullah

Post on 06-May-2015

594 views

Category:

Education


3 download

DESCRIPTION

Start of Compiler Construction course.

TRANSCRIPT

Page 1: Compiler Construction Course - Introduction

Air University, Multan CampusA Faderally Charted Public Sector University

Tutor: Dr. Muhammad Sanaullah

[email protected]

Introduction

Course: Compiler Construction

Page 2: Compiler Construction Course - Introduction

Quotations

2

“If you don’t understand compilers, you can still write programs – you can even be a competent programmer – but you can’t be a master. ”

Hal Abelson, MIT

“If you don’t know how compilers work, then you don’t know how computer work. If you’re not 100% sure whether you know how compilers work, then you don’t know how they work.”

Steve Yegge

Page 3: Compiler Construction Course - Introduction

Translator & Compiler

3

A translator or language processor is a program that translate an input program written in a programming language into an equivalent program in another language.

Compiler is a type of translator, which takes a program written in high-level programming language as input (source program) and translates into an equivalent program in low-level language (target program), such as machine language or assembly language. Compiler

SourceProgram

TargetProgram

Page 4: Compiler Construction Course - Introduction

Compilers

4

Compilers traces the errors in the source program and generates the error report.

Every programming language have different compilers.

Without compilation, no program written in a high-level language can be executed.

After compilation only the program in machine language is loaded into the memory for execution.

During execution, the target program is firstly loaded into the memory and then the user interacts to generate output.

CompilerInput supplied by

the userOutput produced

after execution

Page 5: Compiler Construction Course - Introduction

Preprocessors

5

Before compilation, the source program is processed by the preprocessor to prepare it for compilation.

The preprocessor program creates modified source program from the original source program by replacing the preprocessor directives with the suitable content.

The modified source program act as an input to the compiler.

Page 6: Compiler Construction Course - Introduction

Preprocessors

6

Preprocessor performs the following tasks: It permits the user to include the header files in

the program and user can make use of the functions defined in these header files.

It permits the user to include macros in the program. Macros are the small set of instructions that are used in

a program respectively. Macros have two attributes: macro name and macro definition.

Whenever the macro name is encountered in the program then it is replaced by the macro definition (set of statements correspond to the macro).

Preprocessor SourceProgram

TargetProgram

Compiler

Modified Source

Program

Page 7: Compiler Construction Course - Introduction

Interpreter

7

Interpreter directly executes the source program line by line according to the given inputs.

Translation and execution of each statement are carried out side by side. Thus, separate execution of the program is not required.

The line by line execution of the program provides better debugging environment than a compiler.

Interpreter

SourceProgram

Output

Input

Page 8: Compiler Construction Course - Introduction

Compilers Vs Interpreter

8

The interpreter takes one statement then translates it and executes it and then takes another statement. While the compiler translates the entire program in one go and then executes it.

Compiler generates the error report after the translation of the entire page while an interpreter will stop the translation after it gets the first error.

Compiler takes a larger amount of time in analyzing and processing the high level language code comparatively interpreter takes lesser time in the same process.

Besides the processing and analyzing time the overall execution time of a code is faster for compiler relative to the interpreter.

Page 9: Compiler Construction Course - Introduction

Compilers Vs Interpreter

9

Page 10: Compiler Construction Course - Introduction

Hybrid Compiler

10

Java language processors combine compilation and interpretation

To achieve faster processing of inputs to outputs, some java compilers, called just-in-time compilers, translate the bytecodes into machine language immediately before they run the intermediate program to process the input.

Virtual Machine

IntermediateProgram

Output

Input

Translator

SourceProgram

ByteCodes

Compiler

Interpreter

Page 11: Compiler Construction Course - Introduction

Assemblers

11

In some cases, compiler generates the target program in assembly language, and then this intermediate (assembly program – Mnemonics) is passed to assembler which convert it into machine code.

CompilerSourceProgram

MachineLanguage

CodeAssembler

Assembly LanguageProgram

Page 12: Compiler Construction Course - Introduction

Analysis of a Source Program

12

Some time, a source program may be divided into modules stored in separate files.

The task for collecting the source program (or macros) from separate files is called preprocessor.

The modified source program is then fed to a compiler.

The compiler may produce an assembly-language program as its output., because Assembly language is easier to produce as output and is easier to debug.

The assembly language program is then fed to assembler that produces relocatable machine code as its output.

Page 13: Compiler Construction Course - Introduction

Analysis of a Source Program

13

Larger programs are often compiled in pieces, so the relocatable machine code may have to be linked together with other relacateble object files and library files into the code that actually runs on the machine.

The linker resolves external memory addresses, where the code in one file may refer to a location in another file.

The loader then puts together all of the executable object files into memory for execution.

Page 14: Compiler Construction Course - Introduction

Analysis of a Source Program

14

Preprocessor

Source Program

Compiler

Assembler

Linker/loader

Target Machine Code

Modified Source Program

If target program is in machine language then it execute directly

If target program is in assembly language then it passed to assembler

Relocatable Machine Code

Library Files and Relocatable Object Files

Page 15: Compiler Construction Course - Introduction

References

15

A. V. Aho, Compilers: principles, techniques, and tools, Pearson Education India, 2007.

ITL Education Solutions Limited, Principles of compiler design, Dorling Kindersley (India) Pvt. Ltd, Pearson Education in South Asia, 2012.

P.H. Dave, H.B. Dave, Compilers: Principles and Practice, Dorling Kindersley (India) Pvt. Ltd, Pearson Education in South Asia, 2012.

Page 16: Compiler Construction Course - Introduction

Design Time Methodology for the Formal Modeling and Verification of Smart Environments16

Page 17: Compiler Construction Course - Introduction