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 result of attribute evaluation rules associated with productions of the grammar. Attributes allow to transfer information from anywhere in the abstract syntax tree to anywhere else, in a controlled and formal way.[1] 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.[2] 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.
Attribute grammars were invented by Donald Knuth and Peter Wegner.[3] While Donald Knuth is credited for the overall concept, Peter Wegner invented inherited attributes during a conversation with Knuth. Some embryonic ideas trace back[3] to the work of Edgar T. "Ned" Irons,[4] the author of IMP.
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) ]
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.[5] To formally define a synthesized attribute, let [math]\displaystyle{ G= \langle V_n,V_t,P,S \rangle }[/math] be a formal grammar, where
Then, given a string of nonterminal symbols [math]\displaystyle{ A }[/math] and an attribute name [math]\displaystyle{ a }[/math], [math]\displaystyle{ A.a }[/math] is a synthesized attribute if all three of these conditions are met:
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,
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.
Original source: https://en.wikipedia.org/wiki/Attribute grammar.
Read more |