Click here to load reader
Upload
stephen-stephens
View
18
Download
2
Embed Size (px)
DESCRIPTION
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. Compilation. From Source Code to Executable Code. program gcd(input, output); var i, j: integer; begin - PowerPoint PPT Presentation
Citation preview
11
Compilation and InterpretationCompilation and Interpretation(Sections 1.4-1.6)(Sections 1.4-1.6)
CSCI 431 Programming Languages CSCI 431 Programming Languages
Fall 2003Fall 2003
A modification of slides developed by Felix A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill Hernandez-Campos at UNC Chapel Hill
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
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
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
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))
66
LinkingLinking
• Libraries of subroutinesLibraries of subroutines
77
PortabilityPortability
• Assembly language instead of machine languageAssembly language instead of machine language
• Intermediate source codeIntermediate source code
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
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
1010
Phases of CompilationPhases of Compilation
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
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]
1313
Syntax Analysis: The GrammarSyntax Analysis: The Grammar
• 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> -> * | /
1414
Syntactic and Semantic AnalysisSyntactic and Semantic Analysis
• Derive parse tree in classDerive parse tree in classhttp://www.http://www.cscs..uncaunca..eduedu/~/~brucebruce/Fall03/csci431/slides//Fall03/csci431/slides/
parseTreeparseTree.doc.doc
• Derive abstract syntax tree in classDerive abstract syntax tree in class
1515
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
1616
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
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