

This article is contributed by Vineet Purswani. 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.į -> INTLIT ], where E & Ep are same production symbols annotated to differentiate between parent and child, has an inherited attribute val corresponding to node T. 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. This is a grammar to syntactically validate an expression having additions and multiplications in it. In many cases, translation can be done during parsing without building an explicit tree. 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.

Syntax-directed translation rules use 1) lexical values of nodes, 2) constants & 3) attributes associated with the non-terminals in their definitions. SDT involves passing information bottom-up and/or top-down the parse tree in form of attributes attached to the nodes. Syntax Directed Translation has augmented rules to the grammar that facilitate semantic analysis.

Now to interleave semantic analysis with the syntax analysis phase of the compiler, we use Syntax Directed Translation. Output could be either a parse tree or an abstract syntax tree.
