Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 November 19, 2015 1 November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.

Similar presentations


Presentation on theme: "1 November 19, 2015 1 November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University."— Presentation transcript:

1 1 November 19, 2015 1 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) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS400 Compiler Construction

2 2 November 19, 2015 2 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: 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 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, 2015 3 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Syntax-Directed Translation

4 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, 2015 4 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Synthesized and Inherited Attributes

5 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, 2015 5 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Example Attributes Grammar

6 6 expr.t = “95-2+” term.t = “2” 9-5+2 expr.t = “95-” expr.t = “9”term.t = “5” term.t = “9” November 19, 2015 6 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Example Annotated Parse Tree

7 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, 2015 7 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Depth-First Traversals

8 8 expr.t = “95-2+” term.t = “2” 9-5+2 expr.t = “95-” expr.t = “9”term.t = “5” term.t = “9” Note: all attributes are of the synthesized type November 19, 2015 8 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Depth-First Traversals (Example)

9 9 A translation scheme is a CF grammar embedded with semantic actions rest  + term { print(“+”) } rest Embedded semantic action rest termrest+{ print(“+”) } November 19, 2015 9 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Translation Schemes Here “print” means generation of code

10 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, 2015 10 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Example Translation Scheme Syntax-directed: Syntax (rules) that direct action to take

11 11 expr term 9 - 5 + 2 expr term { print(“-”) } { print(“+”) } { print(“9”) } { print(“5”) } { print(“2”) } Translates 9-5+2 into postfix 95-2+ November 19, 2015 11 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Example Translation Scheme

12 12 November 19, 2015 12 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: 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 13 Thank you very much! Questions? November 19, 2015 13 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Syntax-Directed Translation


Download ppt "1 November 19, 2015 1 November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University."

Similar presentations


Ads by Google