Presentation is loading. Please wait.

Presentation is loading. Please wait.

Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.

Similar presentations


Presentation on theme: "Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization."— Presentation transcript:

1 Semantic Analysis Chapter 6

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

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

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

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

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

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

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

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

10 Attribute Grammar Example  E 1  E 2 + T E 1. type = E 2.type + T.type E 1. type = E 2.type + T.type

11 Attribute Grammar Example  decl  type var-list var-list.dtype =type.dtype var-list.dtype =type.dtype  type  int type.dtype = integer  type  float type.dtype = float  var-list1  id, var-list2 id.dtype = var-list1.dtype id.dtype = var-list1.dtype var-list2.dtype = var-list1.dtype var-list2.dtype = var-list1.dtype  var-list  id id.dtype = var-list.dtype

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

13 Attribute Grammar Example  E 1  E 2 + T E 1. tree = E 1. tree = mkOpNode(+, E 2.tree, T.tree) mkOpNode(+, E 2.tree, T.tree)  E  T E.tree = T.tree E.tree = T.tree  F  number F.tree = mkNumNode(number.lexval) F.tree = mkNumNode(number.lexval)

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

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

16 Symbol Tables  Keep track of identifiers  Must deal with scope efficiently

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

18 Static vs Dynamic Scope int i = 1; void f(void) { printf(“%d\n”,i); } void main(void) { int i = 2; f(); f(); return; return;} What is printed?

19 Kinds of Declarations  sequential –C  collateral –scheme –ML  recursive –requires the function name to be added to the symbol table before processing the body of the function

20 Example - Sequential/Colateral int i = 1; void f(void) { int i = 2, j = i + 1; int i = 2, j = i + 1; …} Is j 2 or 3?

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

22 Example - Recursive void f(void) { … g() … } void g(void) { … f() … } Resolved by using prototype. Actually, this didn’t create a problem.

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

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

25 More Complicated Types  Arrays –base(b)+i*esize –base(ar)+(i1*r2 +i2)*esize  Records –allocate memory sequentially –base+displacement

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

27 Equivalence of type Expressions  Structural Equivalence –two expressions are either the same basic type, or are formed by applying the same constructor to structurally equivalent types. IE equivalent only if they are identical.  Name Equivalence –The following is structurally equivalent, not name typedef link = *cell typedef link = *cell link next; link next; cell * p; cell * p;

28 Name Equivalence typedef int t1; typedef int t2; t2 and t1 are not the same type. t2 and t1 are not the same type. int typeEqual(t1, t2) { if (t1 and t2 are simple types) return t1 == t2; return t1 == t2; if (t1 and t2 are type names) if (t1 and t2 are type names) return t1 == t2; return t1 == t2; else return 0;} in case you read the text else return 0;} in case you read the text


Download ppt "Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization."

Similar presentations


Ads by Google