I have trouble understanding Syntax directed translation/definition/actions. I am reading dragon book but it confuses me even more.
Having production for simple arithmetic
$$ \mathit{expr} \rightarrow \mathit{expr} + \mathit{expr} \mathbin{|} \mathit{expr} - \mathit{expr} \mathbin{|} \mathit{num} $$
$$ \mathit{num} \rightarrow 0 \mathbin{|} 1 \mathbin{|} 2 \mathbin{|} \dots \mathbin{|} 9 $$
If I understood it correctly, SDT just adds attributes to the nodes and define rules.
for this production rules can looks like
$$ \mathit{expr}.\mathit{val} = \mathit{expr}.\mathit{val} + \mathit{expr}.\mathit{val} \mathbin{|} \mathit{expr}.\mathit{val} - \mathit{expr}.\mathit{val} \mathbin{|} \mathit{num}.\mathit{val} $$
$$ \mathit{num}.\mathit{val} = 0 \mathbin{|} 1 \mathbin{|} \dots \mathbin{|} 9 $$
this all together is called Syntax directed definiton.
Now what starts to confuse me:
It is often written that SDT is used to transfer infix to postfix notation e.g
$\mathit{expr} \rightarrow \mathit{expr} + \mathit{expr}$ will have semantic rule such as $\mathit{expr}.\mathit{val} = \mathit{expr}.\mathit{val} \mathbin{\|} \mathit{expr}.\mathit{val} \mathbin{\|} \text{'+'}$, where $\|$ represents concating of strings.
Is STD strictly defined to trasnform notation to postfix?
Also, where in this fall actions?
By definition
A syntax-directed translation scheme is a notation for specifying a translation by attaching program fragments to productions in a grammar. A translation scheme is like a syntax-directed definition, except that the order of evaluation of the semantic rules is explicitly specified. Program fragments embedded within production bodies are called semantic actions
and example
$$ \mathit{expr} \rightarrow^{+} \mathit{expr}\ \ \{\, \mathit{print}(\text{'+'}) \,\}\ \ \mathit{expr} $$
I cannot find what is the usage of this. Which leads to sum of questions: What is STD (rules) for and what are uses of actions in it?
I appreciate all answers, I read wiki but did not help.
Thanks.