1 / 33

Chapter 6

Chapter 6. Semantic Analysis Attributes. Gang S. Liu College of Computer Science & Technology Harbin Engineering University. Translation Process. Source Code. 2. Tokens. Semantic Analyzer. Scanner. Parser. Source Code Optimizer. Code Generator. Target Code Optimizer. Literal Table.

fox
Download Presentation

Chapter 6

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. Chapter 6 Semantic Analysis Attributes Gang S. Liu College of Computer Science & Technology Harbin Engineering University

  2. Translation Process Source Code 2 Tokens Semantic Analyzer Scanner Parser Source Code Optimizer Code Generator Target Code Optimizer Literal Table Symbol Table Error Handler Samuel2005@126.com

  3. Translation Process 3 Source Code Tokens Semantic Analyzer Syntax Tree Scanner Parser Source Code Optimizer Code Generator Target Code Optimizer Literal Table Symbol Table Error Handler Samuel2005@126.com

  4. Translation Process Source Code Tokens Semantic Analyzer Syntax Tree Scanner Parser Annotated Tree 4 Source Code Optimizer Code Generator Target Code Optimizer Literal Table Symbol Table Error Handler Samuel2005@126.com

  5. Attributes • Attribute is any property of a programming language construct. • Attributes can vary widely in the information they contain, their complexity, and the time during the translation/execution process when they can be determined. • Examples of attributes: • The data type of a variable • The value of an expression • The location of a variable in memory • The object code of a procedure • The number of significant digits in a number Samuel2005@126.com

  6. Binding • The process of computing an attribute and associating its computed value with the language construct is referred to as the binding of the attribute. • The time during the compilation/execution process when the binding occur is called its binding time. • Attributes that can be bound prior to execution are called static. • Attributes that can only be bound during execution are dynamic. Samuel2005@126.com

  7. Attribute Grammars • In syntax-directed semantics, attributes are associated directly with the grammar symbols of the language. • If X is a grammar symbol, and a is an attribute associated to X, then we write X.a for the value of a associated to X. • For each grammar rule X0→X1 X2…Xn, the values of attributes Xi.aj of each grammar symbol Xi are related. • Attribute equation or semantic rule: Xi.aj = fij(X0.a1,…,X0.ak, X1.a1,…X1.ak,…Xn.a1,…,Xn.ak) • Attribute grammar for the attributes a1, a2, …, ak is the collection of such equations for all grammar rules of the language. Samuel2005@126.com

  8. Example 6.1 number → number digit | digit digit → 0 | 1| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 • The most significant attribute of a number is its value. • Each digit has a value computable from the digit it represents. • digit→ 0 implies that digit has value 0 digit.val = 0 • If a number is derived using the rule number → digit, then • number.val = digit.val • If a number contains more digits number → number digit, then • number1.val = number2.val * 10 + digit.val number1 → number2 digit Samuel2005@126.com

  9. Example 6.2 exp → exp + term | exp – term | term term → term * factor | factor factor → (exp) | number Grammar Rule Semantic Rules exp1 → exp2+ term exp1.val = exp2.val + term.val exp1 → exp2–term exp1.val = exp2.val - term.val exp → term exp.val = term.val term1 → term2* factor term1.val = term2.val * factor.val term → factor term.val = factor.val factor → (exp) factor.val= exp.val factor → number factor.val = number.val Samuel2005@126.com

  10. Example 6.3 decl → type var-list type → int | float var-list → id , var-list | id Grammar Rule Semantic Rules decl → type var-list var-list.dtype = type.dtype type → int type.dtype = integer type → float type.dtype = float var-list1 → id , var-list2 id.type = var-list1.dtype var-list2.dtype = var-list1.dtype var-list → id id.type = var-list.dtype Samuel2005@126.com

  11. Dependency Graph • Given an attribute grammar, each grammar rule choice has an associated dependency graph. • The graph has a node labeled by each attribute of each symbol in the grammar rule. • For each attribute equation Xi.aj = fij(…Xm.ak…) there is an edge from each node Xm.ak to the node Xi.aj Samuel2005@126.com

  12. Dependency Graph (cont) • Given a legal string in the language, the dependency graph of the string is the union if the dependency graphs of the grammar rule choices representing each (nonleaf) node of the parse tree of the string. Samuel2005@126.com

  13. Example 6.6 number → number digit | digit digit → 0 | 1| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 number1.val = number2.val * 10 + digit.val number1.val number2.val digit.val number.val = digit.val number.val digit.val Samuel2005@126.com

  14. Example 6.6 (cont) number → number digit | digit digit → 0 | 1| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 String: 3 4 5 number.val number.val digit.val ( val = 5 ) number.val digit.val ( val = 4 ) digit.val ( val = 3 ) Samuel2005@126.com

  15. Example 6.7 var-list1→ id , var-list2 has two associated attribute equations id.dtype = var-list1.dtype var-list2.dtype = var-list1.dtype and the dependency graph var-list1.dtype id.dtype var-list2.dtype Samuel2005@126.com

  16. Example 6.7 (cont) • The grammar rule var-list → id has the attribute equation id.dtype = var-list.dtype and the dependency graph var-list.dtype id.dtype • The rule decl → type var-list with the equation var-list.dtype = type.dtype has the dependency graph type.dtype var-list.dtype Samuel2005@126.com

  17. Example 6.7 (cont) • decl is not directly involved in the dependency graph. We often draw the dependency graph superposed over a parse tree segment corresponding to the grammar rule. • This makes clearer the grammar rule to which the dependency is associated decl type.dtype var-list.dtype Samuel2005@126.com

  18. Example 6.7 (cont) • We suppress the dot notation and write the attributes next to associated node. var-list1.dtype id.dtype var-list2.dtype var-list1→ id , var-list2 var-list id,var-list dtype dtype dtype Samuel2005@126.com

  19. Example 6.7 (cont) String: float x, y decl type dtype dtype var-list , dtype var-list dtype id (x) float dtype id (y) Samuel2005@126.com

  20. Order Constrains • Given a particular string of tokens to be translated, the dependency graph of the parse tree of the string gives a set of order constrains under which the algorithm must operate to compute the attributes for the string. • Any algorithm must compute the attribute at each node in the dependency graph before it attempts to compute any successor attributes. • The traversal order of the graph that obeys this restriction is called topological sort. • A necessary and sufficient condition for a topological sort to exist is that the graph must be acyclic (DAG). Samuel2005@126.com

  21. Root decl • A root of the graph is node that has no predecessors. • Attribute values in such nodes do not depend on any other attribute. type dtype dtype var-list , dtype var-list dtype id (x) float dtype id (y) Samuel2005@126.com

  22. Synthesized Attributes • An attribute is synthesized if all its dependencies point from child to parent in the parse tree. • Equivalently, an attribute a is synthesized if, given a grammar rule A → X1 X2,…, Xn the only associated attribute equation with an a on the left-hand side is of the form A.a = f(X1.a1, …, X1.ak, …, Xn.a1,…, Xn.ak) • An attribute grammar in which all the attributes are synthesized is called an S-attributed grammar. Samuel2005@126.com

  23. Attribute Values of S-Attributed Grammar • Given a parse tree or a syntax tree constructed by a parser, the attribute values of S-attributed grammar can be computed by a single bottom-up, or postorder, traversal of the tree. procedure PostEval (T: treenode); for each child C of T do PostEval(C); compute all synthesized attributes of T Samuel2005@126.com

  24. Example number1.val number2.val digit.val number.val digit.val Samuel2005@126.com

  25. Inherited Attribute • An attribute that is not synthesized is called an inherited attribute. • Example: decl type dtype dtype var-list , dtype var-list dtype id (x) float dtype id (y) Samuel2005@126.com

  26. Inherited Attribute (cont) • Dependencies flow either from parent to children or from sibling to sibling. • Inherited attributes can be computed by a preorder traversal, or combined preorder/inorder traversal of the syntax tree. a A a A a B a C a B a C procedure PreEval (T: treenode); for each child C of T do compute all inherited attributes of C PreEval(C); Samuel2005@126.com

  27. Example 6.12 decl →type var-list type → int | float var-list → id , var-list | id decl 1 2 type dtype dtype var-list , dtype var-list 4 dtype id (x) float 3 dtype id (y) 5 Samuel2005@126.com

  28. Attribute Computation and Syntax • Properties of attributes depend on the structure of the grammar. • Modifications to the grammar that do not change the legal strings of the language can make the computation of attributes simpler or more complex. Theorem: Given an attribute grammar, all inherited attributes can be changed into synthesized attributes by suitable modification of the grammar, without changing the language of the grammar. [Knuth] Samuel2005@126.com

  29. Example 6.18 decl → type var-list type → int | float var-list → id , var-list | id dtypeattribute is inherited. Samuel2005@126.com

  30. Example 6.7 (cont) String: float x, y decl type dtype dtype var-list , dtype var-list dtype id (x) float dtype id (y) Samuel2005@126.com

  31. Example 6.18 (cont) decl →type var-list type → int | float var-list → id , var-list | id decl → var-list id var-list → var-list id , | type type → int | float Samuel2005@126.com

  32. Example 6.18 (cont) float x, y decl var-list dtype=real id dtype=real (y) , var-list dtype=real id dtype=real (x) • The two dependencies that are drawn as dashed lines appear to be inherited attributes. • These dependencies are always to the leaves (not recursive) and not viewed as inherited. type dtype=real float Samuel2005@126.com

  33. Homework • 6.1, 6.2, 6.4, 6.7, 6.11, 6.12 Samuel2005@126.com

More Related