
An attribute grammar is a formal way to supplement a formal grammar with semantic information processing. Semantic information is stored in attributes associated with terminal and nonterminal symbols of the grammar. The values of attributes are the result of attribute evaluation rules associated with productions of the grammar. Attributes allow the transfer of information from anywhere in the abstract syntax tree to anywhere else, in a controlled and formal way.
Each semantic function deals with attributes of symbols occurring only in one production rule: both semantic function parameters and its result are attributes of symbols from one particular rule. When a semantic function defines the value of an attribute of the symbol on the left hand side of the rule, the attribute is called synthesized; otherwise it is called inherited. Thus, synthesized attributes serve to pass semantic information up the parse tree, while inherited attributes allow values to be passed from the parent nodes down and across the syntax tree.
In simple applications, such as evaluation of arithmetic expressions, attribute grammar may be used to describe the entire task to be performed besides parsing in straightforward way; in complicated systems, for instance, when constructing a language translation tool, such as a compiler, it may be used to validate semantic checks associated with a grammar, representing the rules of a language not explicitly imparted by the syntax definition. It may be also used by parsers or compilers to translate the syntax tree directly into code for some specific machine, or into some intermediate language.
History
Attribute grammars were invented by Donald Knuth and Peter Wegner. While Donald Knuth is credited for the overall concept, Peter Wegner invented inherited attributes during a conversation with Knuth. Some embryonic ideas trace back to the work of Edgar T. "Ned" Irons, the author of IMP.
Example
The following is a simple context-free grammar which can describe a language made up of multiplication and addition of integers.
Expr → Expr + Term Expr → Term Term → Term * Factor Term → Factor Factor → "(" Expr ")" Factor → integer
The following attribute grammar can be used to calculate the result of an expression written in the grammar. Note that this grammar only uses synthesized values, and is therefore an S-attributed grammar.
Expr1 → Expr2 + Term [ Expr1.value = Expr2.value + Term.value ] Expr → Term [ Expr.value = Term.value ] Term1 → Term2 * Factor [ Term1.value = Term2.value * Factor.value ] Term → Factor [ Term.value = Factor.value ] Factor → "(" Expr ")" [ Factor.value = Expr.value ] Factor → integer [ Factor.value = strToInt(integer.str) ]
Synthesized attributes
A synthesized attribute is computed from the values of attributes of the children. Since the values of the children must be computed first, this is an example of bottom-up propagation. To formally define a synthesized attribute, let be a formal grammar, where
is the set of non terminal symbols
is the set of terminal symbols
is the set of productions
is the distinguished, or start, symbol
Then, given a string of nonterminal symbols and an attribute name
,
is a synthesized attribute if all three of these conditions are met:
(i.e.
is one of the rules in the grammar)
(i.e. every symbol in the body of the rule is either nonterminal or terminal)
, where
(i.e. the value of the attribute is a function
applied to some values from the symbols in the body of the rule)
Inherited attributes
An inherited attribute at a node in parse tree is defined using the attribute values at the parent or siblings. Inherited attributes are convenient for expressing the dependence of a programming language construct on the context in which it appears. For example, we can use an inherited attribute to keep track of whether an identifier appears on the left or the right side of an assignment in order to decide whether the address or the value of the identifier is needed. In contrast to synthesized attributes, inherited attributes can take values from parent and/or siblings. As in the following production,
- S → ABC
where A can get values from S, B, and C. B can take values from S, A, and C. Likewise, C can take values from S, A, and B.
Special types of attribute grammars
- L-attributed grammar: inherited attributes can be evaluated in one left-to-right traversal of the abstract syntax tree
- LR-attributed grammar: an L-attributed grammar whose inherited attributes can also be evaluated in bottom-up parsing.
- ECLR-attributed grammar: a subset of LR-attributed grammars where equivalence classes can be used to optimize the evaluation of inherited attributes.
- S-attributed grammar: a simple type of attribute grammar, using only synthesized attributes, but no inherited attributes
See also
- Affix grammar
- Van Wijngaarden grammar
- Syntax-directed translation
References
- Knuth 1968, p. 134.
- Knuth 1968, p. 132.
- D. E. Knuth: The genesis of attribute grammars. Proceedings of the international conference on Attribute grammars and their applications (1990), LNCS, vol. 461, 1–12.
- "Main".
- Knuth 1968, p. 130.
- Original paper introducing attributed grammars: Knuth, Donald E. (1968). "Semantics of context-free languages" (PDF). Mathematical Systems Theory. 2 (2): 127–145. doi:10.1007/BF01692511. S2CID 5182310. Archived from the original on 2020-05-19. Retrieved 2018-03-23.
{{cite journal}}
: CS1 maint: bot: original URL status unknown (link),
External links
- Why Attribute Grammars Matter, The Monad Reader, Issue 4, July 5, 2005. (This article narrates on how the formalism of attribute grammars brings aspect-oriented programming to functional programming by helping writing catamorphisms compositionally. It refers to the Utrecht University Attribute Grammar Archived 2013-06-05 at the Wayback Machine system (see also Lrc: A Purely Functional, Higher-Order Attribute Grammar based System) as the implementation used in the examples.)
- Attribute grammar in relation to Haskell and functional programming.
- Jukka Paakki: Attribute grammar paradigms—a high-level methodology in language implementation. ACM Computing Surveys 27:2 (June 1995), 196–255.
- Ox is an attribute grammar compiling system that augments Lex and Yacc specifications with definitions of synthesized and inherited attributes written in a combination of Ox and C/C++ syntax. From these specifications, Ox generates ordinary Lex and Yacc specifications that build and decorate an attributed parse tree. Ox works with the Lex and Yacc versions distributed in the Unix and Solaris operating systems, Flex, RE/flex, Bison, BYacc, BtYacc and MSTA (in the DINO GitHub repository). (See the SourceForge repository.)
- Silver is an extensible attribute grammar specification language and system from University of Minnesota. (See also the GitHub repository.)
An attribute grammar is a formal way to supplement a formal grammar with semantic information processing Semantic information is stored in attributes associated with terminal and nonterminal symbols of the grammar The values of attributes are the result of attribute evaluation rules associated with productions of the grammar Attributes allow the transfer of information from anywhere in the abstract syntax tree to anywhere else in a controlled and formal way Each semantic function deals with attributes of symbols occurring only in one production rule both semantic function parameters and its result are attributes of symbols from one particular rule When a semantic function defines the value of an attribute of the symbol on the left hand side of the rule the attribute is called synthesized otherwise it is called inherited Thus synthesized attributes serve to pass semantic information up the parse tree while inherited attributes allow values to be passed from the parent nodes down and across the syntax tree In simple applications such as evaluation of arithmetic expressions attribute grammar may be used to describe the entire task to be performed besides parsing in straightforward way in complicated systems for instance when constructing a language translation tool such as a compiler it may be used to validate semantic checks associated with a grammar representing the rules of a language not explicitly imparted by the syntax definition It may be also used by parsers or compilers to translate the syntax tree directly into code for some specific machine or into some intermediate language HistoryAttribute grammars were invented by Donald Knuth and Peter Wegner While Donald Knuth is credited for the overall concept Peter Wegner invented inherited attributes during a conversation with Knuth Some embryonic ideas trace back to the work of Edgar T Ned Irons the author of IMP ExampleThe following is a simple context free grammar which can describe a language made up of multiplication and addition of integers Expr Expr Term Expr Term Term Term Factor Term Factor Factor Expr Factor integer The following attribute grammar can be used to calculate the result of an expression written in the grammar Note that this grammar only uses synthesized values and is therefore an S attributed grammar Expr1 Expr2 Term Expr1 value Expr2 value Term value Expr Term Expr value Term value Term1 Term2 Factor Term1 value Term2 value Factor value Term Factor Term value Factor value Factor Expr Factor value Expr value Factor integer Factor value strToInt integer str Synthesized attributesA synthesized attribute is computed from the values of attributes of the children Since the values of the children must be computed first this is an example of bottom up propagation To formally define a synthesized attribute let G Vn Vt P S displaystyle G langle V n V t P S rangle be a formal grammar where Vn displaystyle V n is the set of non terminal symbols Vt displaystyle V t is the set of terminal symbols P displaystyle P is the set of productions S displaystyle S is the distinguished or start symbol Then given a string of nonterminal symbols A displaystyle A and an attribute name a displaystyle a A a displaystyle A a is a synthesized attribute if all three of these conditions are met A a P displaystyle A rightarrow alpha in P i e A a displaystyle A rightarrow alpha is one of the rules in the grammar a a1 an i 1 i n ai Vn Vt displaystyle alpha alpha 1 ldots alpha n forall i 1 leq i leq n alpha i in V n cup V t i e every symbol in the body of the rule is either nonterminal or terminal A a f aj1 a1 ajm am displaystyle A a f alpha j 1 a 1 ldots alpha j m a m where aj1 ajm a1 an displaystyle alpha j 1 ldots alpha j m subseteq alpha 1 ldots alpha n i e the value of the attribute is a function f displaystyle f applied to some values from the symbols in the body of the rule Inherited attributesAn inherited attribute at a node in parse tree is defined using the attribute values at the parent or siblings Inherited attributes are convenient for expressing the dependence of a programming language construct on the context in which it appears For example we can use an inherited attribute to keep track of whether an identifier appears on the left or the right side of an assignment in order to decide whether the address or the value of the identifier is needed In contrast to synthesized attributes inherited attributes can take values from parent and or siblings As in the following production S ABC where A can get values from S B and C B can take values from S A and C Likewise C can take values from S A and B Special types of attribute grammarsL attributed grammar inherited attributes can be evaluated in one left to right traversal of the abstract syntax tree LR attributed grammar an L attributed grammar whose inherited attributes can also be evaluated in bottom up parsing ECLR attributed grammar a subset of LR attributed grammars where equivalence classes can be used to optimize the evaluation of inherited attributes S attributed grammar a simple type of attribute grammar using only synthesized attributes but no inherited attributesSee alsoAffix grammar Van Wijngaarden grammar Syntax directed translationReferencesKnuth 1968 p 134 Knuth 1968 p 132 D E Knuth The genesis of attribute grammars Proceedings of the international conference on Attribute grammars and their applications 1990 LNCS vol 461 1 12 Main Knuth 1968 p 130 Original paper introducing attributed grammars Knuth Donald E 1968 Semantics of context free languages PDF Mathematical Systems Theory 2 2 127 145 doi 10 1007 BF01692511 S2CID 5182310 Archived from the original on 2020 05 19 Retrieved 2018 03 23 a href wiki Template Cite journal title Template Cite journal cite journal a CS1 maint bot original URL status unknown link External linksWhy Attribute Grammars Matter The Monad Reader Issue 4 July 5 2005 This article narrates on how the formalism of attribute grammars brings aspect oriented programming to functional programming by helping writing catamorphisms compositionally It refers to the Utrecht University Attribute Grammar Archived 2013 06 05 at the Wayback Machine system see also Lrc A Purely Functional Higher Order Attribute Grammar based System as the implementation used in the examples Attribute grammar in relation to Haskell and functional programming Jukka Paakki Attribute grammar paradigms a high level methodology in language implementation ACM Computing Surveys 27 2 June 1995 196 255 Ox is an attribute grammar compiling system that augments Lex and Yacc specifications with definitions of synthesized and inherited attributes written in a combination of Ox and C C syntax From these specifications Ox generates ordinary Lex and Yacc specifications that build and decorate an attributed parse tree Ox works with the Lex and Yacc versions distributed in the Unix and Solaris operating systems Flex RE flex Bison BYacc BtYacc and MSTA in the DINO GitHub repository See the SourceForge repository Silver is an extensible attribute grammar specification language and system from University of Minnesota See also the GitHub repository