Sunteți pe pagina 1din 21

Concepts of Compiler Design

DR. Marwa Fayez Areed


Lecture 1 October 2009 Department of Computer & Systems Engineering Mansoura University

Translators
Translate a program written in a source language into object language Both source and object are artificial languages

Why Do We Need Translators?


Enables use of high-level languages Otherwise, required to use machine Languages expressed in 1s and 0s deal directly with hardware (e.g .registers

October -2009

Concepts of Compiler Design

How are Languages Implemented?

A compiler is a program takes a program written in a source language and translates it into an equivalent program in a target language.

source program

COMPILER

target program

error messages
Interpreter Run programs as is without preliminary translation: Successive phases of translation (to machine/intermediate code) and execution.
An
October -2009 Concepts of Compiler Design 3

Compiler VS. Interpreter

Compiler Efficient for production applications Order of magnitude faster

Interpreter Efficient and rabid for prototyping Efficient error reporting

October -2009

Concepts of Compiler Design

Language Processing System


Programs needed to create an executable generated code

October -2009

Concepts of Compiler Design

Assembler
The Assembler is responsible for translating the target code usually assembly codeinto an executable machine code. The assembly code is a mnemonic version of machine code in which: 1. Names are used instead of binary codes for operations (Code Table). 2. Names are used for operands instead of memory locations (Symbol Tables).

October -2009

Concepts of Compiler Design

Loader and Linker


The machine code generated by the Assembler can be executed only if allocated in Main Memory starting from the address 0. So Loader will alter the re-locatable addresses of the code to place both instructions and data in the right place in Main Memory.

The starting free address, L, in Main Memory to allocate the program is called the
Relocation Factor. The Loader must add to each re-locatable address the relocation factor L;

The Linker links together the different files/modules of a single program and,
possibly, adds library files.
October -2009 Concepts of Compiler Design

What Qualities You want in Compiler?


1.Correct code

2.Output runs fast


3.Compiler runs fast 4.Compile time proportional to program size 5.Support for separate compilation 6.Good diagnostics for syntax errors

October -2009

Concepts of Compiler Design

Major Parts of Compiler Operation


Compiler consists of two phases

Analysis
(Front End)

Synthesis
(Back End)

Analysis phase: Breaks the source program into constituent pieces and creates
intermediate representation.

The analysis part can be divided along the following phases:

Lexical Analyzer, Syntax Analyzer Semantic Analyzer

Synthesis phase : Generates the target program from the intermediate representation.
The synthesis part can be divided along the following phases:

Intermediate Code Generator; Code Optimizer; Code Generator.


October -2009 Concepts of Compiler Design 9

Phases of A Compiler
Each phase transforms the source program from one representation into another representation. They communicate with error handlers. They communicate with the symbol table.

October -2009

Concepts of Compiler Design

10

Lexical Analysis
The program is considered as a unique sequence of characters. The Lexical Analyzer reads the program from left-to-right and sequence of characters are grouped into tokenslexical units with a collective meaning. The sequence of characters that gives rise to a token is called lexeme. An Example Let us consider the following assignment statement: position = initial + rate * 60 Then, the lexical analyzer will group the characters in the following tokens:

October -2009

Concepts of Compiler Design

11

Symbol Table
An essential function of a compiler is to build the Symbol Table where the identifiers used in the program are recorded along with various Attributes.

October -2009

Concepts of Compiler Design

12

Syntactic Analysis
The Syntactic Analysis is also called Parsing.
Tokens are grouped into grammatical phrases represented by a Parse Tree which gives a hierarchical structure to the source program. The hierarchical structure is expressed by recursive rules, called Productions.
Parse Tree: An Example

October -2009

Concepts of Compiler Design

13

Semantic Analysis
The Semantic Analysis phase checks the program for semantic errors (Type Checking) and gathers type information for the successive phases.

Type Checking: Check types of operands (possibly imposing type coercions); No real number as index for array; etc.

October -2009

Concepts of Compiler Design

14

Intermediate Code Generation


An intermediate code is generated as a program for an abstract machine. The intermediate code should be easy to translate into the target program. As intermediate code we consider the three-address code, similar to assembly: sequence of instructions with at most three operands such that: There is at most one operator, in addition to the assignment. Thus, we

make explicit the operators precedence.


Temporary names must be generated to compute intermediate operations.

October -2009

Concepts of Compiler Design

15

Code Optimization
This phase attempts to improve the intermediate code so that faster-running machine code can be obtained. Different compilers adopt different optimization techniques.

Code Generation
This phase generates the target code consisting of assembly code. 1. Memory locations are selected for each variable; 2. Instructions are translated into a sequence of assembly instructions;

3. Variables and intermediate results are assigned to memory registers.

October -2009

Concepts of Compiler Design

16

Summing Up

October -2009

Concepts of Compiler Design

17

Simplified Compiler Structure

October -2009

Concepts of Compiler Design

18

Compiler Construction Tools


For compiler construction there are special tools like : 1- Scanner generators 2- Parser generators 3- Syntax-directed translation engines 4- Code-generator generators

5- Data-flow analysis engines


6- Compiler-construction toolkits

October -2009

Concepts of Compiler Design

19

Report

Which one of language implementers(compiler & Interpreter) is faster than the other ? and where?

October -2009

Concepts of Compiler Design

20

Thank You! Any Questions?

October -2009

Concepts of Compiler Design

21

S-ar putea să vă placă și