semantic analysis cpsc 388 ellen walker hiram college

24
Semantic Analysis CPSC 388 Ellen Walker Hiram College

Upload: virginia-anderson

Post on 14-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Semantic Analysis

CPSC 388Ellen WalkerHiram College

Syntax vs. Semantics

• Syntax: completely described by CFG– Correct keywords, matched parentheses, etc.

• Semantics: everything else– Variables, functions declared before use

– Type checking– Function parameter checking

Attribute Grammar

• A way to represent semantics• Good when syntax drives semantics

• Rules that express relation of semantics to syntax– E.g.

•Number-> digit number.val = digit.val

– Look familiar??

Attributes

• Each non-terminal can have multiple attributes (X has X.a, X.b, X.c etc)

• Attributes can include – Data type of variable– Value of expression– Location of variable or function in memory

– Size of object (e.g. max length of array)

Vocabulary

• Attribute: – Property of an object / nonterminal

• Attribute grammar– Rules that compute attributes (parallel to grammar rules)

• Binding– Associating an attribute/value to an object

Static vs. Dynamic Binding

• Static = compile-time– Variable type– Constant value– Array max size

• Dynamic = run-time– Variable value– Array contents

Syntax-Directed Semantics

• Attribute grammar rules for integersD -> 0 D.val=0D->1 D.val=1N0 -> N1 D N0.val = N1.val*10+D.val

• Subscript when element appears more than once in a rule

• Result: parse tree “decorated” with attribute values

Type Computation

• Attribute “dtype” = data typeDecl -> Type Vs Vs.dtype = Type.dtype

Vs1 -> id, Vs2 id.dtype = Vs.dtype

Vs -> id id.dtype = Vs.dtype

Multiple Attributes

D -> 0 N.val=0 N.type= int

D->1 N.val=1 N.type = intN0 -> N1 D N0.val = N1.val*10+D.val

N0.type = int

F = N . F.type = doubleF0 = F1 D F0.val = (10*F1.val+D)/10

Computation in rules

• Control constructs & computations allowed in rules, e.g.– B-int -> int Baseint.base=Base.base– Base -> o Base.base = 8– Base -> Base.base = 10– D -> 0 digit.val = 0– D -> 8 if D.base > 8 then 8 else error

Dependency Rules

• You cannot compute an equation unless its pieces have already been computed

• E.g. N0.val = N1.val*10+D.val

– N0.val depends on N1.val and

– N0.val depends on D.val

• Dependency tree: parent is left side, children are items from right side

• Item used in “if” is also a child

Drawing Dependency Graphs

• Often superimposed on parse tree

• Multiple colors for – Parse tree– Each attribute’s dependencies

• Example, p. 273

Dependency Graph Example

basecharbase

digit valbase

basenum val

digit valbase

num valbase

num valbase

num valbase

Dependency Graph Example

basecharbase

digit valbase

basenum val

digit valbase

num valbase

num valbase

num valbase

Dependency Graph Example

basecharbase

digit valbase

basenum val

digit valbase

num valbase

num valbase

num valbase

Dependency Graph -> Evaluation Order

• Standard algorithm: Topological Sort– Mark all nodes without parents (in any order), then remove them from the graph

– Repeat until no items are left

• Requires links from child to parent• Cycles not allowed in dependency graph (Directed Acyclic Graph - DAG)

Attribute Computation Algorithm

• Generate parse tree (using TD or BU parsing techniques)

• Generate dependencies (using parse tree and attribute rules)

• Perform topological sort on dependency graph

• Evaluate attribute rules

Rule-based method

• Fixed evaluation order of attribute rules (independent of parse tree)

• Hard-coded into compiler (applied to parse tree at compile-time)

• Requires strongly non-circular attribute grammar– There is no parse tree for which this grammar can have a cycle

Attributes in YACC

• “Rules” can compute attributes (stored on separate stack; $$ pushed each time)

• Intermediate steps allowed, e.g. – A : b {compute} c d {compute more}

• Inherited attributes need external structures (globals) to handle

Attribute Propagation

• Synthesized attributes– Child -> parent dependencies only

•A->BC A.val = f(B.val, C.val)

• Inherited attributes– Parent->child dependencies

•A-> BC B.val = A.val

– sibling->sibling dependencies•A->BC C.val = B.val

Order of Evaluation

• Synthesized attributes:– Compute all children before their parents•Post-order traversal of the parse tree

• Inherited attributes:– Compute parents before children– Make sure siblings are in the right order (parents can be in between)•Pre-order or mixed pre-order/in-order traversal

Example: Traversal Orders

• Preorder = P, C1, C2– A B D C E F

• Postorder = C1, C2, P– D B E F C A

• Inorder = C1, P, C2– D B A E C F

A

B

E

C

FD

Computing Attributes During Parsing

• Simplifies parser / semantic analyzer into one stage

• Requires no right-to-left inherited attribute– E.g. based-num cannot be done this way

3 ways to compute attributes

• 1. Compute syntax tree and dependency tree, sort and evaluate

• 2. Determine evaluation order in advance, apply to parse tree

• 3. Add computation directly to parse (as in Bison)