
Introduction
By connecting semantic rules to grammar productions, syntax-directed definition determines the values of attributes. It resembles an abstract specification in certain ways.
SDT (syntax-directed translation) adds 'semantics rules' or actions to CFG products. As a result, we may refer to the grammar that has been augmented as attributed grammar.
The 'syntax analysis phase' and the 'semantic analysis phase' are intertwined in syntax-directed translation. To begin the syntax-directed translation, you must first create a 'parse tree.' Following that, apply the production's semantic rules. And then assess the property at the parse tree's nodes.
An annotated parse tree is a parse tree that has the evaluated attribute value. 'L-attributed translations' and 'S-attributed translations' are the two types of syntax-directed translation.
Syntax Directed Definition
A CFG with attributes and rules is called a syntax-directed definition (SDD). The properties are linked to the grammar symbols in an extended CFG (i.e. nodes of the parse tree). The rules are associated with grammar production.
A grammar symbol's attribute can now be integers, types, table references, or a string. Let us go through two kinds of qualities in more detail:
Inherited and Synthesised Attribute
For a non-terminal symbol (node) N, we can declare the value of a synthesized attribute:
- The attribute value of children.
- The total attribute values of N.
For a non-terminal symbol (node) N, we can declare the value of an inherited attribute:
- N's parent's attribute values.
- N's Siblings' attribute values.
- Total attribute values of N.
Evaluation of an SDD at Node of the Parse Tree
To determine the value of attributes at nodes in a parse tree, do the following:
- We must first construct a parse tree.
- Then we must use SDD rules to assess the values of all the nodes in the parse tree's attributes.
- An annotated parse tree is formed via this improvised parse tree.
How to Construct Annotated Parse Tree?
As the name implies, the annotated parse tree displays the value(s) of all node attribute(s). So, before we analyze a node's attribute value, we must first evaluate all the elements that determine its value.
- To evaluate a node's synthesized attribute, we must parse the tree from the bottom up. Its value is determined by the value of the child attributes of the concerned node and the node itself.
- To determine a node's inherited attribute, we must parse the tree from the top down. Its value is determined by its parent, siblings, and the node itself.
- Synthesized and inherited attributes may coexist at some nodes in a parse tree. In this scenario, we're not sure if there is even a single order in which the nodes' characteristics may be evaluated.
A dependency tree can even determine the order in which the characteristics are examined
