Documente Academic
Documente Profesional
Documente Cultură
Compilers
And
Interpreters
Intermediate
Lexical Syntax code Code
analyzer analyzer generator generator
(semantic
analyzer)
Computer
Symbol table
|f|a|h|r|e|n|h|e|i|t|:|=|3|2|+|c|e|l|s| I|o|u|s|*|1|.|8|;|
:=
name attribute
id1 +
int32 *
id1 +
int32 *
:=
id1 +r
inttoreal *r
id1 +r
inttoreal *r
Temp1 := inttoreal(32)
Temp2 := id2 Intermediate code
Temp2 := Temp2 * 1.8
Temp1 := Temp1 + Temp2
id1 := Temp1
Symbol Table
Code optimizer
1
fahrenheit real
2 celsious real
Temp1 := id2
Temp1 := Temp1 * 1.8 optimized code
Temp1 := Temp1 + 32.0
id1 := Temp1
Symbol Table
Code generator
1
fahrenheit real
2 celsious real
movf id2, r1
mulf #1.8, r1 assembly instructions
addf #32.0, r1
movf r1, id1
Syntax analyzer:
Takes lexical units from the lexical analyzer and use them to construct
a hierarchical structure called parse tree
Optimization:
Makes programs smaller or faster or both.
Symbol Table
1 fahrenheit real
celsious real
2
Libraries
O.S. routines
(I/O routines)
Computer
Background:
Early sixties APL, SNOBOL, Lisp.
By the 80s rarely used.
Recent years Significant comeback ( some Web scripting
languages: JavaScritp, php)
Source
program
Result
do
{ item = 10; value = value + item; }
while(value<100);
should not only save the CPU cycles, but can be used on any processor.
We may use the following algorithm to find the basic blocks in a program:
Search header statements of all the basic blocks from where a basic block
starts:
First statement of a program.
Statements that are target of any branch (conditional/unconditional).
Statements that follow any branch statement.
Header statements and the statements following them form a basic block.
A basic block does not include any header statement of any other basic block.
Basic blocks are important concepts from both code generation and
optimization point of view.
const m = 7, n = 85;
var i,x,y,z,q,r;
As in any language, in PL/0 we need
procedure mult;
var a, b; to identify what is the vocabulary and
begin what are the valid names and special
a := x; b := y; z := 0; symbols that we accept as valid:
while b > 0 do
begin
if odd x then z := z+a;
a := 2*a;
b := b/2;
end
end;
begin
x := m;
y := n;
call mult;
end.