1 comp 144 programming language concepts felix hernandez-campos lecture 2: compilation and...

16
COMP 144 Programming Language Concepts Felix Hernandez-Campos 1 Lecture 2: Lecture 2: Compilation and Compilation and Interpretation Interpretation COMP 144 Programming Language COMP 144 Programming Language Concepts Concepts Spring 2002 Spring 2002 Felix Hernandez-Campos Felix Hernandez-Campos Jan 11 Jan 11 The University of North Carolina at The University of North Carolina at Chapel Hill Chapel Hill

Upload: waylon-picot

Post on 14-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

11

Lecture 2: Lecture 2: Compilation and InterpretationCompilation and Interpretation

COMP 144 Programming Language ConceptsCOMP 144 Programming Language Concepts

Spring 2002Spring 2002

Felix Hernandez-CamposFelix Hernandez-Campos

Jan 11Jan 11

The University of North Carolina at Chapel HillThe University of North Carolina at Chapel Hill

Page 2: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

22

From Source Code to Executable CodeFrom Source Code to Executable Code

program gcd(input, output);program gcd(input, output);

var i, j: integer;var i, j: integer;

begin begin

read(i, j);read(i, j);

while i <> j dowhile i <> j do

if i > j then i := i – j;if i > j then i := i – j;

else j := j – i;else j := j – i;

writeln(i)writeln(i)

end.end.

program gcd(input, output);program gcd(input, output);

var i, j: integer;var i, j: integer;

begin begin

read(i, j);read(i, j);

while i <> j dowhile i <> j do

if i > j then i := i – j;if i > j then i := i – j;

else j := j – i;else j := j – i;

writeln(i)writeln(i)

end.end.

CompilationCompilation

Page 3: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

33

Compilation and InterpretationCompilation and Interpretation

• A A compilercompiler is a is a programprogram that that translatestranslates high-level high-level source programs into target programsource programs into target program

• An interpreter is a program that An interpreter is a program that executesexecutes another another program program

Page 4: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

44

Mixing Compilation and InterpretationMixing Compilation and Interpretation

• Fuzzy difference:Fuzzy difference:– A language is A language is interpretedinterpreted when the initial translation is when the initial translation is

simplesimple– A language is A language is compiledcompiled when the translation process is when the translation process is

complicatedcomplicated

Page 5: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

55

PreprocessingPreprocessing

• MacrosMacros– #define <macro> <replacement name>#define <macro> <replacement name>– #define FALSE 0#define FALSE 0– #define max(A,B) ( (A) > (B) ? (A):(B))#define max(A,B) ( (A) > (B) ? (A):(B))

Page 6: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

66

LinkingLinking

• Libraries of subroutinesLibraries of subroutines

Page 7: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

77

PortabilityPortability

• Assembly language instead of machine languageAssembly language instead of machine language

• Intermediate source codeIntermediate source code

Page 8: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

88

Programming EnvironmentsProgramming Environments

• Much more than compilers and interpretersMuch more than compilers and interpreters– Assemblers, debuggers, preprocessors and linkersAssemblers, debuggers, preprocessors and linkers– EditorsEditors– Pretty printersPretty printers– Style CheckersStyle Checkers– Version managementVersion management– ProfilersProfilers

• Integrated environmentsIntegrated environments– Beyond a simple Beyond a simple bus errorbus error– EmacsEmacs

Page 9: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

99

Overview of CompilationOverview of Compilation

program gcd(input, output);program gcd(input, output);

var i, j: integer;var i, j: integer;

begin begin

read(i, j);read(i, j);

while i <> j dowhile i <> j do

if i > j then i := i – j;if i > j then i := i – j;

else j := j – i;else j := j – i;

writeln(i)writeln(i)

end.end.

program gcd(input, output);program gcd(input, output);

var i, j: integer;var i, j: integer;

begin begin

read(i, j);read(i, j);

while i <> j dowhile i <> j do

if i > j then i := i – j;if i > j then i := i – j;

else j := j – i;else j := j – i;

writeln(i)writeln(i)

end.end.

CompilationCompilation

Page 10: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1010

Phases of CompilationPhases of Compilation

Page 11: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1111

ExampleExample

• From Scott’s class notesFrom Scott’s class notes

• Desk calculator languageDesk calculator language

• Example program:Example program:

read Aread A

read Bread B

sum := A + Bsum := A + B

write sumwrite sum

write sum / 2write sum / 2

Page 12: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1212

Lexical AnalysisLexical Analysis

• Tokens:Tokens:

idid = letter ( letter | digit ) * [ except "read" and "write" ] = letter ( letter | digit ) * [ except "read" and "write" ]

literalliteral = digit digit * = digit digit *

":=", "+", "-", "*", "/", "(", ")“":=", "+", "-", "*", "/", "(", ")“

$$$ [end of file]$$$ [end of file]

Page 13: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1313

Syntax AnalysisSyntax Analysis

• Grammar in EBNFGrammar in EBNF<pgm> -> <statement list> $$$<pgm> -> <statement list> $$$

<stmt list> -> <stmt list> <stmt> | E<stmt list> -> <stmt list> <stmt> | E

<stmt> -> id := <expr> | read <id> | write <expr><stmt> -> id := <expr> | read <id> | write <expr>

<expr> -> <term> | <expr> <add op> <term><expr> -> <term> | <expr> <add op> <term>

<term> -> <factor | <term> <mult op> <factor<term> -> <factor | <term> <mult op> <factor

<factor> -> ( <expr> ) | id | literal<factor> -> ( <expr> ) | id | literal

<add op> -> + | -<add op> -> + | -

<mult op> -> * | /<mult op> -> * | /

Page 14: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1414

Code GenerationCode Generation

• Intermediate code:Intermediate code: readread

pop Apop A

readread

pop Bpop B

push Apush A

push Bpush B

addadd

pop sumpop sum

push sumpush sum

writewrite

push sumpush sum

push 2push 2

divdiv

writewrite

Page 15: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1515

Code Generation Code Generation

• Target code:Target code: .data.data

A: .long 0A: .long 0

B: .long 0B: .long 0

sum: .long 0sum: .long 0

.text.text

main: jsr readmain: jsr read

movl d0,d1movl d0,d1

movl d1,Amovl d1,A

jsr readjsr read

movl d0,d1movl d0,d1

movl d1,Bmovl d1,B

movl A,d1movl A,d1

Page 16: 1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1616

Code GenerationCode Generation

movl B,d2movl B,d2

addl d1,d2addl d1,d2

movl d1,summovl d1,sum

movl sum,d1movl sum,d1

movl d1,d0movl d1,d0

jsr writejsr write

movl sum,d1movl sum,d1

movl #2,d2movl #2,d2

divsl d1,d2divsl d1,d2

movl d1,d0movl d1,d0

jsr writejsr write