1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction
2 November 19, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Keep in mind following questions Syntax-Directed Translation –What is added into a grammar? –What is an attribute? –How to use an attribute? Why we need attributes –How to parse by a grammar? –What inspires you? What is your reflection –Like it, why? –Hate it, why?
3 Uses a CF grammar to specify the syntactic structure of the language AND associates a set of attributes with the terminals and nonterminals of the grammar AND associates with each production a set of semantic rules to compute values of attributes A parse tree is traversed and semantic rules applied: after the computations are completed the attributes contain the translated form of the input November 19, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Syntax-Directed Translation
4 An attribute is said to be … –synthesized if its value at a parse-tree node is determined from the attribute values at the children of the node –inherited if its value at a parse-tree node is determined by the parent (by enforcing the parent’s semantic rules) November 19, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Synthesized and Inherited Attributes
5 expr expr 1 + term expr expr 1 - term expr term term 0 term 1 … term 9 expr.t := expr 1.t // term.t // “+” expr.t := expr 1.t // term.t // “-” expr.t := term.t term.t := “0” term.t := “1” … term.t := “9” ProductionSemantic Rule String concat operator November 19, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Example Attributes Grammar
6 expr.t = “95-2+” term.t = “2” expr.t = “95-” expr.t = “9”term.t = “5” term.t = “9” November 19, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Example Annotated Parse Tree
7 procedure visit(n : node); begin for each child m of n, from left to right do visit(m); evaluate semantic rules at node n end November 19, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Depth-First Traversals
8 expr.t = “95-2+” term.t = “2” expr.t = “95-” expr.t = “9”term.t = “5” term.t = “9” Note: all attributes are of the synthesized type November 19, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Depth-First Traversals (Example)
9 A translation scheme is a CF grammar embedded with semantic actions rest + term { print(“+”) } rest Embedded semantic action rest termrest+{ print(“+”) } November 19, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Translation Schemes Here “print” means generation of code
10 expr expr + term expr expr - term expr term term 0 term 1 … term 9 { print(“+”) } { print(“-”) } { print(“0”) } { print(“1”) } … { print(“9”) } November 19, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Example Translation Scheme Syntax-directed: Syntax (rules) that direct action to take
11 expr term expr term { print(“-”) } { print(“+”) } { print(“9”) } { print(“5”) } { print(“2”) } Translates into postfix November 19, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Example Translation Scheme
12 November 19, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Got it with following questions Syntax-directed Translation –W–What is added in to a grammar? –W–What is an attribute? –H–How to use an attribute? Why we need attributes –H–How to parse by a grammar? –W–What inspires you? What is your reflection –L–Like it, why? –H–Hate it, why?
13 Thank you very much! Questions? November 19, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Syntax-Directed Translation