Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler design.

Similar presentations


Presentation on theme: "Compiler design."— Presentation transcript:

1 Compiler design

2 Phases of compiler

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

4 Lexical analysis

5 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.

6 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

7 Lexical analysis

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

9 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.

10 Lexical error

11 Syntax Analyzer Taken token stream as input and generate syntax tree corresponding to it. Token stream= id1=id2+id3*60. Syntax Analyzer uses grammar to generate Infix expression.

12

13 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

14

15 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

16 Syntax Error

17 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 int into float or float into int. Semantic analyzer will perform type casting or type checking.

18 Because of higher to lower is done then information could be lost.
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.

19

20 Since id1, id2, id3 are float type, so we need to convert 60 into float as well.

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

22 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.)

23 Code optimization

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


Download ppt "Compiler design."

Similar presentations


Ads by Google