Download presentation
Presentation is loading. Please wait.
Published byWaylon Picot Modified over 9 years ago
1
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos Jan 11 The University of North Carolina at Chapel Hill
2
2 COMP 144 Programming Language Concepts Felix Hernandez-Campos From Source Code to Executable Code program gcd(input, output); var i, j: integer; begin read(i, j); while i <> j do if i > j then i := i – j; else j := j – i; writeln(i)end. program gcd(input, output); var i, j: integer; begin read(i, j); while i <> j do if i > j then i := i – j; else j := j – i; writeln(i)end. Compilation
3
3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Compilation and Interpretation A compiler is a program that translates high-level source programs into target programA compiler is a program that translates high-level source programs into target program An interpreter is a program that executes another programAn interpreter is a program that executes another program
4
4 COMP 144 Programming Language Concepts Felix Hernandez-Campos Mixing Compilation and Interpretation Fuzzy difference:Fuzzy difference: –A language is interpreted when the initial translation is simple –A language is compiled when the translation process is complicated
5
5 COMP 144 Programming Language Concepts Felix Hernandez-Campos Preprocessing MacrosMacros –#define –#define –#define FALSE 0 –#define max(A,B) ( (A) > (B) ? (A):(B))
6
6 COMP 144 Programming Language Concepts Felix Hernandez-Campos Linking Libraries of subroutinesLibraries of subroutines
7
7 COMP 144 Programming Language Concepts Felix Hernandez-Campos Portability Assembly language instead of machine languageAssembly language instead of machine language Intermediate source codeIntermediate source code
8
8 COMP 144 Programming Language Concepts Felix Hernandez-Campos Programming Environments Much more than compilers and interpretersMuch more than compilers and interpreters –Assemblers, debuggers, preprocessors and linkers –Editors –Pretty printers –Style Checkers –Version management –Profilers Integrated environmentsIntegrated environments –Beyond a simple bus error –Emacs
9
9 COMP 144 Programming Language Concepts Felix Hernandez-Campos Overview of Compilation program gcd(input, output); var i, j: integer; begin read(i, j); while i <> j do if i > j then i := i – j; else j := j – i; writeln(i)end. program gcd(input, output); var i, j: integer; begin read(i, j); while i <> j do if i > j then i := i – j; else j := j – i; writeln(i)end. Compilation
10
10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Phases of Compilation
11
11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Example From Scott’s class notesFrom Scott’s class notes Desk calculator languageDesk calculator language Example program:Example program: read A read A read B read B sum := A + B sum := A + B write sum write sum write sum / 2 write sum / 2
12
12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lexical Analysis Tokens:Tokens: id = letter ( letter | digit ) * [ except "read" and "write" ] literal = digit digit * ":=", "+", "-", "*", "/", "(", ")“ $$$ [end of file]
13
13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Syntax Analysis Grammar in EBNFGrammar in EBNF -> $$$ -> $$$ -> | E -> | E -> id := | read | write -> id := | read | write -> | -> | -> -> <factor -> ( ) | id | literal -> ( ) | id | literal -> + | - -> + | - -> * | / -> * | /
14
14 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation Intermediate code:Intermediate code: read read pop A pop A read read pop B pop B push A push A push B push B add add pop sum pop sum push sum push sum write write push sum push sum push 2 push 2 div div write write
15
15 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation Target code:Target code:.data.data A:.long 0 A:.long 0 B:.long 0 B:.long 0 sum:.long 0 sum:.long 0.text.text main: jsr read main: jsr read movl d0,d1 movl d0,d1 movl d1,A movl d1,A jsr read jsr read movl d0,d1 movl d0,d1 movl d1,B movl d1,B movl A,d1 movl A,d1
16
16 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation movl B,d2 movl B,d2 addl d1,d2 addl d1,d2 movl d1,sum movl d1,sum movl sum,d1 movl sum,d1 movl d1,d0 movl d1,d0 jsr write jsr write movl sum,d1 movl sum,d1 movl #2,d2 movl #2,d2 divsl d1,d2 divsl d1,d2 movl d1,d0 movl d1,d0 jsr write jsr write
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.