Compiler design Phases of compiler Lexical Analysis Lexemes

  • Slides: 24
Download presentation
Compiler design

Compiler design

Phases of compiler

Phases of compiler

Lexical Analysis • Lexemes- Meaningful character stream stored in source program. • Pattern- Already

Lexical Analysis • Lexemes- Meaningful character stream stored in source program. • Pattern- Already saved in compiler. • Tokens- Lexemes that matches with pattern result into taken.

Lexical analysis

Lexical analysis

Lexical Analysis • Symbol table include: – Serial no. – Variable name. – Variable

Lexical Analysis • Symbol table include: – Serial no. – Variable name. – Variable type. – Since float, operators are already known to the compiler, they are not stored in compiler. – Only new information is stored in compiler.

Lexical Anlaysis • Longest match rule is applied. Eg. a+b In this + operator

Lexical Anlaysis • Longest match rule is applied. Eg. a+b In this + operator is followed by operand, so + will be one token. a++ + followed by another + so, whole ++ would be taken as one token

Lexical analysis

Lexical analysis

 • Removes all the comment line. /* */ // • Compaction of consecutive

• Removes all the comment line. /* */ // • Compaction of consecutive white spaces into one and remove that space.

Int sum; smu = 15; Here smu will not match with any pattern, because

Int sum; smu = 15; Here smu will not match with any pattern, because in declaration sum is declared. So, corresponding to smu token would not be generated. It results into lexical error.

Lexical error

Lexical error

Syntax Analyzer • Taken token stream as input and generate syntax tree corresponding to

Syntax Analyzer • Taken token stream as input and generate syntax tree corresponding to it. • Token stream= id 1=id 2+id 3*60. • Syntax Analyzer uses grammar to generate Infix expression.

 • We would be given with grammer, and corresponding to it, parse tree

• We would be given with grammer, and corresponding to it, parse tree would be generated. • Parse tree would be compressed to generate syntax tree. • Internal nodes would be operator and leaf nodes would be operand

 • If due to any reason, syntax tree could not obtained, then syntax

• If due to any reason, syntax tree could not obtained, then syntax error would be generated. • For ex. If there is any operator in the left hand side (a+b=c), then it would not be satisfied by grammar and syntax error would be generated

Syntax Error

Syntax Error

Semantic Analyzer • Take syntax tree as input, and check semantic consistency. • For

Semantic Analyzer • Take syntax tree as input, and check semantic consistency. • For ex. Int + float: Addition of int and float. There either we need to convert into float or float into int. • Semantic analyzer will perform type casting or type checking.

 • Type casting: – Implicit (performed by compiler automatically) – Explicit (performed by

• Type casting: – Implicit (performed by compiler automatically) – Explicit (performed by user) • Implicit type casting is from lower to higher side. i. e. . Int would be converted float by compiler (since int is of 2 byte and float of 4 byte) • Because of higher to lower is done then information could be lost.

 • Since id 1, id 2, id 3 are float type, so we

• Since id 1, id 2, id 3 are float type, so we need to convert 60 into float as well.

Intermediate code generation • Three address code is generated using temporary variables.

Intermediate code generation • Three address code is generated using temporary variables.

 • Up to intermediate code, everything is machine independent. • Intermediate code generation

• Up to intermediate code, everything is machine independent. • Intermediate code generation generates machine independent code. • Three address code(in right side there must be utmost one operator and utmost three variables in whole. )

Code optimization

Code optimization

Stages in a Compiler • Two stages – Front end (or Analysis) – Back

Stages in a Compiler • Two stages – Front end (or Analysis) – Back end (or Synthesis). • Front End – Preprocessed file to Intermediate Code • Back End – Intermediate Code to Target Assembly Code