Semantic Analysis Chapter 6 Two Flavors u Static

  • Slides: 29
Download presentation
Semantic Analysis Chapter 6

Semantic Analysis Chapter 6

Two Flavors u Static (done during compile time) –C – Ada u Dynamic (done

Two Flavors u Static (done during compile time) –C – Ada u Dynamic (done during run time) – LISP – Smalltalk u Optimization

Static Semantic Analysis u Build symbol table u Keep track of declarations u Perform

Static Semantic Analysis u Build symbol table u Keep track of declarations u Perform type checking

Static Analysis u Description – Attributes (properties) u Implementation – Attribute equations (semantic rules)

Static Analysis u Description – Attributes (properties) u Implementation – Attribute equations (semantic rules) – Application of rules Syntax-directed semantics

General Attribute u Property of the Language – Data type – Value of expressions

General Attribute u Property of the Language – Data type – Value of expressions – Location of variables in memory – Object code of procedure – Number of Significant digits

Specific Attributes Parameters/Arguments type u Parameters/Arguments number u Array subscript type u Array subscript

Specific Attributes Parameters/Arguments type u Parameters/Arguments number u Array subscript type u Array subscript number u Continue with no place to continue to u Variable undeclared u Variable duplicately declared u Scope u Incorrect structure reference u

Specific Attributes Cont. Break inappropriate u Incorrect Return u – Wrong type – Array

Specific Attributes Cont. Break inappropriate u Incorrect Return u – Wrong type – Array – None when needed (void) No main u Two main’s u Constant on left side u Expression types u

Binding Time of Attributes u Static - prior to execution – Fortran u Dynamic

Binding Time of Attributes u Static - prior to execution – Fortran u Dynamic - during execution u Combination –C – Java – Pascal

Attribute Grammars u. X is grammar symbol, Xa is an attribute for this symbol

Attribute Grammars u. X is grammar symbol, Xa is an attribute for this symbol X ABCD (grammar) X. x = A. a B. b C. c D. d (attribute grammar)

Attribute Grammar Example u E 1 E 2 + T E 1. type =

Attribute Grammar Example u E 1 E 2 + T E 1. type = E 2. type + T. type

Attribute Grammar Example type var-list. dtype =type. dtype u type int type. dtype =

Attribute Grammar Example type var-list. dtype =type. dtype u type int type. dtype = integer u type float type. dtype = float u var-list 1 id, var-list 2 id. dtype = var-list 1. dtype var-list 2. dtype = var-list 1. dtype u var-list id id. dtype = var-list. dtype u decl

Attribute Grammar Comments u Symbols may have more than one attribute u The grammar

Attribute Grammar Comments u Symbols may have more than one attribute u The grammar is not the master u More of a guide

Attribute Grammar Example u E 1 E 2 + T E 1. tree =

Attribute Grammar Example u E 1 E 2 + T E 1. tree = mk. Op. Node(+, E 2. tree, T. tree) u. E T E. tree = T. tree u. F number F. tree = mk. Num. Node(number. lexval)

Attribute Up and Down Dependency Tree u Synthesized – Point from child to parent

Attribute Up and Down Dependency Tree u Synthesized – Point from child to parent u Inherited – Point child to child or parent to child

Symbol Tables u Lists of Lists u Hash – Collision resolving by use of

Symbol Tables u Lists of Lists u Hash – Collision resolving by use of buckets – Collision resolving by probing u…

Symbol Tables u Keep track of identifiers u Must deal with scope efficiently

Symbol Tables u Keep track of identifiers u Must deal with scope efficiently

Code Fragment int f(int size) { char i, temp; … { double j, i;

Code Fragment int f(int size) { char i, temp; … { double j, i; } { char * j; *j = i = 5; } }

Static vs Dynamic Scope compile time or run time int i = 1; void

Static vs Dynamic Scope compile time or run time int i = 1; void f(void) { printf(“%dn”, i); } void main(void) { int i = 2; f(); return; } What is printed?

Kinds of Declarations u Sequential – each declaration is available starting with the next

Kinds of Declarations u Sequential – each declaration is available starting with the next line – C u Collateral – each declaration is evaluated in the environment preceding the declaration group. Declared identifiers are available only after all finishes. – scheme – ML u Recursive - requires the function name to be added to the symbol table before processing the body of the function. C functions and type declarations are recursive.

Example - Sequential/Colateral order is not important with in group int i = 1;

Example - Sequential/Colateral order is not important with in group int i = 1; void f(void) { int i = 2, j = i + 1; … } Is j 2 or 3?

Example - Recursive int gcd(int n, int m) { if (m == 0) return

Example - Recursive int gcd(int n, int m) { if (m == 0) return n; else return gcd(m, n%m); } gcd must be added to the symbol table before processing the body

Example - Recursive void f(void) { … g() … } void g(void) { …

Example - Recursive void f(void) { … g() … } void g(void) { … f() … } Resolved by using prototype. Some languages have issue with using g before g is defined. (pascal)

Data Types – Type Checking u Explicit datatype – int x u Implicit datatype

Data Types – Type Checking u Explicit datatype – int x u Implicit datatype – #define x 5

Implementation of Types u Hardware implementation – int – double – float u Software

Implementation of Types u Hardware implementation – int – double – float u Software implementation – boolean – char – enum – can be integers to save space

More Complicated Types u Arrays – base(b)+i*esize – base(ar)+(i 1*r 2 +i 2)*esize u

More Complicated Types u Arrays – base(b)+i*esize – base(ar)+(i 1*r 2 +i 2)*esize u Records – allocate memory sequentially – base+displacement

Type Checking Statements u. S id = E S. type = if id. type

Type Checking Statements u. S id = E S. type = if id. type = E. type then void else error u S if E then S 1 S. type=if E. type=boolean then S 1. type

Equivalence of type Expressions u Structural Equivalence – two expressions are either the same

Equivalence of type Expressions u Structural Equivalence – two expressions are either the same basic type, or are formed by applying the same constructor to structurally equivalent types. I. E. equivalent only if they are identical. – Example typedef link = *cell link next; cell * p; u Name Equivalence – two expressions use the same name

Name Equivalence typedef int t 1; typedef int t 2; t 2 and t

Name Equivalence typedef int t 1; typedef int t 2; t 2 and t 1 are not the same type. int type. Equal(t 1, t 2) { if (t 1 and t 2 are simple types) return t 1 == t 2; if (t 1 and t 2 are type names) return t 1 == t 2; else return 0; } in case you read the text

Name Equivalence typedef int t 1; typedef int t 2; t 2 x; t

Name Equivalence typedef int t 1; typedef int t 2; t 2 x; t 2 y; t 1 z; x and y are the same type. z is not the same type.