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

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

Post on 14-Dec-2015

213 views

Category:

Documents

0 download

TRANSCRIPT

  • Slide 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 Slide 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 ij 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 ij do if i > j then i := i j; else j := j i; writeln(i)end. Compilation Slide 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 Slide 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 Slide 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)) Slide 6 6 COMP 144 Programming Language Concepts Felix Hernandez-Campos Linking Libraries of subroutinesLibraries of subroutines Slide 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 Slide 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 Slide 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 ij 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 ij do if i > j then i := i j; else j := j i; writeln(i)end. Compilation Slide 10 10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Phases of Compilation Slide 11 11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Example From Scotts class notesFrom Scotts 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 Slide 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] Slide 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 -> | -> | -> ->( ) | id | literal -> ( ) | id | literal -> + | - -> + | - -> * | / -> * | / Slide 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 Slide 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 Slide 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

Recommended

View more >