Академический Документы
Профессиональный Документы
Культура Документы
16E41A0531
Phases of compiler
Lexical Analysis
The first phase of scanner works as a text
scanner. This phase scans the source
code as a stream of characters and
converts it into meaningful lexemes.
3
4
Lexical Analyzer represents these lexemes in
the form of tokens as:
<token-name, attribute-value>
Syntax Analysis
The next phase is called the syntax analysis or
parsing. It takes the token produced by lexical
analysis as input and generates a parse tree (or
syntax tree). In this phase, token arrangements
are checked against the source code grammar,
i.e. the parser checks if the expression made by
the tokens is syntactically correct.
5
Semantic Analysis
Semantic analysis checks whether the parse tree
constructed follows the rules of language. For
example, assignment of values is between
compatible data types, and adding string to an
integer. Also, the semantic analyzer keeps track of
identifiers, their types and expressions; whether
identifiers are declared before use or not etc. The
semantic analyzer produces an annotated syntax
tree as an output.
6
Intermediate Code Generation
After semantic analysis the compiler generates
an intermediate code of the source code for the
target machine. It represents a program for
some abstract machine. It is in between the
high-level language and the machine language.
This intermediate code should be generated in
such a way that it makes it easier to be
translated into the target machine code.
7
Code Generation
In this phase, the code generator takes the
optimized representation of the intermediate
code and maps it to the target machine
language. The code generator translates the
intermediate code into a sequence of
(generally) re-locatable machine code.
Sequence of instructions of machine code
performs the task as the intermediate code
would do.
8
Symbol Table
It is a data-structure maintained
throughout all the phases of a compiler.
All the identifier's names along with their
types are stored here. The symbol table
makes it easier for the compiler to quickly
search the identifier record and retrieve it.
The symbol table is also used for scope
management.
9
Total=base price+(no . of units*2)
Lexical Analyzer:-
id1=id2+(id3*2)
Syntax Analyzer:-
10
11
Temp1=int to real(2)
Temp2=id3*Temp1
Temp3=id2+Temp2
Id1=temp3
Code Optimizer:-
Temp1=id3*2.0
id1=id2+Temp1
12
movF R2,id3
mulF R2,#2.0
movF R1,id2
ADDF R1,R2
movF id1,R1
13
There are different ways in which parameter data can
be passed into and out of methods and functions. Let
us assume that a function B() is called from another
function A(). In this case A is called the “caller
function” and B is called the “called function or callee
function”. Also, the arguments which A sends to B are
called actual arguments and the parameters of B are
called formal arguments.
14
Important methods of Parameter
Passing
Pass By Value : This method uses in-mode semantics.
Changes made to formal parameter do not get
transmitted back to the caller. Any modifications to
the formal parameter variable inside the called
function or method affect only the separate storage
location and will not be reflected in the actual
parameter in the calling environment. This method is
also called as call by value.
15
// C program to illustrate
// call by value
#include <stdio.h>
// Passing parameters
func(x, y);
printf("In main, x = %d y = %d\n", x, y);
return 0;
}
16
Pass by reference(aliasing) : This
technique uses in/out-mode semantics. Changes
made to formal parameter do get transmitted
back to the caller through parameter passing. Any
changes to the formal parameter are reflected in
the actual parameter in the calling environment
as formal parameter receives a reference (or
pointer) to the actual data. This method is also
called as <em>call by reference. This method is
efficient in both time and space.
17
// C program to illustrate
// call by reference
#include <stdio.h>
int main(void)
{
int a = 10, b = 20;
// passing parameters
swapnum(&a, &b);
19
Preprocessor
A preprocessor is a program that processes its
input data to produce output that is used as
input to another program. The output is said
to be a preprocessed form of the input data,
which is often used by some subsequent
programs like compilers.
1.Macro processing
2. File Inclusion
3.Rational Preprocessors
4.Language extension
20
Assembler
Assembler creates object code by translating assembly
instruction mnemonics into machine code. There are two
types of assemblers: