chapter 6 programming languages and their translators
TRANSCRIPT
©
Christian Jacob
Chapter Overview
Chapter 6
Programming Languagesand their Translators
6.1 Interpreters versus Compilers
6.2 How Compilers Produce Machine Code
6.2.1 Stages of Translating a C++ Program
6.2.2 Components of a Compiler
6.3 Describing Syntactic Structures
6.3.1 The Backus-Naur Form (BNF)
6.3.2 Semantics (“Meaning”) Derived from Syntactic Structures
6.4 An Overview of Programming Languages
6.5 References
Page 2 Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
guage translators:
ehaviour”)
software
e, which can then
(vs. interpreted)
rogrammers have formation)
First Back TOC
6.1 Interpreters versus Compilers
There are two general kinds of programming lan
• Interpreters:
transform programs directly into command (“bsequences that run on a virtual machine
+ good for
rapid prototyping
of experimental
+ performs error checking at runtime
•
Compilers
:
translate programs into low-level machine codrun on an
actual machine
+ compiled machine code is generally faster
- compilation itself takes time
- to enable error checking
before
runtime, pto provide more information (e.g., type in
Page 3 Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
ne Code
First Back TOC How Compilers Produce Machine Code
6.2 How Compilers Produce Machi
An example C++ program, ready to be compiled:
#include <iostream.h>
void main(){
int a;float b, c;
a = 2; b = 3.1415;c = a * b;
cout << c << endl;}
Page 4
Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
Preprocessor
Compiler
Linker
Loader
First Back TOC How Compilers Produce Machine Code
6.2.1 Stages of Translating a C++ Program
Source code
Expanded source code
Object code
Relocatable executable code
Executing program
myProgram.cc
myProgram.o
a.outmyProgram
Page 5
Chapter 6: Programming Languages and their Translators
© Christian Jacob
Prev Next Last
Correct error(s)
yes
yes
First Back TOC How Compilers Produce Machine Code
Stages of Translating a C++ Program (Part 1)
Source code
Expanded source code
Preprocessor
Compiler
Prep. error(s)?
Compiler error(s)?
Object code
Library header files
no
no
Page 6 Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
e
yes
Free up memory
First Back TOC How Compilers Produce Machine Code
Stages of Translating a C++ Program (Part 2)
Object code
Relocatable executable cod
Linker
Loader
Linker error(s)?
Loader error(s)?
Executing program
Library object code
no
no
Page 7 Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
Linker
a.out
g++ Program.cc
ader files
First Back TOC How Compilers Produce Machine Code
An Alternative View of the Translation Stages
Program Input
Texteditor
Program.cc
Program.oC++ Compiler
Execution of
a.out
Loader
Input Data
Results / Output
Runtime system
Preprocessor
Library he
Page 8 Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
<streamio.h>
).
include files.
(syntax analysis).
antic analysis).
ic analysis).
First Back TOC How Compilers Produce Machine Code
• Preprocessor:
- Looks for compiler directives (e.g., #include
- Inserts predefined code and macros from the
⇒
Produces
expanded source code
.
•
Compiler:
- Checks the expanded code for syntax errors
- Checks for missing variable declarations (
sem
- Checks for missing type information (
semant
- … and much more (see for details later!)
⇒
Produces
object code
.
Page 9 Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
ed for the program for input/output,
h relative
ode is called base
addresses with
First Back TOC How Compilers Produce Machine Code
• Linker:
- Links the object code with other code requirto run (library object code; e.g., specific codemathematical functions, etc.)
⇒ Produces relocatable executable code witaddressing.
•
Loader
:
- Loads executable code into memory.
- The actual starting address of the program c
address.
- All addresses within the program are
relative
respect to the base address.
⇒
Runs the program.
Page 10 Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
fy the “words”
the “sentences”
heir structures
ning (semantics)
ctuation marks.
First Back TOC How Compilers Produce Machine Code
6.2.2 Components of a Compiler
A simple view of a compiler
Scanning
Parsing
Generating executable code
Tokens
Syntactic structures
Identi
Identify
and t
⇒ mea
and pun
Page 11 Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
orCode
optimizer
Y N T H E S I S
Objectprogram
ented
First Back TOC How Compilers Produce Machine Code
A more detailed view of a compiler
Shared Data / Tables
Lexicalanalyzer
Syntacticanalyzer
Semanticanalyzer
Codegenerat
A N A L Y S I S S
Sourceprogram
source code / language dependent machine-ori
Scanning Parsing
Page 12
Chapter 6: Programming Languages and their Translators
©
Christian Jacob
Prev Next Last
s
ibe syntactic ) and syntactic
ence of words:
ibing syntax.
First Back TOC Describing Syntactic Structures
6.3 Describing Syntactic Structure
6.3.1 The Backus-Naur Form (BNF)
The BNF1 was invented in the late 1950s to descrstructures of programming languages (ALGOL 60structures in general.
The BNF works like a word replacement system
:
a
→
b | c | a a
Applying this rule (once per line) produces a sequ
aa aa a a (This is just one possibility!)b c b
1. John Backus and Peter Naur were members of an international committee to develop a precise notation for descr
Page 13 Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
ctures
tence_sequence
redicate_part
ntribute
nttribute
ct
e noun
First Back TOC Describing Syntactic Structures
Example (1): Description of English sentence stru
sentence_sequence → sentence .
sen
|
sentence
.
sentence
→
subject_part
p
subject_part
→
extended_nou
|
noun_with_at
noun_with_attribute
→
extended_nou
|
second_case_a
predicate_part
→
predicate
obje
predicate
→
verb
object
→
article
noun
|
article
adjectiv
Page 14 Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
ssistants
First Back TOC Describing Syntactic Structures
extended_noun → The students | The teaching a
second_case_attribute →
of CPSC 231
|
of CPSC 233
verb
→
like
|
enjoy
article
→
the
adjective
→
weekly
|
daily
noun
→
lecture
|
chat
|
learning
|
labs
Page 15 Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
weekly labs
part
object
adjective noun
..
First Back TOC Describing Syntactic Structures
The students of CPSC 231 enjoy
extended_noun
noun_with_attribute
predicate_
article
sentence_sequence
subject_part
predicate
sentence
the
second_case_attribute verb
Page 16
Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
ructures
ath.h | …
tion; |tion; declarations
s
er |er, identifiers
First Back TOC Describing Syntactic Structures
Example (2): Description of basic C++ program st
program → includesdeclarationsmain
includes → #include <lib
>
lib
→
iostream.h
|
m
declarations
→
single_declara
single_declara
single_declaration
→
type
identifier
type
→
float
|
int
| …
identifiers
→
single_identifi
single_identifi
Page 17 Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
ion; |ion; instructions
|n |
pression |
First Back TOC Describing Syntactic Structures
main → main() { declarations instructions
return(0)};
instructions
→
single_instruct
single_instruct
single_instruction
→
assignment
|
cin_instruction
cout_instructio
conditional
|
loop
| …
assignment
→
identifier
=
ex
identifier
=
…
(etc., this example grammar is not complete)
Page 18 Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
Syntactic
* (9 + 8 / 2)
| | | 6 7 8 9
First Back TOC Describing Syntactic Structures
6.3.2 Semantics (“Meaning”) Derived fromStructures
Example: analysing the arithmetic expression 12
BNF for arithmetic expressions:
Expression:
→
| |
Term:
→
| |
Factor:
→
|
Number:
→
|
Digit:
→
| | | | | |
E T E+ T E– T
T F * T F / T F
F E( ) N
N DN D
D 0 1 2 3 4 5
Page 19 Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
(9 + 8 / 2) looks as
st (non-terminal)
D * T 12 * T → →
12 * N E+( ) → →
9 F /T+( ) →
12 * 9 8/F+( ) → →
First Back TOC Describing Syntactic Structures
A derivation for the arithmetic expression 12 *follows:
This is only one of many possible derivations!
Here we used a left derivation, i.e., the left-mosymbol is always expanded first.
E T F * T N * T DN * T 1N * T 1→ → → → → →
12 * F 12 * E( ) 12 * T E+( ) 12 * F E+( )→ → →
12 * D E+( ) 12 * 9 E+( ) 12 * 9 T+( ) 12 *→ → →
12 * 9 N /T+( ) 12 * 9 D/T+( ) 12 * 9 8/T+( )→ →
12 * 9 8/N+( ) 12 * 9 8/D+( ) 12 * 9 8/2+( )→ →
Page 20 Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
First Back TOC Describing Syntactic Structures
A Parse Tree for 12 * ( 9 + 8 / 2 )
E
T
F * T
N
N
F
( E )
+T E
T
F / T
N
D
8
F
N
D
2
F
N
D
9
1 D
2
D
The syntax tree provides semantic information:
• priority of ‘*’ and ‘/’ before ‘+’ and ‘-’.
• same priority: compute from left to right
Page 21
Chapter 6: Programming Languages and their Translators © Christian Jacob
Prev Next Last
Languages
First Back TOC An Overview of Programming Languages
6.4 An Overview of Programming
Page 22
Chapter 6: Programming Languages and their Translators
©
Christian Jacob
Prev Next Last
, Boston, MA: WCB/ 4.5.
First Back TOC References
6.5 References
• G. Blank and R. Barnes, The Universal MachineMcGraw-Hill, 1998. Chapters 4.1, 4.2, 4.4.2, and