Top Banner
Semantic Analysis CPSC 388 Ellen Walker Hiram College
24

Semantic Analysis CPSC 388 Ellen Walker Hiram College.

Dec 14, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

Semantic Analysis

CPSC 388Ellen WalkerHiram College

Page 2: Semantic Analysis CPSC 388 Ellen Walker Hiram 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

Page 3: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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??

Page 4: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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)

Page 5: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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

Page 6: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

Static vs. Dynamic Binding

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

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

Page 7: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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

Page 8: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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

Page 9: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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

Page 10: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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

Page 11: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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

Page 12: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

Drawing Dependency Graphs

• Often superimposed on parse tree

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

• Example, p. 273

Page 13: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

Dependency Graph Example

basecharbase

digit valbase

basenum val

digit valbase

num valbase

num valbase

num valbase

Page 14: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

Dependency Graph Example

basecharbase

digit valbase

basenum val

digit valbase

num valbase

num valbase

num valbase

Page 15: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

Dependency Graph Example

basecharbase

digit valbase

basenum val

digit valbase

num valbase

num valbase

num valbase

Page 16: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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)

Page 17: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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

Page 18: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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

Page 19: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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

Page 20: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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

Page 21: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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

Page 22: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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

Page 23: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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

Page 24: Semantic Analysis CPSC 388 Ellen Walker Hiram College.

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)