34
Compiler Design Lecture 1 Sunday, October 3, 2010

Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Embed Size (px)

Citation preview

Page 1: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Compiler DesignLecture 1

Sunday, October 3, 2010

Page 2: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Problem in Real Life

Sunday, October 3, 2010

Page 3: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Problem in Real Life

Egyptian

Arabic

Sunday, October 3, 2010

Page 4: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Problem in Real Life

Egyptian British

Arabic English

Sunday, October 3, 2010

Page 5: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

?

Problem in Real Life

Egyptian British

Arabic English

Sunday, October 3, 2010

Page 6: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Solution

Egyptian British ?

Arabic English

Sunday, October 3, 2010

Page 7: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Solution

Egyptian British ?

Arabic English

Sol(1):No Translator (En)advantages:

•Faster interaction.disadvantages:

•Difficult (He must learn english).

Sunday, October 3, 2010

Page 8: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Solution

Egyptian British ?

Arabic English Sol(2):Translator (Ar)advantages:

•He can use his native language, easier.disadvantages:

•Slower (additional person).

Sol(1):No Translator (En)advantages:

•Faster interaction.disadvantages:

•Difficult (He must learn english).

Sunday, October 3, 2010

Page 9: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Problem in Computer World

You Computer

High level language Machine language

Sunday, October 3, 2010

Page 10: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

SolutionYou Computer

High level language

Machine language Sol(2):Translator (HL)

advantages:•You can use high level language.

✓Easier to debug.✓Readable.

disadvantages:•Need additional phase (translation).

Sol(1):No Translator (ML)advantages:

•Faster (no translation is required).disadvantages:

•Difficult,you must use machine language.

Sunday, October 3, 2010

Page 11: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Translator

Translates a program into a form in which it can be executed by a computer.

The most common kinds:

๏Compiler.

๏ Interpreter.

Sunday, October 3, 2010

Page 12: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Compiler

Compiler

Source Program

Target Program

Errors

Fig1. Translation

Sunday, October 3, 2010

Page 13: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Compiler

Compiler

Source Program

Target Program

Errors Target Program

Inputs

Outputs

Fig1. Translation Fig2. Execution

Sunday, October 3, 2010

Page 14: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Compiler

Compiler

Source Program

Target Program

Errors Target Program

Inputs

Outputs

Fig1. Translation Fig2. Execution

•It does translation only (Produces target program).•Target program maps inputs to outputs without having to compile the source program again.

Sunday, October 3, 2010

Page 15: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Interpreter

Interpreter

Source Program

Outputs

Errors Inputs

Fig1. Translation+Execution

Sunday, October 3, 2010

Page 16: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Interpreter

Interpreter

Source Program

Outputs

Errors Inputs

Fig1. Translation+Execution

•Translation + Execution (statement by statement)•Every time the source program is run, it will do translation.•Do translation whether or not there are changes in the source program.

Sunday, October 3, 2010

Page 17: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Processing TimeTarget program produced by compiler has less processing time (faster) than the interpreted source program.(True or false)?

Sunday, October 3, 2010

Page 18: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Processing TimeTarget program produced by compiler has less processing time (faster) than the interpreted source program.(True or false)?

True

Sunday, October 3, 2010

Page 19: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Processing TimeTarget program produced by compiler has less processing time (faster) than the interpreted source program.(True or false)?

Target program produce by compiler:Processing time=Execution time only-->small

processing time-->faster

True

Sunday, October 3, 2010

Page 20: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Processing TimeTarget program produced by compiler has less processing time (faster) than the interpreted source program.(True or false)?

Target program produce by compiler:Processing time=Execution time only-->small

processing time-->fasterInterpreted source program:

Processing time=Translation time+Execution time--> large processing time-->slower

True

Sunday, October 3, 2010

Page 21: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Language Processing SystemPrograms needed to create an executable generated code.

Sunday, October 3, 2010

Page 22: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Language Processing SystemPrograms needed to create an executable generated code.

Compiler

Source Program

Assembler

Linker

Assembly language program

Machine language module(s)

Loader

Library routines(ML)

Machine language program(exe)

Memory

Sunday, October 3, 2010

Page 23: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Assembler

Translate target program (assembly program) into machine language program.

Sunday, October 3, 2010

Page 24: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Linker

3

Object file format

• Object file header describes the size and position of the other pieces of the file

• Text segment contains the machine instructions• Data segment contains binary representation of data in

assembly file• Relocation info identifies instructions and data that depend

on absolute addresses• Symbol table associates addresses with external labels and

lists unresolved references• Debugging info

Object fileheader

Textsegment

Datasegment

Relocationinformation

Symboltable

Debugginginformation

Process for producing an executable file

Objectfile

Sourcefile Assembler

LinkerAssembler

Assembler Programlibrary

Objectfile

Objectfile

Sourcefile

Sourcefile

Executablefile

Sunday, October 3, 2010

Page 25: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Loader

Part of the OS that brings an executable file residing on disk into memory and starts its running.

Sunday, October 3, 2010

Page 26: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Compiler Structure

There are 2 parts(phases):

✓Analysis(Front-end):➡ Determines the operations implied by the source program which are

recorded in a tree structure.

➡ Collects information about the source program and stores it in a data structure called symbol table.

➡ Machine independent.

✓Synthesis(Back-end):➡ Constructs the target program from the intermediate representation and

information in symbol table.

➡ Machine dependent.

Sunday, October 3, 2010

Page 27: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Compiler StructureLexical Analyzer/Scanner

Syntax Analyzer/Parser

Semantic Analyzer

Intermediate Code Generator

Code Optimizer

Code Generator

Symbol Table

Sunday, October 3, 2010

Page 28: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Lexical Analyzer/ScannerPerforms lexical analysis (scanning).Steps:✓ Reads source program character by character from left to right.✓ Groups characters into lexeme.✓ For each lexeme, it produces a token.✓ Stores information about the token in the symbol table.

Sunday, October 3, 2010

Page 29: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Lexical Analyzer/ScannerPerforms lexical analysis (scanning).Steps:✓ Reads source program character by character from left to right.✓ Groups characters into lexeme.✓ For each lexeme, it produces a token.✓ Stores information about the token in the symbol table.

Source Programposition = initial + rate * 60

ScannerLexeme Tokenposition <id, 1>

= <=>initial <id ,2>

+ <+>rate <id, 3>

* <*>60 <60>

<id,1> <=> <id,2> <+> <id,3> <*> <60>

1 position ...

2 initial ...3 rate ...

Symbol Table

Sunday, October 3, 2010

Page 30: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Syntax Analyzer/Parser

Performs syntax analysis (parsing).

Checks if the token sequence is correct with respect to language specification.

If it is correct, it will build syntax tree using tokens as nodesIf it is not correct --> syntax error.

Example

Sunday, October 3, 2010

Page 31: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Semantic Analyzer

Uses syntax tree and the information in the symbol table.

Checks for semantic errors (type checking).

Possibly performs coercions.

Sunday, October 3, 2010

Page 32: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Intermediate Code GeneratorGenerates an intermediate code (machine independent).This code should be:✓easy to produce.✓easy to translate into the target language.

Example: three-address code✓assembly-like instructions✓at most three operands per instruction.✓at most one operator on RHS.✓temporary names hold the computed values.

Sunday, October 3, 2010

Page 33: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Code Optimizer

Improves the intermediate code so that faster and shorter target code that consumes less power can be obtained.

Reduce redundancy.

Example.

Sunday, October 3, 2010

Page 34: Compiler Design – Lecture 0 - ECED Mansoura · PDF fileCompiler Design Lecture 1 Sunday, October 3, 2010. ... Target program produced by compiler has less processing time (faster)

Code Generator

Maps (optimized) intermediate code into the target code.

Registers and memory locations are selected for each of the variables.

Example.

Sunday, October 3, 2010