290 likes | 335 Vues
This chapter explores two flavors of semantic analysis - static and dynamic - implemented in languages like C, Ada, LISP, and Smalltalk. It delves into static analysis, attributes, syntax-directed semantics, attribute grammars, type checking, and more. Learn about symbol tables, binding time of attributes, declaration kinds, and type equivalence in programming languages. Detailed examples and discussions help grasp the concepts effectively.
E N D
Semantic Analysis Chapter 6
Two Flavors • Static (done during compile time) • C • Ada • Dynamic (done during run time) • LISP • Smalltalk • Optimization
Static Semantic Analysis • Build symbol table • Keep track of declarations • Perform type checking
Static Analysis • Description • Attributes (properties) • Implementation • Attribute equations (semantic rules) • Application of rules Syntax-directed semantics
General Attribute • 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 • 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
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
Binding Time of Attributes • Static - prior to execution • Fortran • Dynamic - during execution • Combination • C • Java • Pascal
Attribute Grammars • X is grammar symbol, Xa is an attribute for this symbol XABCD (grammar) X.x= A.a B.b C.c D.d (attribute grammar)
Attribute Grammar Example • E1 E2 + T E1.type= E2.type+ T.type
Attribute Grammar Example • decl type var-list 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 var-list2.dtype = var-list1.dtype • var-list id id.dtype = var-list.dtype
Attribute Grammar Comments • Symbols may have more than one attribute • The grammar is not the master • More of a guide
Attribute Grammar Example • E1 E2 + T E1.tree= mkOpNode(+, E2.tree, T.tree) • E T E.tree = T.tree • F number F.tree = mkNumNode(number.lexval)
Attribute Up and DownDependency Tree • Synthesized • Point from child to parent • Inherited • Point child to child or parent to child
Symbol Tables • Lists of Lists • Hash • Collision resolving by use of buckets • Collision resolving by probing • …
Symbol Tables • Keep track of identifiers • Must deal with scope efficiently
Code Fragment int f(int size) { char i, temp; … { double j, i; } { char * j; *j = i = 5; } }
Static vs Dynamic Scopecompile time or run time int i = 1; void f(void) { printf(“%d\n”,i); } void main(void) { int i = 2; f(); return; } What is printed?
Kinds of Declarations • Sequential – each declaration is available starting with the next line • C • Collateral – each declaration is evaluated in the environment preceding the declaration group. Declared identifiers are available only after all finishes. • scheme • ML • 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/Colateralorder 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 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) { … f() … } Resolved by using prototype. Some languages have issue with using g before g is defined. (pascal)
Data Types – Type Checking • Explicit datatype • int x • Implicit datatype • #define x 5
Implementation of Types • Hardware implementation • int • double • float • Software implementation • boolean • char • enum – can be integers to save space
More Complicated Types • Arrays • base(b)+i*esize • base(ar)+(i1*r2 +i2)*esize • Records • allocate memory sequentially • base+displacement
Type Checking Statements • S id = E S.type = if id.type = E.type then void else error • S if E then S1 S.type=if E.type=boolean then S1.type
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. I.E. equivalent only if they are identical. • Example typedef link = *cell link next; cell * p; • Name Equivalence • two expressions use the same name
Name Equivalence typedef int t1; typedef int t2; t2 and t1 are not the same type. int typeEqual(t1, t2) { if (t1 and t2 are simple types) return t1 == t2; if (t1 and t2 are type names) return t1 == t2; else return 0;} in case you read the text
Name Equivalence typedef int t1; typedef int t2; t2 x; t2 y; t1 z; x and y are the same type. z is not the same type.