Cs419 lec12 semantic analyzer

60 %
40 %
Information about Cs419 lec12 semantic analyzer
Education

Published on February 20, 2014

Author: hussiensharaf

Source: slideshare.net

WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Computer Science Department dr.sharaf@from-masr.com

Dr. Hussien M. Sharaf PHASES OF COMPILER position := initial + rate * 60 1. Lexical analyzer id1 := id2 + id3 * 60 2. Syntax analyzer 4. Intermediate code generator temp1:= inttoreal(60) temp2 := id3 * temp1 temp3 := id2 + temp2 id1 := temp3 := 5. Code optimizer + id1 * id2 id3 60 3. Semantic analyzer := + id1 * id2 id3 inttoreal temp1 := id3 * 60.0 id1 := id2 + temp1 6. Code generator MOVF id3 , R2 MULF #60.0 , R2 MOVF id2 , R1 ADDF R2, R1 MOVF R1, id1 60 2

Dr. Hussien M. Sharaf 3. SEMANTIC ANALYZER The semantics of a program are its meaning as opposed to syntax or structure.  The semantics consist of:  Runtime semantics: behavior of program at run time.  Static semantics: checked by the compiler.  3

Dr. Hussien M. Sharaf STATIC SEMANTICS  Static semantics include:  Declaration of variables and constants before use. i.e. int x; x = 3;  Calling functions that exist (predefined in a library or defined by the user) i.e. n = Max(4,7); int Max(int x, int y) { int z; if(x > y) z = x; else z = y; return z; } 4

Dr. Hussien M. Sharaf STATIC SEMANTICS  Passing parameters properly.  Type checking, i.e. int x, y; x = 3; y = 2.5; performs an error, cause 2.5 is not int it is float data type.  Static semantics can not be checked by the parser. 5

Dr. Hussien M. Sharaf SEMANTIC ANALYZER (CONT.)  The semantic analyzer does the following:  Checks the static semantics of the language.  Annotates the syntax tree with type information, as shown in example. := x + y * 2.5; a := real id a real + real id x real Annotated syntax tree * real inttoreal id y integer literal 2.5 real 6

Dr. Hussien M. Sharaf 4. INTERMEDIATE CODE GENERATOR Intermediate language called “Three-address code”.  Should have two important properties:  Should be easy to produce.  Should be easy to translate to the target language.  A TAC instruction have at most one instruction per line and can have at most three operands.  7

Dr. Hussien M. Sharaf INTERMEDIATE CODE GENERATOR  Example: a := x + y * 2.5; := real Three-address code id a real + real id x real * real inttoreal temp1 := inttoreal(y) temp2 := temp1 real* 2.5 temp3 := x real+ temp2 a := temp3 literal 2.5 real id y integer  A TAC instruction have at most one instruction per line and can have at most three operands. 8

Dr. Hussien M. Sharaf 5. CODE OPTIMIZER  Code optimization can be applied to: code – independent of the target machine.  Target code – dependent on the target machine.  Intermediate 9

Dr. Hussien M. Sharaf INTERMEDIATE CODE OPTIMIZATION  Intermediate code optimization include: A. B. C. D. E. Constant folding. Elimination of common sub- expressions. Identification and elimination of unreachable code (called dead code). Improving loops. Improving function calls. 10

Dr. Hussien M. Sharaf A. CONSTANT FOLDING Simplifying constant expressions at compile time.  Example: i = 320 * 200 * 32 In this example modern compilers identify constructs, and substitute the computed values at compile time (in this case – 2,048,000)  11

Dr. Hussien M. Sharaf B. ELIMINATION OF COMMON SUB- EXPRESSIONS Replace the common expressions with a single variable holding the computed value.  Example:  a = b * c + g; d = b * c * e; tmp = b * c; a = tmp + g; d = tmp * e; 12

Dr. Hussien M. Sharaf C. IDENTIFICATION AND ELIMINATION OF DEAD CODE Dead code is the code in a program which is executed but whose result is never used in any other computation.  Dead code wastes computation time.  Example:  int f (int x, int y) { int z = x + y; return x * y; } should be eliminated. 13

Dr. Hussien M. Sharaf D. IMPROVING LOOPS    There are a lot of strategies for improving loops. A simple one is: Move loop invariants out of the loop Example: For y = 0 to Height-1 For x = 0 to Width-1 ' y*Width is invariant i = y*Width + x Process i Next x Next y  For y = 0 to Height-1 temp = y*Width For x = 0 to Width-1 i = temp + x Process i Next x Next y This link : http://www.aivosto.com/vbtips/loopopt.html illustrates all the strategies for interested readers. 14

Dr. Hussien M. Sharaf E. IMPROVING FUNCTION CALLS    One strategy is by Removing recursion (function that calls itself). Example of recursive function: unsigned int factorial(unsigned int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } } After removing recursion and using loops instead it will be: unsigned int factorial(unsigned int n) { int result = 1; if (n == 0) { return 1; } else { for (i = n; i>=1; i--) { result = result * i; } return result; } } 15

Dr. Hussien M. Sharaf TARGET CODE OPTIMIZATION  Target Code optimization is done by improving the intermediate code, and removing the redundant code.  Example: Temp1 := int-to-real(60) Temp2 := id3 * temp1 Temp3 := id2 + temp2 Id1 := temp3 temp1 := id3 * 60.0 id1 := id2 + temp1 16

Dr. Hussien M. Sharaf TARGET CODE OPTIMIZATION  Target code optimization include: a. b. Allocation and use of registers. Selection of better (safer) instructions and addressing modes. 17

Dr. Hussien M. Sharaf 6. CODE GENERATOR Generates code for the target machine.  Selects appropriate machine instructions.  Allocates memory locations for variables.  Allocates registers for intermediate computations.  Three-address code temp1 := int2real(y) temp2 := temp1 * 2.5 temp3 := x + temp2 a : = temp3 Assembly code LOADI MOVF MULF LOADF ADDF STORF R1 , y F1 , R1 F2 , F1, 2.5 F3 , x F4 , F3, F2 a, F4 ;; R1 ;; F1 ;; F2 ;; F3 ;; F4 ;; a y int2real(R1) F1 * 2.5 x F3 + F2 F4 18

Dr. Hussien M. Sharaf CODE GENERATOR  Generation target code example: temp := id3 * 60.0 id1 := id2 + temp MOVF MULF MOVF ADDF MOVF id3 , R2 #60.0, R2 id2 , R1 R2 , R1 R1, id1 ;; id3 R2 ;; R2 * 60.0 ;; id2 R1 ;; R2 + R1 ;; R1 id1 R2 R1 19

Dr. Hussien M. Sharaf THANK YOU

Add a comment

Related presentations

Related pages

Principles of Compiler Design Prof. Y.N. Srikant ...

So, we have completed lexical analyzer, syntax analyzer, ... syntax analyzer, the syntax tree is input into the semantic analyzer module, which outputs ...
Read more

The Semantic Web - Uta Priss

I Analyse domain knowledge (Note: the following 6 slides are based on “Ontology Development ... The Semantic Web Author: SET09103 Advanced Web Technologies
Read more

Lexical Analysis | LinkedIn

View 968 Lexical Analysis posts, presentations, experts, and more. Get the professional knowledge you need on LinkedIn.
Read more

Google

Advertising Programmes Business Solutions +Google About Google Google.com © 2016 - Privacy - Terms. Search; Images; Maps; Play; YouTube; News; Gmail ...
Read more

Ron Zacharski | Class Schedule

cs419 Data Mining. Syllabus; Class Schedule; Teams; Fall 2013. cs110H Honors Intro to CS. Class Schedule. Binary Thermometer; Syllabus; Teams; cs230 Data ...
Read more

Presentation "Please have a seat. Our program will ...

Presentation on theme: "Please have a seat. Our program will commence shortly." — Presentation transcript:
Read more

Cyber warfare: Issues and challenges - ScienceDirect.com ...

Cyber warfare: Issues and challenges. Michael Robinson a ... semantic attack, ... It is out of the scope of this paper to analyse all 95 rules from the ...
Read more

Qualitative Research in Nursing - Scribd - Read Unlimited ...

Qualitative Research in Nursing “Not everything that counts can be counted, and not everything that can be counted counts” -Albert Einstein
Read more