Languages and Compilers (Sprog Og Oversættere) : Bent Thomsen Department of Computer Science Aalborg University
Languages and Compilers (Sprog Og Oversættere) : Bent Thomsen Department of Computer Science Aalborg University
(SProg og Oversættere)
Bent Thomsen
Department of Computer Science
Aalborg University
2
The “Phases” of a Compiler
Source Program
Code Generation
Object Code
3
Different Phases of a Compiler
The different phases can be seen as different
transformation steps to transform source code into
object code.
The different phases correspond roughly to the different
parts of the language specification:
• Syntax analysis <-> Syntax
• Contextual analysis <-> Contextual constraints
• Code generation <-> Semantics
4
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
This program
program isis useless
useless except
except for
for
!! illustration
illustration
let
let var
var n:
n: integer;
integer;
var
var c:
c: char
char
in
in begin
begin
cc :=
:= ‘&’;
‘&’;
nn :=
:= n+1
n+1
end
end
5
1) Syntax Analysis
Source Program
6
1) Syntax Analysis -> AST
Program
LetCommand
SequentialCommand
SequentialDeclaration AssignCommand
AssignCommand BinaryExpr
Contextual analysis:
• Scope checking: verify that all applied occurrences of
identifiers are declared
• Type checking: verify that all operations in the program are
used according to their type rules.
Annotate AST:
• Applied identifier occurrences => declaration
• Expressions => Type
8
2) Contextual Analysis -> Decorated AST
Program
LetCommand
SequentialCommand
SequentialDeclaration AssignCommand
Example 2:
foo
10
3) Code Generation
Code Generation
Object Code
n Integer
12
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.
13
Single Pass Compiler
A single pass compiler makes a single pass over the source text,
parsing, analyzing and generating code all at once.
Syntactic Analyzer
calls calls
14
Multi Pass Compiler
A multi pass compiler makes several passes over the program. The
output of a preceding phase is stored in a data structure and used by
subsequent phases.
15
Example: The Triangle Compiler Driver
public
public class
class Compiler
Compiler {{
public
public static
static void
void compileProgram(...)
compileProgram(...) {{
Parser
Parser parser
parser == new
new Parser(...);
Parser(...);
Checker
Checker checker
checker == new
new Checker(...);
Checker(...);
Encoder
Encoder generator
generator == new
new Encoder(...);
Encoder(...);
Program
Program theAST
theAST == parser.parse();
parser.parse();
checker.check(theAST);
checker.check(theAST);
generator.encode(theAST);
generator.encode(theAST);
}}
public
public void
void main(String[]
main(String[] args)
args) {{
...
... compileProgram(...)
compileProgram(...) ...
...
}}
}}
16
Compiler Design Issues
18
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;
19
Language Issues
Example Pascal:
– Every identifier must be declared before it is used.
– How to handle mutual recursion then?
Class Example {
void inc() { n = n + 1; }
int n;
void use() { n = 0 ; inc(); }
}
21
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
22