Top Banner
Compilers Introduction Alex Aiken
37

Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Jul 19, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Compilers

Introduction

Alex Aiken

Page 2: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

Main Text Book:Compilers: Principles, Techniques & Tools, 2nd ed.,Aho, Lam, Sethi, and Ullman, 2007

Evaluation:Midterm Exam 32.5%Final Exam 32.5%Assignments and Quizzes 15%Project 20%

Page 3: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

• Compilers

• Interpreters

• a program that translates an executable program in a source language (usually high level)into an equivalent executable program in a target language(usually low level)

• a program that reads an executable program and produces the results of running that program

• usually, this involves executing the source program in some fashion

Page 4: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

• Compilers

CompilerSource

Program

Target

Program

Errors

Target ProgramInput Output

Page 5: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

• Interpreters

Interpreter

Input

Output

Source

Program

Translates line by line

Executes each translated line immediately

Execution is slower because translation is repeated

But, usually give better error diagnostics than a compiler

Page 6: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

• 1954 IBM develops the 704

– Successor to the 701

– customers found that the software costs exceeded the

hardware costs.

Alex Aiken

Page 7: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

• “Speedcoding”

JohnBackus

Image used with permission of The History of Computing Project(www.thocp.net)Alex Aiken

– an early example of an interpreter

– developed in 1953 by John Backus

– much faster way of developing programs

– programs were ten to twenty times slower

Page 8: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

• FORTRAN I

Image used with permission of The History of Computing Project(www.thocp.net)Alex Aiken

– FORmula TRANslation Project

– FORTRAN ran from 1954 To1957

– By 1958, over 50 percent of all of programs

were in FORTRAN

JohnBackus

Page 9: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Alex Aiken

Intro to Compilers

• The first compiler

– Huge impact on computer science

• Led to an enormous body of theoreticalwork

• Modern compilers preserve the outline of FORTRAN I

Page 10: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Alex Aiken

Intro to Compilers

1. LexicalAnalysis

2. Parsing

3. SemanticAnalysis

4. Optimization

5. Code Generation

Page 11: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Alex Aiken

Intro to Compilers

• First step: recognize words.

– Smallest unit aboveletters

This is a sentence.

Page 12: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Alex Aiken

Intro to Compilers

ist his ase nte nce

Page 13: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Alex Aiken

Intro to Compilers

• Lexical analysis divides program text into “words” or

“tokens”

if x == y then z = 1; else z = 2;

Page 14: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Alex Aiken

Intro to Compilers

• Once words are understood, the next step is tounderstand sentence structure

• Parsing = Diagramming Sentences

– The diagram is a tree

Page 15: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

This sentenceline is a longer

Alex Aiken

Page 16: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

This sentence

article

line is

noun verb

a longer

article adjective noun

Alex Aiken

Page 17: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

This sentence

article

line is

noun verb

a longer

article adjective noun

subject object

Alex Aiken

Page 18: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

This sentence

article

line is

noun verb

a longer

article adjective noun

subject object

sentence

Alex Aiken

Page 19: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

if x == y then z = 1; else z = 2;

if-then-elseAlex Aiken

Page 20: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

predicate else-stmtthen-stmt

if-then-elseAlex Aiken

if x == y then z = 1; else z = 2;

Page 21: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

x == y

relation

predicate

z 2

assign

else-stmt

z 1

assign

then-stmt

if-then-elseAlex Aiken

if x == y then z = 1; else z = 2;

Page 22: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Alex Aiken

Intro to Compilers

• Once sentence structure is understood, we can try to

understand “meaning”

– This is hard!

• Compilers perform limited semantic analysis to catch

inconsistencies

Page 23: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Alex Aiken

Intro to Compilers

• Example:

Jack said Jerry left his assignment at home.

• Even worse:

Jack said Jack left his assignment at home?

Page 24: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Alex Aiken

Intro to Compilers

• Programming languages

define strict rules to

avoid such ambiguities

{

int Jack = 3 ;

{

int Jack = 4;

cout << Jack;

}

}

Page 25: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Alex Aiken

Intro to Compilers

• Compilers perform many semantic checks besides variable bindings

• Example:

Jack left her homework at home.

• A “type mismatch” between her and Jack; we know they are different people

Page 26: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Alex Aiken

Intro to Compilers

• Optimization has no strong counterpart in English

– But a little bit likeediting

– But akin to editing

• Automatically modify programs so that they– Runfaster

– Use less memory

– Use less power

– Use less network messages, database accesses, etc.

Page 27: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Alex Aiken

Intro to Compilers

X = Y *0 is the same as X =0

NOT ALWAYS CORRECT

NaN

Page 28: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Alex Aiken

Intro to Compilers

• Produces assembly code (usually)

• A translation into another language

– Analogous to humantranslation

Page 29: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Alex Aiken

Intro to Compilers

• The overall structure of almost everycompiler

adheres to our outline

• The proportions have changed since FORTRAN

L P S O C

L P S O C

Page 30: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

Page 31: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

Errors

position = initial + rate * 60

lexical analyzer

syntax analyzer

semantic analyzer

intermediate code generator

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

:=

<id, 1><id, 2>

<id, 3>

+

*

60

:=

<id, 1>

<id, 2>

<id, 3>

+

*

inttofloat

60

Symbol Table

1 position ....

2 initial ….

3 rate….

Page 32: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

Intro to Compilers

Errors

intermediate code generator

code optimizer

final code generator

t1 := inttofloat(60)

t2 := id3 * t1

t3 := id2 + t2

id1 := t3

t1 := id3 * 60.0

id1 := id2 + t1

LDF R2, id3

MULF R2, R2, #60.0

LDF R1, id2

ADDF R1, R1, R2

STF id1, R1

1 position ....

2 initial ….

3 rate….

Symbol Table

3 address codes

Page 33: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

AlexAiken

Economy

• Why are there so many programming languages?

• Why are there new programminglanguages?

• What is a good programminglanguage?

Page 34: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

AlexAiken

Economy

Why are there so many programming languages?

Application domains have distinctive/conflictingneeds

o Scientific computing

• good floating point support

• good arrays

• parallelism

o Business applications

• persistence

• report generation

• data analysis

o Systems programming

• control of resources

• Real time constraints

Page 35: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

AlexAiken

Economy

Why are there new programminglanguages?

Claim:Programmertraining is thedominantcost fora programming language.

o widely used languages are slow to change

o it's easy to start a new language

o new languages are born to fill a void

New languages tend to look like old languages

o Java vsC++

Page 36: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

AlexAiken

Economy

What is a goodprogramming language?

o There is NO universally accepted metric for language

• Popularity? VB was once a very popular language

Page 37: Introduction to Programmingsharif.edu/~sani/courses/compiler/01-introduction.pdf · Intro toCompilers 1. LexicalAnalysis 2. Parsing 3. SemanticAnalysis 4. Optimization 5. CodeGeneration.

AlexAiken

Economy

Application domains have conflictingneeds.

Programmer training isthe dominant cost for a

programming language.