# Syntax directed translation part of which compiler phase?

I am reading dragon book for compilers. I am not able to understand in which phase does syntax directed translation actually happen.

A CFG can have set of semantic rules attached to it which is evaluated. For example, we can have semantic rules attached to a CFG which adds the type of identifier in the symbol table. Does it happen in syntax analysis or semantic analysis phase?

• Please be patient. It may take some time for people to see your question. Thank you.
– D.W.
Dec 6, 2017 at 17:13
• I asked it again so that my question doesn't get lost. Dec 6, 2017 at 18:20

Here is an example. If you have the following grammar rule: $$stmt \rightarrow\boldsymbol{if}(expr)\text{ }stmt\text{ }\text{ }\boldsymbol{else}\text{ }stmt$$ And if we denote by $[\cdot]$ the translation then the above rule generates the translation rule $$[\boldsymbol{if}(e)\text{ }s_1\text{ }\text{ }\boldsymbol{else}\text{ }s_2]=T_{if}([e],[s_1],[s_2])$$ where $T_{if}$ if the translation action for this rule.
One obvious use of SDT is to generate the AST of the input. In which case $T_{if}$ would look something like (I use ML-like syntax) $$T_{if}(e,s_1,s_2)=AstIfNode(e,s_1,s_2).$$ But they are other possibilities. Furthermore, it is possible to generate an AST with attributes so that we can perform semantic analysis at the same time. For example if we have the rule $$expr\rightarrow expr\boldsymbol{+}expr$$ then a translation for a "calculator-like" language could be $$T_{+}(e_1,e_2)=\begin{cases}\{type=int,val=e_1.val+e_2.val\}&\text{if }e_1.type=int\text{ and }e_1.type=int\\\{type=error\}&\text{otherwise}\end{cases}$$