Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bernd Fischer COMP2010: Compiler Engineering Attribute Grammars and Attribute Evaluation.

Similar presentations


Presentation on theme: "Bernd Fischer COMP2010: Compiler Engineering Attribute Grammars and Attribute Evaluation."— Presentation transcript:

1 Bernd Fischer b.fischer@ecs.soton.ac.uk COMP2010: Compiler Engineering Attribute Grammars and Attribute Evaluation

2 Attributes store extra information in AST nodes. type:int val:3 code: iconst_3... type:int val:4 code: iconst_4... type:int val:4 env: offs:1 code: a_load1... type:double val:1.0 code: dconst_1... '*' '+' IntLit '+''+' RealLitId... xintconst4... type:double val:35.0 env: code: swap i2d dmul... Typical attributes: type value environment code type:int val:7 code: iadd... IntLit type:double...

3 Attribute Evaluation Conceptually, attributes are functions on entire ASTs: attr: AST × Node → AVal ⇒ need restrictions for efficient evaluation In practice, attributes are evaluated by scanner: attached to tokens (e.g., source locations) –too restricted parser: integrated with AST construction –common attribute evaluator: extra AST traversals –common in later phases (e.g., code generation)

4 Attribute Evaluation Conceptually, attributes are functions on entire ASTs: attr: AST × Node → AVal ⇒ need restrictions for efficient evaluation An attribute at a node can only be computed from attributes at parent, siblings, and children.

5 Synthesized attributes are computed from children. type:int val:3... type:int val:4... type:int val:4... type:double val:1.0... '*' '+' IntLit '+''+' Id type:int val:7... IntLit type:double val:5.0... RealLit type:double val:35.0... ⇒ evaluation functions still depend on current node

6 Inherited attributes are computed from parent and siblings. '*' '+' IntLit '+''+' IdIntLitRealLit Id... xintconst4 env: offs:2 env: offs:1 env:... xintconst4 ydoubleconst- ConstDecl

7 Attribute Evaluation Order inherited synthesized... computed available Careful: cyclic dependencies A → BA.s = B.i B.i = A.s + 1 A A.s B B.i cycle detection in grammar is NP complete!

8 Attribute Evaluation Rules Attribute evaluation can be added to parse rules decl returns [String type, List vars] : t = type ids+=ID (',' ids+=ID)* {$type = $t.text; $vars = $ids} ; synthesized attributes evaluation functions

9 Attribute Evaluation Rules (II) Difference between synthesized and inherited attributes: Synthesized attribute for non-terminal A at a node N –defined by evaluation rule associated with production at N –production must have A as its head –defined in terms of N’s children and N itself Inherited attribute for non-terminal B at a node N –defined by evaluation rule associated with production at parent of N –production must have B in its body –defined in terms of N’s parent and siblings, and N itself

10 Attributed Grammars We can limit the use synthesized and inherited attributes to guarantee that the attribute evaluation is cycle-free: A grammar is S-attributed if every attribute is synthesized. A grammar is L-attributed if every attribute is 1.synthesized, or 2.inherited, with the restriction that the evaluation rule uses only  attributes inherited from the parent, or  attributes (synthesized or inherited) from left siblings

11 Attribute Evaluation Order vs. Parsing S-attributed grammars are well-suited for LR-parsing (bottom-up): –when applying a reduce operation, all attribute values are available to calculate the attributes of the parent. L-attributed grammars are well-suited for LL-parsing (top-down, left-to-right): –when calling a parse rule function, all attribute values are available to calculate the attributes of the non- terminal. L-attributed grammars can be used for LR-parsing –inheritance from above is tricky

12 Summary (Or: What to know for the exam) synthesized vs. inherited attributes attributed grammars attribute evaluation and parsing


Download ppt "Bernd Fischer COMP2010: Compiler Engineering Attribute Grammars and Attribute Evaluation."

Similar presentations


Ads by Google