Semantic Analysis Chapter 6. Two Flavors Static (done during compile time) –C –Ada Dynamic...

29
Semantic Analysis Semantic Analysis Chapter 6 Chapter 6

Transcript of Semantic Analysis Chapter 6. Two Flavors Static (done during compile time) –C –Ada Dynamic...

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

Semantic AnalysisSemantic Analysis

Chapter 6Chapter 6

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

Two FlavorsTwo Flavors

Static (done during compile time)Static (done during compile time)– CC– AdaAda

Dynamic (done during run time)Dynamic (done during run time)– LISPLISP– SmalltalkSmalltalk

OptimizationOptimization

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

Static Semantic AnalysisStatic Semantic Analysis

Build symbol tableBuild symbol table Keep track of declarationsKeep track of declarations Perform type checkingPerform type checking

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

Static AnalysisStatic Analysis

DescriptionDescription– Attributes (properties)Attributes (properties)

ImplementationImplementation– Attribute equations (semantic rules)Attribute equations (semantic rules)– Application of rulesApplication of rules

Syntax-directed semanticsSyntax-directed semantics

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

General AttributeGeneral Attribute

Property of the LanguageProperty of the Language– Data typeData type– Value of expressionsValue of expressions– Location of variables in memoryLocation of variables in memory– Object code of procedureObject code of procedure– Number of Significant digitsNumber of Significant digits

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

Specific AttributesSpecific Attributes

Parameters/Arguments typeParameters/Arguments type Parameters/Arguments numberParameters/Arguments number Array subscript typeArray subscript type Array subscript numberArray subscript number Continue with no place to continue toContinue with no place to continue to Variable undeclaredVariable undeclared Variable duplicately declaredVariable duplicately declared ScopeScope Incorrect structure referenceIncorrect structure reference

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

Specific Attributes Cont.Specific Attributes Cont.

Break inappropriateBreak inappropriate Incorrect ReturnIncorrect Return

– Wrong typeWrong type– ArrayArray– None when needed (void)None when needed (void)

No mainNo main Two main’sTwo main’s Constant on left sideConstant on left side Expression typesExpression types

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

Binding Time of AttributesBinding Time of Attributes

Static - prior to executionStatic - prior to execution– FortranFortran

Dynamic - during executionDynamic - during execution CombinationCombination

– CC– Java Java – PascalPascal

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

Attribute GrammarsAttribute Grammars

X is grammar symbol, XX is grammar symbol, Xaa is an is an attribute for this symbolattribute for this symbol XXABCD (grammar)ABCD (grammar)

X.xX.x = A.a B.b C.c D.d= A.a B.b C.c D.d

(attribute grammar)(attribute grammar)

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

Attribute Grammar ExampleAttribute Grammar Example

EE11 E E2 2 + T+ T

EE1.1.typetype = E= E22.type.type + T.type+ T.type

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

Attribute Grammar ExampleAttribute Grammar Example

decl decl type var-list type var-list

var-list.dtype =type.dtypevar-list.dtype =type.dtype type type int type.dtype = integer int type.dtype = integer type type float type.dtype = float float type.dtype = float var-list1 var-list1 id, var-list2 id, var-list2

id.dtype = var-list1.dtypeid.dtype = var-list1.dtype

var-list2.dtype = var-list1.dtypevar-list2.dtype = var-list1.dtype var-list var-list id id.dtype = var-list.dtype id id.dtype = var-list.dtype

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

Attribute Grammar CommentsAttribute Grammar Comments

Symbols may have more than one Symbols may have more than one attributeattribute

The grammar is not the masterThe grammar is not the master More of a guideMore of a guide

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

Attribute Grammar ExampleAttribute Grammar Example

EE11 E E2 2 + T+ T

EE1.1.treetree = =

mkOpNode(+, EmkOpNode(+, E22.tree, T.tree).tree, T.tree) E E T T

E.tree = T.treeE.tree = T.tree F F number number

F.tree = mkNumNode(number.lexval)F.tree = mkNumNode(number.lexval)

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

Attribute Up and DownAttribute Up and DownDependency TreeDependency Tree

Synthesized Synthesized – Point from child to parentPoint from child to parent

InheritedInherited– Point child to child or parent to childPoint child to child or parent to child

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

Symbol TablesSymbol Tables

Lists of ListsLists of Lists HashHash

– Collision resolving by use of bucketsCollision resolving by use of buckets– Collision resolving by probingCollision resolving by probing

……

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

Symbol TablesSymbol Tables

Keep track of identifiersKeep track of identifiers Must deal with scope efficientlyMust deal with scope efficiently

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

Code FragmentCode Fragment

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

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

Static vs Dynamic ScopeStatic vs Dynamic Scopecompile time or run timecompile time or run time

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

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

Kinds of DeclarationsKinds of Declarations Sequential – each declaration is available starting Sequential – each declaration is available starting

with the next linewith the next line– CC

Collateral – each declaration is evaluated in the Collateral – each declaration is evaluated in the environment preceding the declaration group. environment preceding the declaration group. Declared identifiers are available only after all Declared identifiers are available only after all finishes.finishes.– schemescheme– MLML

Recursive - requires the function name to be Recursive - requires the function name to be added to the symbol table before processing the added to the symbol table before processing the body of the function. C functions and type body of the function. C functions and type declarations are recursive.declarations are recursive.

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

Example - Sequential/ColateralExample - Sequential/Colateralorder is not important with in grouporder is not important with in group

int i = 1;int i = 1;void f(void)void f(void){ { int i = 2, j = i + 1;int i = 2, j = i + 1; … …}}

Is j 2 or 3?Is j 2 or 3?

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

Example - RecursiveExample - Recursive

int gcd(int n, int m)int gcd(int n, int m)

{ if (m == 0) return n;{ 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 gcd must be added to the symbol table beforebefore processing the body processing the body

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

Example - RecursiveExample - Recursive

void f(void)void f(void){ … g() … }{ … g() … }void g(void)void g(void){ … f() … }{ … f() … }

Resolved by using prototype.Resolved by using prototype.Some languages have issue with using Some languages have issue with using

g before g is defined. (pascal)g before g is defined. (pascal)

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

Data Types – Type CheckingData Types – Type Checking

Explicit datatypeExplicit datatype– int xint x

Implicit datatypeImplicit datatype– #define x 5#define x 5

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

Implementation of TypesImplementation of Types

Hardware implementationHardware implementation– intint– doubledouble– floatfloat

Software implementationSoftware implementation– booleanboolean– charchar– enum – can be integers to save spaceenum – can be integers to save space

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

More Complicated TypesMore Complicated Types

ArraysArrays– base(b)+i*esizebase(b)+i*esize– base(ar)+(i1*r2 +i2)*esizebase(ar)+(i1*r2 +i2)*esize

RecordsRecords– allocate memory sequentiallyallocate memory sequentially– base+displacementbase+displacement

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

Type Checking StatementsType Checking Statements

S S id = E id = E

S.type = if id.type = E.type then voidS.type = if id.type = E.type then void

else errorelse error S S if E then S if E then S11

S.type=if E.type=boolean then SS.type=if E.type=boolean then S11.type.type

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

Equivalence of type Equivalence of type ExpressionsExpressions

Structural EquivalenceStructural Equivalence– two expressions are either the same basic type, or two expressions are either the same basic type, or

are formed by applying the same constructor to are formed by applying the same constructor to structurally equivalent types. I.E. equivalent only structurally equivalent types. I.E. equivalent only if they are identical. if they are identical.

– ExampleExampletypedef link = *celltypedef link = *cell

link next;link next; cell * p;cell * p; Name EquivalenceName Equivalence

– two expressions use the same nametwo expressions use the same name

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

Name EquivalenceName Equivalence

typedef int t1;typedef int t1;

typedef int t2;typedef int t2;

t2 and t1 are not the same type.t2 and t1 are not the same type.

int typeEqual(t1, t2)int typeEqual(t1, t2)

{ if (t1 and t2 are simple types){ 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 textelse return 0;} in case you read the text

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

Name EquivalenceName Equivalence

typedef int t1;typedef int t1;

typedef int t2;typedef int t2;

t2 x;t2 x;

t2 y;t2 y;

t1 z;t1 z;

x and y are the same type.x and y are the same type.

z is not the same type.z is not the same type.