Download presentation
Presentation is loading. Please wait.
Published byHector Chase Modified over 9 years ago
1
1 Compilation and Interpretation (Sections 1.4-1.6) Compilation and Interpretation (Sections 1.4-1.6) CSCI 431 Programming Languages Fall 2003 A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill
2
2 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 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 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 Preprocessing MacrosMacros –#define –#define –#define FALSE 0 –#define max(A,B) ( (A) > (B) ? (A):(B))
6
6 Linking Libraries of subroutinesLibraries of subroutines
7
7 Portability Assembly language instead of machine languageAssembly language instead of machine language Intermediate source codeIntermediate source code
8
8 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 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 Phases of Compilation
11
11 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 Lexical Analysis Tokens:Tokens: id = letter ( letter | digit ) * [ except "read" and "write" ] literal = digit digit * ":=", "+", "-", "*", "/", "(", ")“ $$$ [end of file]
13
13 Syntax Analysis: The Grammar Grammar in EBNFGrammar in EBNF -> $$$ -> $$$ -> | E -> | E -> id := | read id | write -> id := | read id | write -> | -> | -> ( ) | id | literal -> ( ) | id | literal -> + | - -> + | - -> * | / -> * | /
14
14 Syntactic and Semantic Analysis Derive parse tree in classDerive parse tree in class http://www.cs.unca.edu/~bruce/Fall03/csci431/slides/parseT ree.doc http://www.cs.unca.edu/~bruce/Fall03/csci431/slides/parseT ree.doc Derive abstract syntax tree in classDerive abstract syntax tree in class
15
15 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
16
16 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 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
© 2025 SlidePlayer.com. Inc.
All rights reserved.