ArticleslgStudy

science

Attribute grammar

Attribute grammar is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Attribute grammar rather than just read about it. In short: 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.

Key takeaways

  • Attribute grammar belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Attribute grammar to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Attribute grammar from memory before moving on to harder problems.

Reference excerpt

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 G=⟨Vn,Vt,P,S⟩ be a formal grammar, where

Vn is the set of non terminal symbols Vt 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→α)∈P (i.e. A→α is one of the rules in the grammar) α=α1…αn,∀i,1≤i≤n:αi∈(Vn∪Vt) (i.e. every symbol in the body of the rule is either nonterminal or terminal) A.a=f(αj1.a1,…,αjm.am), where {αj1,…,αjm}⊆{α1,…,αn} (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: 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

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),

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Attribute grammar

Start with the simplest possible case. Write down what Attribute grammar claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Attribute grammar before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Attribute grammar ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Attribute grammar

In research
Attribute grammar appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Attribute grammar in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Attribute grammar is common in secondary-school and first-year university syllabi. It links to neighbouring topics Compiler construction, Formal languages, Parsing, so understanding it makes those chapters shorter.
In everyday life
Look for Attribute grammar outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Attribute grammar” →

How to study Attribute grammar in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Attribute grammar means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Attribute grammar out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Attribute grammar in simple terms?

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.

Why does Attribute grammar matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Attribute grammar?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Attribute grammar.

Tags

  • Compiler construction
  • Formal languages
  • Parsing

Keep exploring