Search: in
Attribute grammar
Attribute grammar in Encyclopedia Encyclopedia
  Tutorials     Encyclopedia     Videos     Books     Software     DVDs  
       





Attribute grammar

An attribute grammar is a formal way to define attributes for the productions of a formal grammar, associating these attributes to values. The evaluation occurs in the nodes of the abstract syntax tree, when the language is processed by some parser or compiler.

The attributes are divided into two groups: synthesized attributes and inherited attributes. The synthesized attributes are the result of the attribute evaluation rules, and may also use the values of the inherited attributes. The inherited attributes are passed down from parent nodes.

In some approaches, synthesized attributes are used to pass semantic information up the parse tree, while inherited attributes help pass semantic information down and across it. For instance, when constructing a language translation tool, such as a compiler, it may be used to assign semantic values to syntax constructions. Also, it is possible to validate semantic checks associated with a grammar, representing the rules of a language not explicitly imparted by the syntax definition.

Attribute grammars can also be used to translate the syntax tree directly into code for some specific machine, or into some intermediate language.

One strength of attribute grammars is that they can transport information from anywhere in the abstract syntax tree to anywhere else, in a controlled and formal way.

Contents


Example

The following is a simple Context-free grammar which can describe a language made up of multiplication and addition of integers.

 ExprExpr + Term  ExprTerm  TermTerm * Factor  TermFactor  Factor → "(" Expr ")"  Factorinteger

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.

 Expr1Expr2 + Term [ Expr1.value = Expr2.value + Term.value ]  ExprTerm [ Expr.value = Term.value ]  Term1Term2 * Factor [ Term1.value = Term2.value * Factor.value ]  TermFactor [ Term.value = Factor.value ]  Factor → "(" Expr ")" [ Factor.value =  Expr.value ]  Factorinteger [ Factor.value = strToInt(integer.str) ]  

Synthesized attributes

Let G=<V_n,V_t,P,S> be an Attribute 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 symbol, that is the start symbol

A.a is a synthesized attribute if,

  • A \rightarrow \alpha \in P
  • \alpha = \alpha_1 \ldots \alpha_n, \forall_{i, 1 \leq i \leq n}: \alpha_i \in (V_n \cup V_t)
  • \{\alpha_{j1}, \ldots ,\alpha_{jm}\} \subseteq \{\alpha_1, \ldots ,\alpha_n\}
  • A.a = f(\alpha_{j1}.a_1, \ldots ,\alpha_{jm}.a_m)

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.

Production rule Semantic rule
S T L L.in:=T.type
T int T.type:=integer
T float T.type:=float
T char T.type:=char
T double T.type:=double
L L1, id L1.in=L.in enter_type(id.entry, L.in)
L id enter_type(id.entry, L.in)

Special types of attribute grammars

See also

External links

de:Attributgrammatik fr:Grammaire attribu e nl:Attributengrammatica ja: pt:Gram tica de atributos






Source: Wikipedia | The above article is available under the GNU FDL. | Edit this article



Search for Attribute grammar in Tutorials
Search for Attribute grammar in Encyclopedia
Search for Attribute grammar in Videos
Search for Attribute grammar in Books
Search for Attribute grammar in Software
Search for Attribute grammar in DVDs
Search for Attribute grammar in Store




Advertisement




Attribute grammar in Encyclopedia
Attribute_grammar top Attribute_grammar

Home - Add TutorGig to Your Site - Disclaimer

©2011-2013 TutorGig.info All Rights Reserved. Privacy Statement