Академический Документы
Профессиональный Документы
Культура Документы
Source program
Target program
programs
in
another
high-level
language
is
termed
as
preprocessor.
THE CONTEXT OF A COMPILER
The following figure illustrates the context of a compiler in a real time
language processing system that takes the high level language source and
generates machine code.
1
2
3
Sym
boltabl
e
Man
ager
4
5
6
Source
Progra
Lexic
m al
Synt
Anal
ax
Semanti
yzer
Anal
c
Inter
yzer
Analyze
medi
rCode
ate
Opti
Code
Code
mize
Gene
Gene
Target
rrator
rator
Progra
m
Erro
r
Han
dler
LEXICAL ANALYSIS
The module corresponding to this phase is lexical analyzer or scanner.
The stream of characters making up the source program is read from left-toright and grouped into tokens. A set of strings that confirm to a pattern are
associated with a token. The string is often referred to as lexeme.
Identifiers, keywords, constants, operators, delimiters are typical
tokens. A token is often represented by a pair < token type, attribute of the
token>. Some tokens may not have attributes associated with them.
Consider the assignment
position = initial + rate * 60
The token stream corresponding to this is
5
positi
on
1
0
initial
1
5
rate
2
5
60
<ID,5>
<ASSIGN>
<ID, 10>
<PLUS>
<ID, 15>
<MUL>
<NUM, 25>
Symbol Table
The token types ID, ASSIGN, PLUS, MUL, NUM are indeed the symbolic
constants in the implementation of lexical analyzer. The token ID represents
an identifier and its attribute passed on represents the pointer (index) into
the symbol table that stores a record of information pertaining to the
identifier.
Parse tree
syntax
S ID = E
represent
EE+E
the
E*E
ID
NUM
Token refer to
terminals of CFG.
SEMANTIC ANALYSIS
Semantic tree
The semantic analysis phase checks the source program for semantic
errors and gathers type information for the subsequent code generation
phase. An important aspect of semantic analysis
CODE OPTIMIZATION
OP
ARG1
ARG2
INTORE
AL
MUL
PLUS
25
15
10
10
-35
36
--
RESU
LT
35
36
37
37
if A > B GOTO L2
GOTO L3
L2:
Can be optimized to
if A < = B GOTO L3
Another example of local optimization is common sub expression elimination.
The high level language expressions
A=B+C+D
E=B+C+F
Can be transformed to
T1 = B + C
A=T+D
E = T1 + F
An example of loop optimization involves the identifications of loop
invariants and displacing them, external to the loop.
CODE GENERATION
The final phase is the code generation phase. This phase cogenerates
either assembly code or relocatable machine code. Memory locations are
selected for each of the variable used by the program. Then the intermediate
instructions are each translated to a sequence of assembly or machine
instructions. For the above intermediate code, the target code is
MOVF ID15, R2
MULF #60.0, R2
MOVF ID10, R1
ADDF R2, R1
MOVF R1, ID5
SYMBOL TABLE
It is a data structure used by a compiler to keep track of scope/ binding
information about names. These names are used in the source program to
identify the various program elements, like variable, constants, procedures,
and the labels of statements. The symbol table is searched every time a
code generator
MOVF
MULF
MOVF
ADDF
MOVF
id3, R2
#60.0, R2
id2, R1
R2,
R1,
R1
id1