T01740010220104019T0174-Pert 01-02

21

Transcript of T01740010220104019T0174-Pert 01-02

Page 1: T01740010220104019T0174-Pert 01-02
Page 2: T01740010220104019T0174-Pert 01-02

IntroductionSession 1-2

Course : T0174 - Technics CompilerYear : February 2011

Page 3: T01740010220104019T0174-Pert 01-02

Learning Outcomes At the end of this Session, the student will be

expected to:• Explain the importance of the use of compilers in

design of computer programs.

• Understanding the stages of the compilation process and elements that used in the compilation

Bina Nusantara University 3

Page 4: T01740010220104019T0174-Pert 01-02

Content Outline

• The Evolution of Programming Language• Understanding of compilation• Grouping of phases• Cousins of the compiler• Language Processing System• Stages compilation• Sample translation• Compiler construction tools

Bina Nusantara University 4

Page 5: T01740010220104019T0174-Pert 01-02

The Evolution of Programming Languages

• The first electronic computers appeared in the 1940's and were programmed machine language by sequences of 0's and 1's that explicitly told the computer what operations to execute and in what order.

• The operations themselves were very low level: move data from one location to another, add the contents of two registers, compare two values, and so on.

• Needless to say, this kind of programming was slow, tedious, and error prone. And once written, the programs were hard to understand and modify.

Bina Nusantara University 5

Page 6: T01740010220104019T0174-Pert 01-02

Basic understanding

• The process of translation in everyday life• The process of compiling with a computer• Compiler is a program that reads a program

written in a language->source language - and translate it into another language -> the target language.

Own language Translator Other Languages

Source Language Compiler Target language

Bina Nusantara University 6

Page 7: T01740010220104019T0174-Pert 01-02

Basic understanding• Source language of the high level language such

as PASCAL, C, C + +, FORTRAN, COBOL, etc.The target language can be either Assembly language, machine language, or other language of the source language.

• For example, translation from FORTRAN to C language, PASCAL language to Assembly, or combinations that may occur from the languages mentioned above.

Bina Nusantara University 7

Page 8: T01740010220104019T0174-Pert 01-02

Classification of compiler

• single pass• multi pass• load-and-go• Debugging• optimizing.

Bina Nusantara University 8

Page 9: T01740010220104019T0174-Pert 01-02

General description compiler

CompilerSourceProgram

TargetProgram

Error messages

Bina Nusantara University 9

Page 10: T01740010220104019T0174-Pert 01-02

Cousins of the compiler • Preprocessor

– To incorporate skeletal program that becomes an input to the compiler, and perform the functions of processing macros, file inclusion, rational preprocessor, and language extensions.

• Assembler – perform processing on the output of the compiler that produces

binary codes that do not have the memory address (memory addressing)

• Loader and link-editor – merge between relocatable machine code with the library or

relocatable object files to get absolute machine code.

Bina Nusantara University 10

Page 11: T01740010220104019T0174-Pert 01-02

Language Processing System

Compiler

Preprocessor

Assembler

Loader / Link-editor

skeletal Source program

source program

target assembly program

relocatable machine code

absolute machine code

Library, relocatable Object files

Bina Nusantara University 11

Page 12: T01740010220104019T0174-Pert 01-02

Preprocessor functions• Macro processing

– macro processing wird defined by programmer• File inclusion

– preprocessor would be to include the header files into the program• Rational preprocessor

– preprocessor can improve capability or add facilities of the language that had long (older language)

• Language extensions – combine commands that written in other languages into the

program in the form of a built-in macros.

Bina Nusantara University 12

Page 13: T01740010220104019T0174-Pert 01-02

Stages compilation :lexical analyzer

semantic analyzer

code optimizer

code generator

intermediate code generator

syntax analyzer

errorhandler

Target program(Object code)

symbol-table manager

Source program

Bina Nusantara University 13

Page 14: T01740010220104019T0174-Pert 01-02

Stages compilation• Phase analysis :

– lexical analyzer– syntax analyzer– semantic analyzer.

• Phase synthesis :– intermediate code generator– code optimizer– code generator.

• Symbol table manager – to make a record of the identifier used in the source program

and collect the attributes related to each identifier• Error Handler

– Part of the compiler to handle and report errors found.Bina Nusantara University 14

Page 15: T01740010220104019T0174-Pert 01-02

Translation example : position := initial +rate * 60

lexical analyzer

syntax analyzer

id1 := id2 + id3 * 60

Symbol Table1 position……..2 initial……..3 rate……..4

id1:=

*60

+id2

id3

id3

60

id1

:=

*inttoreal

+id2

semantic analyzer

intermediate code generator

Bina Nusantara University 15

Page 16: T01740010220104019T0174-Pert 01-02

Advanced examples ..

code generator

temp1 := inttoreal(60)temp2 := id3 * temp1temp3 := id2 + temp2id1 := temp3

temp1 := id3 * 60.0id1 := id2 + temp1

MOVF id3, R2MULF #60.0, R2MOVF id2 ,R1ADDF R2, R1MOVF R1, id1

intermediate code generator

code optimizer

Bina Nusantara University 16

Page 17: T01740010220104019T0174-Pert 01-02

Compiler construction tools • Parser Generator

– tools that generate Syntax Analyzer (parser), from input-based Context Free Grammar.

• Scanner Generator – tools generate lexical analyzer (scanner), from

regular expression based specifications.• Syntax Directed translation Engine • tools that can generate a set of routines that can 'walk'

(run) in the parse tree and produces intermediate code.

Bina Nusantara University 17

Page 18: T01740010220104019T0174-Pert 01-02

Compiler construction tools• Automatic Code Generator

– tools to retrieve a collection of rules that defines the translation of each operation from the intermediate language into machine language of the target machine.

• Data Flow Engine– tools to gather information about how values are

transmitted from one part of the program to other programs, for the purposes of "data flow analysis" in the code optimization phase.

Bina Nusantara University 18

Page 19: T01740010220104019T0174-Pert 01-02

Some commonly used compiler-construction tools include

1. Parser generators that automatically produce syntax analyzers from a grammatical description of a programming language. 2. Scanner generators that produce lexical analyzers from a regular-expres- sion description of the tokens of a language. 3. Syntax-directed translation engines that produce collections of routines for walking a parse tree and generating intermediate code. 4. Code-generator generators that produce a code generator from a

collection of rules for translating each operation of the intermediate language into the machine language for a target machine. 5. Data-flow analysis engines that facilitate the gathering of information about how values are transmitted from one part of a program to each other part. Data-flow analysis is a key part of code optimization. 6. Compiler-construction tools that provide an integrated set of routines for constructing various phases of a compiler. We shall describe many of these tools throughout book (Author : Aho, …)  

Bina Nusantara University 19

Page 20: T01740010220104019T0174-Pert 01-02

• Exercises for this Session

• Exercise 1:What is the difference between a compiler and an interpreter?

 • Exercise 2:What are the advantages of

(a) a compiler over an interpreter (b) an interpreter over a compiler?

• Exercise 3 : What advantages are there to a language-processing system in which the compiler produces assembly language rather than machine language?

Bina Nusantara University 20

Page 21: T01740010220104019T0174-Pert 01-02

• Exercise 4:

• A compiler that translates a high-level language into another high-level language is called a source-to-source translator.

• What advantages are there to using C as a target language for a compiler?

• Exercise 5 : Describe some of the tasks that an assembler needs to perform.

Bina Nusantara University 21