Attribute Grammars Attribute Grammars
Attribute Grammars Attribute Grammars
Formal framework based on grammar and parse tree Aho, Sethi, & Ullman describe syntax-directed definitions. These are
just attribute grammars by another name.
Idea: “attribute” the tree
Attribute grammar
– can add attributes (fields) to each node
– generalization of context-free grammar
– specify equations to define values
– each grammar symbol has an associated set of attributes
– both inherited and synthesized attributes
– augment grammar with rules defining attribute values
– high-level specification, independent of evaluation scheme
Attribute grammars are very general. Can be used for • Note: translation scheme has evaluation order
– infix to postfix translation of arithmetic expressions (ASU p.34) Dependencies among attributes
– type checking (context-sensitive analysis) – values are computed from constants & other attributes
– construction of intermediate representation (AST) – synthesized attribute - value computed from children
– desk calculator (interpreter) – inherited attribute - value computed from siblings & parent
– code generation (compiler) – key notion: induced dependency graph
1
Example Attribute grammars
The attribute dependency graph
• nodes represent attributes
• edges represent the flow of values
• graph is specific to parse tree
LIST0 pos
• size is related to parse tree's size
val • can be built alongside parse tree
The dependency graph must be acyclic
LIST1 BIT
pos pos
Evaluation order
val val
• Topological sort of the dependency graph to order
attributes
Note: • using this order, evaluate the rules
• semantic rules define a partial dependency graph
• structure can be used to derive characteristics of generated This order depends on both the grammar and the
total dependency graphs input string
pos
pos SIGN neg LIST0 val
SIGN neg LIST val
pos pos
pos pos
LIST1 val BIT2 val
LIST BIT • val and neg are synthesized attributes
val val
• pos is an inherited attribute
pos pos
LIST2 val
BIT1 val
pos pos LIST0.pos is an inherited attribute with
LIST val
BIT val an empty dependency set.
pos
BIT0 val
pos
BIT val
- 1 0 1
- 1 0 1
2
Attribute grammars Example grammar final result
A topological order for the example
0
1. SIGN.neg NUM val :-5
2. LIST0 .pos
pos: 0
3. LIST1 .pos SIGN neg : T LIST0 val: 5
4. LIST2 .pos Evaluate in this order
5. BIT0 .pos pos: 1 pos: 0
LIST1 BIT2
6. BIT1 .pos Yields NUM.val: -5 val: 4 val: 1
7. BIT2 .pos
pos: 2 pos: 1
8. BIT0 .val LIST2 val: 4 BIT1 val: 0
9. LIST2 .val
10. BIT1 .val
pos: 2
11. LIST1 .val BIT0 val: 4 The evaluation
12. BIT2 .val process is also called
13. LIST0 .val decorating the
- 1 0 1
14. NUM.val parse tree
3
Example: AST construction Example: AST construction
Semantic rules can be implemented as operations on the
stack.
4
Real World Evaluating S-attributed Definitions
• In practice, computing all of the attribute
dependencies from the tree is rarely, if ever, done • Bottom-up evaluation
• Instead, syntax-directed definitions are used • Keep a stack S parallel to parsing stack
where the attribute evaluation order can be – consider production A →XY with rule A.val = X.val + Y.val
determined from the actions – When reducing by A →XY the top of the S stack has X.val and
Y.val
– compute A.val
• The most important special case is S-attributed – pop X.val and Y.val from S, push A.val
grammars (grammars with only synthesized • Symmetric with reduce action on the parse stack
attributes)
– These attributes can be evaluated bottom-up during
• Tools like Bison/Flex support S-attributed definitions
parsing
5
Example translation schemes L-attributed grammars
AST construction example:
Restrictions for translation scheme to make sure that
attribute grammar is L-attributed:
– || is a concatenation operator
– addr(id) returns memory address of identifier id
What does the translation scheme look like?
6
BU evaluation of inherited attributes Attribute grammars
Method to implement L-attributed definitions for
bottom-up parsing: Advantages
• Can handle all L-attributed definitions • clean formalism
corresponding to LL(1) grammars and some LR(1) • automatic generation of evaluator
(not all!). • high-level specification
• Basic idea:
1. Transform grammar so all embedded actions of
translation scheme occur at the end of RHS of some Disadvantages
production (i.e. at a reduction). This will make sure that
attribute values will be in the stack.
• evaluation strategy determines efficiency
2. Introduce copy rules if necessary to locate attribute values • increased space requirements
in the stack. • results distributed over tree
3. Note: modifications to input attribute grammar must not
change its LR-ness.