🔗 syntax-directed translation - geeksforgeeks

To interleave semantic analysis with the syntax analysis phase of the compiler, we use Syntax Directed Translation.

The general approach to Syntax-Directed Translation is to construct a parse tree or syntax tree and compute the values of attributes at the nodes of the tree by visiting them in some order. In many cases, translation can be done during parsing without building an explicit tree.

E -> E+T | T
T -> T*F | F
F -> INTLIT

This is a grammar to syntactically validate an expression having additions and multiplications in it. Now, to carry out semantic analysis we will augment SDT rules to this grammar, in order to pass some information up the parse tree and check for semantic errors, if any. In this example, we will focus on the evaluation of the given expression, as we don’t have any semantic assertions to check in this very basic example.

The process of Syntax-Directed Translation is Two-fold:

  1. Syntax-Directed Definitions (major concept: attributes, semantic rules)
  2. Syntax-Directed Translation Schemes (major concept: semantic actions)

Syntax-Directed Definitions(SDD)


🔗 syntax-directed definition - geeksforgeeks

🔗 syntax-directed definition - computer notes

Syntax Directed Definition(SDD) specifies the values of attributes by associating semantic rules with the grammar productions.