Professional Documents
Culture Documents
Programming Languages and Their Translators: 6.1 Interpreters Versus Compilers 6.2 How Compilers Produce Machine Code
Programming Languages and Their Translators: 6.1 Interpreters Versus Compilers 6.2 How Compilers Produce Machine Code
Programming Languages and Their Translators: 6.1 Interpreters Versus Compilers 6.2 How Compilers Produce Machine Code
Chapter 6
Programming Languages
and their Translators
Chapter Overview
Page 2 Chapter 6: Programming Languages and their Translators © Christian Jacob
#include <iostream.h>
void main()
{
int a;
float b, c;
a = 2; b = 3.1415;
c = a * b;
First Back TOC How Compilers Produce Machine Code Prev Next Last
Page 4 Chapter 6: Programming Languages and their Translators © Christian Jacob
Preprocessor
Compiler
Linker
Loader
myProgram
a.out Executing program
First Back TOC How Compilers Produce Machine Code Prev Next Last
Page 5 Chapter 6: Programming Languages and their Translators © Christian Jacob
yes
Prep. error(s)?
no
Compiler
yes
Compiler error(s)?
no
Object code
First Back TOC How Compilers Produce Machine Code Prev Next Last
Page 6 Chapter 6: Programming Languages and their Translators © Christian Jacob
Object code
yes
Linker error(s)?
no
Free up memory
Loader
Loader error(s)?
no
Executing program
First Back TOC How Compilers Produce Machine Code Prev Next Last
Page 7 Chapter 6: Programming Languages and their Translators © Christian Jacob
Program Input
Texteditor
g++ Program.cc
Execution of
a.out Loader a.out
Results / Output
First Back TOC How Compilers Produce Machine Code Prev Next Last
Page 8 Chapter 6: Programming Languages and their Translators © Christian Jacob
• Preprocessor:
- Looks for compiler directives (e.g., #include <streamio.h>).
- Inserts predefined code and macros from the include files.
⇒ Produces expanded source code.
• Compiler:
- Checks the expanded code for syntax errors (syntax analysis).
- Checks for missing variable declarations (semantic analysis).
- Checks for missing type information (semantic analysis).
- … and much more (see for details later!)
⇒ Produces object code.
First Back TOC How Compilers Produce Machine Code Prev Next Last
Page 9 Chapter 6: Programming Languages and their Translators © Christian Jacob
• Linker:
- Links the object code with other code required for the program
to run (library object code; e.g., specific code for input/output,
mathematical functions, etc.)
⇒ Produces relocatable executable code with relative
addressing.
• Loader:
- Loads executable code into memory.
- The actual starting address of the program code is called base
address.
- All addresses within the program are relative addresses with
respect to the base address.
⇒ Runs the program.
First Back TOC How Compilers Produce Machine Code Prev Next Last
Page 10 Chapter 6: Programming Languages and their Translators © Christian Jacob
Tokens
First Back TOC How Compilers Produce Machine Code Prev Next Last
Page 11 Chapter 6: Programming Languages and their Translators © Christian Jacob
Source Object
program program
ANALYSIS SYNTHESIS
Scanning Parsing
Lexical Syntactic Semantic Code Code
analyzer analyzer analyzer generator optimizer
First Back TOC How Compilers Produce Machine Code Prev Next Last
Page 12 Chapter 6: Programming Languages and their Translators © Christian Jacob
a → b|c|aa
a
aa
aaa (This is just one possibility!)
bcb
1. John Backus and Peter Naur were members of an international committee to develop a precise notation for describing syntax.
subject_part → extended_noun
| noun_with_attribute
noun_with_attribute → extended_noun
| second_case_attribute
predicate → verb
verb → like
| enjoy
article → the
adjective → weekly
| daily
noun → lecture
| chat
| learning
| labs
subject_part predicate_part
sentence
sentence_sequence
program → includes
declarations
main
declarations → single_declaration; |
single_declaration; declarations
identifiers → single_identifier |
single_identifier, identifiers
main → main() {
declarations
instructions
return(0)
};
instructions → single_instruction; |
single_instruction; instructions
single_instruction → assignment |
cin_instruction |
cout_instruction |
conditional |
loop | …
Expression: E → T +E | T –E | T
Term: T → F*T | F/T | F
Factor: F → (E ) | N
Number: N → DN | D
Digit: D → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
E → T → F * T → N * T → DN * T → 1N * T → 1D * T → 12 * T →
12 * F → 12 * ( E ) → 12 * ( T + E ) → 12 * ( F + E ) → 12 * ( N + E ) →
12 * ( D + E ) → 12 * ( 9 + E ) → 12 * ( 9 + T ) → 12 * ( 9 + F/T ) →
12 * ( 9 + N /T ) → 12 * ( 9 + D/T ) → 12 * ( 9 + 8/T ) → 12 * ( 9 + 8/F ) →
12 * ( 9 + 8/N ) → 12 * ( 9 + 8/D ) → 12 * ( 9 + 8/2 )
F * T
N F
D N ( E )
1 D T + E
2 F T
N F / T
The syntax tree provides
semantic information: D N F
• priority of ‘*’ and ‘/’
before ‘+’ and ‘-’. 9 D N
6.5 References