Presentation is loading. Please wait.

Presentation is loading. Please wait.

Semantic Analysis CPSC 388 Ellen Walker Hiram College.

Similar presentations


Presentation on theme: "Semantic Analysis CPSC 388 Ellen Walker Hiram College."— Presentation transcript:

1 Semantic Analysis CPSC 388 Ellen Walker Hiram College

2 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

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

4 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)

5 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

6 Static vs. Dynamic Binding Static = compile-time –Variable type –Constant value –Array max size Dynamic = run-time –Variable value –Array contents

7 Syntax-Directed Semantics Attribute grammar rules for integers D -> 0D.val=0 D->1D.val=1 N 0 -> N 1 DN 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

8 Type Computation Attribute “dtype” = data type Decl -> Type VsVs.dtype = Type.dtype Vs 1 -> id, Vs 2 id.dtype = Vs.dtype Vs -> idid.dtype = Vs.dtype

9 Multiple Attributes D -> 0N.val=0 N.type= int D->1N.val=1 N.type = int N 0 -> N 1 DN 0.val = N 1.val*10+D.val N 0.type = int F = N. F.type = double F 0 = F 1 DF 0.val = (10*F 1.val+D)/10

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

11 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

12 Drawing Dependency Graphs Often superimposed on parse tree Multiple colors for –Parse tree –Each attribute’s dependencies Example, p. 273

13 Dependency Graph Example basecharbase digit valbase basenum val digit valbase numvalbase numvalbase numvalbase

14 Dependency Graph Example basecharbase digit valbase basenum val digit valbase numvalbase numvalbase numvalbase

15 Dependency Graph Example basecharbase digit valbase basenum val digit valbasenumvalbasenumvalbasenumvalbase

16 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)

17 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

18 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

19 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

20 Attribute Propagation Synthesized attributes –Child -> parent dependencies only A->BCA.val = f(B.val, C.val) Inherited attributes –Parent->child dependencies A-> BCB.val = A.val –sibling->sibling dependencies A->BCC.val = B.val

21 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

22 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

23 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

24 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)


Download ppt "Semantic Analysis CPSC 388 Ellen Walker Hiram College."

Similar presentations


Ads by Google