Lexical Analysis:

advertisement
Lecture: 2
Prepared by: Nitin Bansal
Roll No: 03CS3015
Introduction to Compilers (cont..)
Lexical Analyzer:
 Compilers maintain a symbol table i.e. it keeps track of variables, labels etc.
depending on the design.
For example: an expression of the form :
Position =Initial +60*Rate gets converted to
id1 = id2 +60*id3
SYMBOL TABLE:
Position
id1 & attributes


Initial
id2 & attributes
Rate
id3 & attributes
So the lexical analyzer converts symbols to an array of easy to use symbolic
constants (TOKENS).
Also, it removes spaces and other unnecessary things like comments etc.
Syntax Analyzer:
 It checks the code syntax using CFG : i.e. the set of rules
For example: if we have grammar of the form
1. EE=E
2. EE+E
3. EE*E
4. Econst
Then corresponding parse tree derivation is:
EE=Eid=E+Eid=id+E*Eid=id+id*60

Parser thus consumes these tokens .If any token is left unconsumed, the parser
gives an error /warning.
Semantic Analyzer:
 In this checks are performed to ensure that the components of a program fit
together meaningfully.
For example: we have a sample code:
int a; int b;
char c[];
a=b+c; (Type check is done)
Now the SYNTHESIS starts:
1
Lecture:2
Intermediate Code Generation:
We can think of this intermediate representation as a program for an abstract
machine.
For the example used in lexical analysis the intermediate representation will be:
temp1=initoreal(60)
temp2= id3*temp1
temp3=id2+temp2
id1=temp3
Code Optimization:
This phase attempts to improve the intermediate code, so that faster running
machine code will result .Some optimization are trivial.
So the final code for example above will be
temp1=id3*60
// removed unnecessary variables
id1=id2+temp1
Target Code Generation: Machine (M/C) Specific
 Compilers may generate many types of target codes depending on M/C while
some compilers make target code only for a specific M/C.
 It is normally the generation of relocatable machine code or assembly code.
RELATED COMPILER FIELDS
Macro Processor:
 A Processor may allow a user to define macros that are shorthand for
longer constructs.
 It is quite similar to “Inline Functions” but there are few differences.
Like Macros is processed before compilation whereas Inline Function
is processed during compilation.
For example: we have
An Inline Function: Fmax(a, b)
{a>b? a: b;}
Macro defined as: # Mmax(a, b) (a>b? a: b)
And main function:
main ()
{a=1; b=3;
Fmax(++a, ++b)
// this calls the inline function with incremented
//values of a and b during compilation
Whereas instead of this if we have a call like
Mmax(++a, ++b)
// this leads to two increments in value of b
// as expression replaced is (++a>++b? ++a: ++b)
This just takes the passed arguments and replaces them in the expression.
Then compilation evaluates it.
PRO-C: C+SQL
 Acts as a translator
2
3
Download