1 chapter 1. introduction. 2 outline language processors the structure of a compiler the evolution...

27
1 Chapter 1. Introduction

Upload: kathlyn-lee

Post on 28-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

1

Chapter 1. Introduction

Page 2: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

2

Outline

• Language Processors• The Structure of a Compiler• The Evolution of Programming

Languages• Why study principle of programming

languages

Page 3: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

3

Language Processors

• A compiler

Compiler

source program

target program

Page 4: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

4

• Running the target program

Target Programinput output

Page 5: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

5

• An interpreter– Much slower program execution– Better error diagnostics

Interpretersource program

inputoutput

Page 6: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

6

• A hybrid compiler, e.g. Java

Virtual Machine

inputoutput

Translator

source program

intermediate program

Page 7: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

7

Outline

• Language Processors• The Structure of a Compiler• The Evolution of Programming

Languages

• Why study principle of programming languages

Page 8: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

8

A Language Processing System

Compiler

source program

target machine code

Preprocessor

Assembler

Linker/Loader

modified source program

target assembly program

relocatable machine code

library filesrelocatable object files

Page 9: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

9

The Structure of a Compiler

• Analysis– Front end– Using a grammatical structure to create an

intermediate representation– Collecting information about the source

program in a symbol table

• Synthesis– Back end– Constructing the target program from the

intermediate representation and the symbol table

Page 10: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

10

Phases of a Compiler

Syntax Analyzer

character stream

target machine code

Lexical Analyzer

Intermediate Code Generator

Code Generator

token stream

syntax tree

intermediate representation

SymbolTable

Semantic Analyzer

syntax treeMachine-Independent

Code Optimization

Machine-Dependent Code Optimization

(optional)

(optional)

Page 11: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

11

Lexical Analysis (Scanning)

• Grouping characters into lexemes• E.g.

– position = initial + rate * 60– <id,1> <=> <id,2> <+> <id,3> <*>

<60>

Page 12: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

12

Syntax Analysis (Parsing)

• Creating a tree-like (e.g. syntax tree) intermediate representation that depicts the grammatical structure of the token streams– E.g.– <id,1> <=> <id,2> <+> <id,3> <*>

<60>–

=

<id, 1> +

<id, 2> *

<id, 3> 60

Page 13: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

13

Semantic Analysis

• Type checking• Type conversions or coercions• E.g.

– =

<id, 1> +

<id, 2> *

<id, 3>

60

int2float

Page 14: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

14

Intermediate Code Generation

• Generating a low-level intermediate representation– It should be easy to produce– It should be easy to translate into the

target machine– E.g. three-address code

t1 = int2float(60)t2 = id3 * t1t3 = id2 + t2id1 = t3

Page 15: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

15

Code Optimization

• Attempts to improve the intermediate code– Better: faster, shorter code, or code that

consumes less power (Chap. 8 -)– E.g.

• t1 = id3 * 60.0id1 = id2 + t1

Page 16: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

16

Code Generation

• Mapping intermediate representation of the source program into the target language (Chap. 8)– Machine code: register/memory location

assignments– E.g.

• LDF R2, id3MULF R2, R2, #60.0LDF R1, id2ADDF R1, R1, R2STF id1, R1

Page 17: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

17

Symbol Table Management

• To record the variable names and collect information about various attributes of each name– Storage, type, scope– Number and types of arguments,

method of argument passing, and the type returned

Page 18: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

18

Grouping of Phases into Passes

• Front-end pass– Lexical analysis, syntax analysis,

semantic analysis, intermediate code generation

• (Optional) Code optimization pass• Back-end pass

– Code generation

Page 19: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

19

Outline

• Language Processors• The Structure of a Compiler• The Evolution of Programming

Languages• Why study principle of programming

languages

Page 20: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

20

The Evolution of Programming Languages

• Machine language: 1940’s• Assembly language: early 1950’s• Higher-level languages: late 1950’s

– Fortran: scientific computation– Cobol: business data processing– Lisp: symbolic computation

• Today: thousands of programming languages

Page 21: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

21

Classification of Programming Languages – by Generation

• First generation: machine languages• Second generation: assembly languages• Third generation: high-level languages

– Fortran, Cobol, Lisp, C, C++, C#, Java• Fourth generation: specific application

– NOMAD, SQL, Postscript• Fifth generation: logic- and constraint-

based– Prolog, OPS5

Page 22: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

22

Classification of Programming Languages - by Functions

• Imperative: how– C, C++, C#, Java

• Declarative: what– ML, Haskell, Prolog

• von Neumann language– Fortran, C

• Object-oriented language– Simula 67, Smalltalk, C++, C#, Java, Ruby

• Scripting languages– Awk, JavaScript, Perl, PHP, Python, Ruby, Tcl

Page 23: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

23

Impacts on Compilers

• To translate and support new language features

• To take advantage of new hardware capabilities

• To promote the use of high-level languages by minimizing the execution overhead

• To make high-performance computer architectures effective on users’ applications

• To evaluate architectural concepts

Page 24: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

24

Outline

• Language Processors• The Structure of a Compiler• The Evolution of Programming

Languages• Why study principle of programming

languages

Page 25: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

25

Why study principle of programming languages?

• Become a better software engineer– Understand how to use language features– Appreciate implementation issues

• Better background for language selection– Familiar with range of languages– Understand issues / advantages /

disadvantages

• Better able to learn languages– You might need to know a lot

Page 26: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

26

Why study programming languages?

• Better understanding of implementation issues– How is “this feature” implemented?– Why does “this part” run so slowly?

• Better able to design languages– Those who ignore history are bound to

repeat it…

Page 27: 1 Chapter 1. Introduction. 2 Outline Language Processors The Structure of a Compiler The Evolution of Programming Languages Why study principle of programming

27

End of Chapter 1