13
1 July 3, 2022 1 July 3, 2022 July 3, 2022 Azusa, Azusa, CA CA Sheldon X. Liang Ph. D. Computer Science at Computer Science at Azusa Azusa Pacific University Pacific University Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/ CS400 Compiler Construction CS400 Compiler Construction

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

Embed Size (px)

Citation preview

Page 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

1

April 20, 20231

April 20, 2023April 20, 2023 Azusa, CAAzusa, CA

Sheldon X. Liang Ph. D.

Computer Science at Computer Science at Azusa Pacific UniversityAzusa Pacific University

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS400 Compiler ConstructionCS400 Compiler Construction

Page 2: 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

2

April 20, 20232

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Keep in mind following questionsKeep 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?

Page 3: 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

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

April 20, 20233

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Syntax-Directed TranslationSyntax-Directed Translation

Page 4: 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

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)

April 20, 20234

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Synthesized and Inherited AttributesSynthesized and Inherited Attributes

Page 5: 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

5

expr expr1 + termexpr expr1 - termexpr termterm 0term 1…term 9

expr.t := expr1.t // term.t // “+”expr.t := expr1.t // term.t // “-”expr.t := term.tterm.t := “0”term.t := “1”…term.t := “9”

Production Semantic Rule

String concat operator

April 20, 20235

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Example Attributes GrammarExample Attributes Grammar

Page 6: 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

6

expr.t = “95-2+”

term.t = “2”

9 - 5 + 2

expr.t = “95-”

expr.t = “9” term.t = “5”

term.t = “9”

April 20, 20236

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Example Annotated Parse TreeExample Annotated Parse Tree

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

7

procedure visit(n : node);begin for each child m of n, from left to right do visit(m); evaluate semantic rules at node nend

April 20, 20237

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Depth-First TraversalsDepth-First Traversals

Page 8: 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

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 areof the synthesized type

April 20, 20238

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Depth-First Traversals (Example)Depth-First Traversals (Example)

Page 9: 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

9

• A translation scheme is a CF grammar embedded with semantic actions

rest + term { print(“+”) } rest

Embeddedsemantic action rest

term rest+ { print(“+”) }

April 20, 20239

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Translation SchemesTranslation Schemes

Here “print” means generation of code

Page 10: 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

10

expr expr + termexpr expr - termexpr termterm 0term 1…term 9

{ print(“+”) }{ print(“-”) }

{ print(“0”) }{ print(“1”) }…{ print(“9”) }

April 20, 202310

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Example Translation SchemeExample Translation Scheme

Syntax-directed:

Syntax (rules) that direct action to take

Page 11: 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

11

expr

term

9

-

5

+

2

expr

expr term

term

{ print(“-”) }

{ print(“+”) }

{ print(“9”) }

{ print(“5”) }

{ print(“2”) }

Translates 9-5+2 into postfix 95-2+

April 20, 202311

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Example Translation SchemeExample Translation Scheme

Page 12: 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

12

April 20, 202312

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Got it with following questionsGot it with following questions

• Syntax-directed Translation– What is added in to 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?

Page 13: 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

13

Thank you very much!

Questions?

April 20, 202313

Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/

CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction

Syntax-Directed TranslationSyntax-Directed Translation