No Slide Title

Download Report

Transcript No Slide Title

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

Attribute Grammar • A way to represent semantics • Good when syntax drives semantics • Rules that express relation of semantics to syntax – E.g. • Number-> digit – Look familiar??

number.val = digit.val

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 integers D -> 0 D->1 D.val=0 D.val=1 N 0 -> N 1 D N 0 .val = N 1 .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 type Decl -> Type Vs Vs 1 -> id, Vs 2 Vs -> id Vs.dtype = Type.dtype

id.dtype = Vs.dtype

id.dtype = Vs.dtype

Multiple Attributes D -> 0 D->1 N 0 -> N 1 D F = N . F 0 = F 1 D N.val=0 N.type= int N.val=1 N.type = int N 0 .val = N 1 .val*10+D.val

N 0 .type = int F.type = double F 0 .val = (10*F 1 .val+D)/10

Computation in rules • Control constructs & computations allowed in rules, e.g.

– B-int -> int Base int.base=Base.base

– Base -> o – Base -> e – D -> 0 Base.base = 8 Base.base = 10 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. N 0 .val = N 1 .val*10+D.val

– N 0 .val depends on N 1 .val and – N 0 .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 basenum

val

num

base val

basechar

base

num

base val

digit

base val

num

base val

digit

base val

Dependency Graph Example basenum

val

num

base val

basechar

base

num

base val

digit

base val

num

base val

digit

base val

Dependency Graph Example basenum

val

num

base val

basechar

base

num

base val

digit

base val

num

base val

digit

base val

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 D A B C E F

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)