As in the following production,Ī can get values from S, B and C. In contrast to synthesized attributes, inherited attributes can take values from parent and/or siblings. Synthesized attributes never take values from their parent nodes or any sibling nodes. If S is taking values from its child nodes (A,B,C), then it is said to be a synthesized attribute, as the values of ABC are synthesized to S.Īs in our previous example (E → E + T), the parent node E gets its value from its child node. To illustrate, assume the following production: These attributes get values from the attribute values of their child nodes. Based on the way the attributes get their values, they can be broadly divided into two categories : synthesized attributes and inherited attributes. Semantic attributes may be assigned to their values from their domain at the time of parsing and evaluated at the time of assignment or conditions. Here, the values of non-terminals E and T are added together and the result is copied to the non-terminal E. The right part of the CFG contains the semantic rules that specify how the grammar should be interpreted. Attribute grammar (when viewed as a parse-tree) can pass values or information among the nodes of a tree.Į → E + T Each attribute has well-defined domain of values, such as integer, float, character, string, and expressions.Īttribute grammar is a medium to provide semantics to the context-free grammar and it can help specify the syntax and semantics of a programming language.
The productions of context-free grammar, which makes the rules of the language, do not accommodate how to interpret them. The plain parse-tree constructed in that phase is generally of no use for a compiler, as it does not carry any information of how to evaluate the tree. We have learnt how a parser constructs parse trees in the syntax analysis phase.