HOME

TheInfoList



OR:

An attribute grammar is a formal way to supplement a
formal grammar In formal language theory, a grammar (when the context is not given, often called a formal grammar for clarity) describes how to form strings from a language's alphabet that are valid according to the language's syntax. A grammar does not describe ...
with semantic information processing. Semantic information is stored in
attributes Attribute may refer to: * Attribute (philosophy), an extrinsic property of an object * Attribute (research), a characteristic of an object * Grammatical modifier, in natural languages * Attribute (computing), a specification that defines a prope ...
associated with
terminal and nonterminal symbols In computer science, terminal and nonterminal symbols are the lexical elements used in specifying the production rules constituting a formal grammar. ''Terminal symbols'' are the elementary symbols of the language defined by a formal grammar. ...
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 In computer science, an abstract syntax tree (AST), or just syntax tree, is a tree representation of the abstract syntactic structure of text (often source code) written in a formal language. Each node of the tree denotes a construct occurr ...
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
parser Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar. The term ''parsing'' comes from Lat ...
s or
compiler In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs tha ...
s to translate the syntax tree directly into code for some specific machine, or into some
intermediate language An intermediate representation (IR) is the data structure or code used internally by a compiler or virtual machine to represent source code. An IR is designed to be conducive to further processing, such as optimization and translation. A " ...
.


History

Attribute grammars were invented by
Donald Knuth Donald Ervin Knuth ( ; born January 10, 1938) is an American computer scientist, mathematician, and professor emeritus at Stanford University. He is the 1974 recipient of the ACM Turing Award, informally considered the Nobel Prize of computer sc ...
and
Peter Wegner Peter A. Wegner (August 20, 1932 – July 27, 2017) was a computer scientist who made significant contributions to both the theory of object-oriented programming during the 1980s and to the relevance of the Church–Turing thesis for empirical ...
.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.
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 IMP or imp may refer to: * Imp, a fantasy creature Arts and entertainment Fictional characters * Imp (She-Ra), a character in ''She-Ra: Princess of Power'' * Imp a character in '' Artemis Fowl: The Lost Colony'' * Imp, a character in the '' Cl ...
.


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 S-attributed grammars are a class of attribute grammars characterized by having no inherited attributes, but only synthesized attributes. Inherited attributes, which must be passed down from parent nodes to children nodes of the abstract syntax ...
. 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 G= \langle V_n,V_t,P,S \rangle be a formal grammar, where * V_n is the set of non terminal symbols * V_t is the set of terminal symbols * P is the set of productions * S is the distinguished, or start, symbol Then, given a string of nonterminal symbols A and an attribute name a, A.a is a synthesized attribute if all three of these conditions are met: * A \rightarrow \alpha \in P (i.e. A \rightarrow \alpha is one of the rules in the grammar) *\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(\alpha_.a_1, \ldots ,\alpha_.a_m), where \ \subseteq \ (i.e. the value of the attribute is a function f 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 LR-attributed grammars are a special type of attribute grammars. They allow the attributes to be evaluated on LR parsing. As a result, attribute evaluation in LR-attributed grammars can be incorporated conveniently in bottom-up parsing. zyacc is ba ...
: an L-attributed grammar whose ''inherited attributes'' can also be evaluated in
bottom-up parsing In computer science, parsing reveals the grammatical structure of linear input text, as a first step in working out its meaning. Bottom-up parsing recognizes the text's lowest-level small details first, before its mid-level structures, and leavin ...
. *
ECLR-attributed grammar ECLR-attributed grammars are a special type of attribute grammars. They are a variant of LR-attributed grammar LR-attributed grammars are a special type of attribute grammars. They allow the attributes to be evaluated on LR parsing. As a result, ...
: a subset of LR-attributed grammars where equivalence classes can be used to optimize the evaluation of inherited attributes. *
S-attributed grammar S-attributed grammars are a class of attribute grammars characterized by having no inherited attributes, but only synthesized attributes. Inherited attributes, which must be passed down from parent nodes to children nodes of the abstract syntax ...
: a simple type of attribute grammar, using only ''synthesized attributes'', but no ''inherited attributes''


See also

*
Affix grammar An affix grammar is a kind of formal grammar; it is used to describe the syntax of languages, mainly computer languages, using an approach based on how natural language is typically described.Koster, Cornelis HA.Affix grammars for natural languages ...
*
Van Wijngaarden grammar In computer science, a Van Wijngaarden grammar (also vW-grammar or W-grammar) is a two-level grammar which provides a technique to define potentially infinite context-free grammars in a finite number of rules. The formalism was invented by Adriaa ...
*
Syntax-directed translation Syntax-directed translation refers to a method of compiler implementation where the source language translation is completely driven by the parser. A common method of syntax-directed translation is translating a string into a sequence of actions b ...


References

* Original paper introducing attributed grammars: {{Cite journal , url = https://www.csee.umbc.edu/courses/331/fall16/01/resources/papers/Knuth67AG.pdf , title = Semantics of context-free languages , first = Donald E. , surname = Knuth , year = 1968 , authorlink = Donald Knuth , volume = 2 , number = 2 , journal = Mathematical Systems Theory , pages = 127–145 , doi = 10.1007/BF01692511 , s2cid = 5182310
backup


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 In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding behavior to existing code (an advice) ''without'' modifying th ...
to
functional programming In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that ...
by helping writing
catamorphism In category theory, the concept of catamorphism (from the Ancient Greek: "downwards" and "form, shape") denotes the unique homomorphism from an initial algebra into some other algebra. In functional programming, catamorphisms provide generalizat ...
s compositionally. It refers to th
Utrecht University Attribute Grammar
system (see als

as the implementation used in the examples.)
Attribute grammar
in relation to
Haskell Haskell () is a general-purpose, statically-typed, purely functional programming language with type inference and lazy evaluation. Designed for teaching, research and industrial applications, Haskell has pioneered a number of programming lan ...
and
functional programming In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions. It is a declarative programming paradigm in which function definitions are trees of expressions that ...
. * Jukka Paakki
Attribute grammar paradigms—a high-level methodology in language implementation
''ACM Computing Surveys'' 27:2 (June 1995), 196–255.
Silver
is an extensible attribute grammar specification language and system from University of Minnesota. (See also th
GitHub repository
) Formal languages Compiler construction Parsing