spof03-2-2
TRANSCRIPT
-
8/8/2019 SPOF03-2-2
1/22
1
Languages and Compilers
(SProg og Oversttere)
Bent Thomsen
Department of Computer ScienceAalborg University
With acknowledgement to Norm Hutchinson whos slides this lecture is based on.
-
8/8/2019 SPOF03-2-2
2/22
2
Compilation
So far we have treated language processors (including
compilers) as black boxes
Now we take a first look "inside the box": how are
compilers built.
And we take a look at the different phases and their
relationships
-
8/8/2019 SPOF03-2-2
3/22
3
The Phases of a Compiler
Syntax Analysis
Contextual Analysis
Code Generation
Source Program
Abstract Syntax Tree
Decorated Abstract Syntax Tree
Object Code
Error Reports
Error Reports
-
8/8/2019 SPOF03-2-2
4/22
-
8/8/2019 SPOF03-2-2
5/22
5
Example Program
We now look at each of the three different phases in a little
more detail. We look at each of the steps in transforming
an example Triangle program into TAM code.
! This program is useless except for
! illustration
let var n: integer;
var c: char
in begin
c := &;n := n+1
end
-
8/8/2019 SPOF03-2-2
6/22
6
1) Syntax Analysis
Syntax Analysis
Source Program
Abstract Syntax Tree
Error Reports
Note: Not all compilers construct an
explicit representation of an AST. (e.g. on
a single pass compiler generally no need
to construct an AST)
-
8/8/2019 SPOF03-2-2
7/22
7
1) Syntax Analysis -> AST
Program
LetCommand
SequentialDeclaration
n Integer c Char c & n n + 1
Ident Ident Ident Ident Ident Ident Ident OpChar.Lit Int.Lit
SimpleT
VarDecl
SimpleT
VarDecl
SimpleV
Char.Expr
SimpleV
VNameExp Int.Expr
AssignCommand BinaryExpr
SequentialCommand
AssignCommand
-
8/8/2019 SPOF03-2-2
8/22
8
2) Contextual Analysis -> Decorated AST
Contextual Analysis
Decorated Abstract Syntax Tree
Error Reports
Abstract Syntax Tree
Contextual analysis:
Scope checking: verify that all applied occurrences ofidentifiers are declared
Type checking: verify that all operations in the program areused according to their type rules.
Annotate AST:
Applied identifier occurrences => declaration
Expressions => Type
-
8/8/2019 SPOF03-2-2
9/22
9
2) Contextual Analysis -> Decorated AST
Program
LetCommand
SequentialDeclaration
n
Ident Ident Ident Ident
SimpleT
VarDecl
SimpleT
VarDecl
Integer c Char c & n n + 1
Ident Ident Ident OpChar.Lit Int.Lit
SimpleV
Char.Expr
SimpleV
VNameExp Int.Expr
AssignCommand BinaryExpr
SequentialCommand
AssignCommand
:char:char
:int
:int:int :int
-
8/8/2019 SPOF03-2-2
10/22
10
Contextual Analysis
Finds scope and type errors.
AssignCommand
:char
Example 1:
:int
***TYPEERROR (incompatible types in
assigncommand)
Example 2:
foo
Ident
SimpleV
foo not found
***SCOPEERROR: undeclared variable foo
-
8/8/2019 SPOF03-2-2
11/22
11
3) Code Generation
Assumes that program has been thoroughly
checked and is well formed (scope & type rules) Takes into account semantics of the source
language as well as the target language.
Transforms source program into target code.
Code Generation
Decorated Abstract Syntax Tree
Object Code
-
8/8/2019 SPOF03-2-2
12/22
12
3) Code Generation
let var n: integer;
var c: char
in begin
c := &;
n := n+1end
PUSH 2
LOADL 38
STORE 1[SB]
LOAD 0
LOADL 1CALL add
STORE 0[SB]
POP 2
HALT
n
Ident Ident
SimpleT
VarDecl
Integer
address = 0[SB]
-
8/8/2019 SPOF03-2-2
13/22
13
Compiler Passes
A pass is a complete traversal of the source program, or
a complete traversal of some internal representation of
the source program.
A pass can correspond to a phase but it does not have
to! Sometimes a single pass corresponds to several phases
that are interleaved in time.
What and how many passes a compiler does over the
source program is an important design decision.
-
8/8/2019 SPOF03-2-2
14/22
14
Single Pass Compiler
Compiler Driver
Syntactic Analyzer
calls
calls
Contextual Analyzer Code Generator
calls
Dependency diagram of a typical Single Pass Compiler:
A single pass compiler makes a single pass over the source text,parsing, analyzing and generating code all at once.
-
8/8/2019 SPOF03-2-2
15/22
15
Multi Pass Compiler
Compiler Driver
Syntactic Analyzer
callscalls
Contextual Analyzer Code Generator
calls
Dependency diagram of a typical Multi Pass Compiler:
A multi pass compiler makes several passes over the program. Theoutput of a preceding phase is stored in a data structure and used by
subsequent phases.
input
Source Text
output
AST
input output
Decorated AST
input output
Object Code
-
8/8/2019 SPOF03-2-2
16/22
16
Example:The Triangle Compiler Driver
public class Compiler {
public static void compileProgram(...) {Parser parser = new Parser(...);Checker checker = new Checker(...);Encoder generator = new Encoder(...);
Program theAST = parser.parse();
checker.check(theAST);generator.encode(theAST);
}public void main(String[] args) {
... compileProgram(...) ...}
}
-
8/8/2019 SPOF03-2-2
17/22
17
Compiler Design Issues
Single Pass Multi Pass
Speed
Memory
Modularity
Flexibility
Global optimization
Source Language
better worse
better for
large programs
(potentially) better
for small programsworse better
betterworse
impossible possible
single pass compilers are not possible
for many programming languages
-
8/8/2019 SPOF03-2-2
18/22
18
Language Issues
Example Pascal:
Pascal was explicitly designed to be easy to implement
with a single pass compiler:
Every identifier must be declared before it is first use.
var n:integer;
procedure inc;
begin
n:=n+1
end
Undeclared Variable!
procedure inc;
begin
n:=n+1
end;
var n:integer;
?
-
8/8/2019 SPOF03-2-2
19/22
19
Language Issues
Example Pascal:
Every identifier must be declared before it is used.
How to handle mutual recursion then?
procedure ping(x:integer)
begin
... pong(x-1); ...
end;
procedure pong(x:integer)
begin
... ping(x); ...
end;
-
8/8/2019 SPOF03-2-2
20/22
20
Language Issues
Example Pascal:
Every identifier must be declared before it is used.
How to handle mutual recursion then?
forward procedure pong(x:integer)
procedure ping(x:integer)
begin
... pong(x-1); ...
end;
procedure pong(x:integer)
begin
... ping(x); ...
end;
OK!
-
8/8/2019 SPOF03-2-2
21/22
21
Language Issues
Example Java:
identifiers can be declared before they are used.
thus a Java compiler need at least two passes
Class Example {
void inc() { n = n + 1; }
int n;
void use() { n = 0 ; inc(); }
}
-
8/8/2019 SPOF03-2-2
22/22
22
Keep in mind
There are many issues influencing the design of a new
programming language: Choice of paradigm
Syntactic preferences
Even the compiler implementation
e.g no of passes available tools
There are many issues influencing the design of new
compiler: No of passes
The source, target and implementation language
Available tools