A context-free-grammar with some additional rules is known as a syntax-directed definition. In SDT, attributes are associated with grammar symbols and rules are associated with productions. The attributes can be any kind of number, type, table, string, etc.
In other words, we can say that; CFG + Semantic rules = SDT.
Inherited and Synthesized Attributes
- SDT supports only these two attributes.
- A synthesized attribute is a nonterminal attribute on the left-side or head of the production. This attribute depicts the information that is being passed to the parse tree. The synthesized attribute can take values only from its children.
Example: Consider a production X => YZ. If attributes of X depend on attributes of Y or Z, then it is known as synthesized attributes.
- An inherited attribute is a nonterminal attribute on the right side of the production. This attribute can take values from their parents or siblings.
Example: Consider a production X => YZ. If an attribute of Y depends on the attribute of X or Z, then it is known as inherited attributes.
It is to note that terminals can have synthesized attributes but not inherited attributes.
Below is an example of a Syntax-directed definition of a simple desk calculator.
PRODUCTION SEMANTIC RULES
L => E n L.val = E.val
E => E1 + T E.val = E1.val + T.val
E => T E: val = T: val
T => T1 * F T: val = T 1 : val # F: val
T => F T: val = F: val
F => (E) F.val = E.val
F => digit F.val = digit.lexval