1 / 24

Semantic Analysis

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.

ballm
Download Presentation

Semantic Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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-> digit number.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 -> 0 D.val=0 D->1 D.val=1 N0 -> 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

  8. Type Computation • Attribute “dtype” = data type Decl -> Type Vs Vs.dtype = Type.dtype Vs1 -> id, Vs2 id.dtype = Vs.dtype Vs -> id id.dtype = Vs.dtype

  9. Multiple Attributes D -> 0 N.val=0 N.type= int D->1 N.val=1 N.type = int N0 -> N1 D N0.val = N1.val*10+D.val N0.type = int F = N . F.type = double F0 = F1 D F0.val = (10*F1.val+D)/10

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

  11. 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

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

  13. basenum val num base val basechar base Dependency Graph Example digit num base val base val digit num base val base val

  14. Dependency Graph Example basenum val num base val basechar base digit num base val base val digit num base val base val

  15. basenum val num base val basechar base digit num base val base val digit num base val base val Dependency Graph Example

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

  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)

More Related