In syntax directed translation, along with the grammar we associate some informal notations and these notations are called as semantic rules.
So we can say that
Syntax direct translation is implemented by constructing a parse tree and performing the actions in a left to right depth first order.
Parse tree for SDT:
Intermediate code:
Intermediate code can be represented in two ways:
High level intermediate code can be represented as source code. To enhance performance of source code, we can easily apply code modification. But to optimize the target machine, it is less preferred.
Low level intermediate code is close to the target machine, which makes it suitable for register and memory allocation etc. it is used for machine-dependent optimizations.
When you create a parse tree then it contains more details than actually needed. So, it is very difficult to compiler to parse the parse tree. Take the following parse tree as an example:
A sentence id + id * id would have the following syntax tree:
Abstract syntax tree can be represented as:
Abstract syntax trees are important data structures in a compiler. It contains the least unnecessary information.
Abstract syntax trees are more compact than a parse tree and can be easily used by a compiler.
The quadruples have four fields to implement the three address code. The field of quadruples contains the name of the operator, the first source operand, the second source operand and the result respectively.
Consider the grammar
The translation scheme of above grammar is given below:
Production rule | Semantic actions |
---|---|
S → id :=E | {p = look_up(id.name); If p ≠ nil then Emit (p = E.place) Else Error; } |
E → E1 + E2 | {E.place = newtemp(); Emit (E.place = E1.place '+' E2.place) } |
E → E1 * E2 | {E.place = newtemp(); Emit (E.place = E1.place '*' E2.place) } |
E → (E1) | {E.place = E1.place} |
E → id | {p = look_up(id.name); If p ≠ nil then Emit (p = E.place) Else Error; } |
Boolean expressions have two primary purposes. They are used for computing the logical values. They are also used as conditional expression using if-then-else or while-do.
Here is the example which generates the three address code using the above translation scheme:
Here, S is a statement, L is a statement-list, A is an assignment statement and E is a Boolean-valued expression.
![]() |
Download the notes
Simple Syntax Directed Translator
|
Download as PDF |
In a production A → α, the translation rule of A.CODE consists of the concatenation of the CODE translations of the non-terminals in α in the same order as the non-terminals appear in α.
Production can be factored to achieve postfix form.
A suitable transition scheme would be
Elements of arrays can be accessed quickly if the elements are stored in a block of consecutive location. Array can be one dimensional or two dimensional.
Row major or column major forms
Procedure is an important and frequently used programming construct for a compiler. It is used to generate good code for procedure calls and returns.
The translation for a call includes a sequence of actions taken on entry and exit from each procedure. Following actions take place in a calling sequence:
26 videos|67 docs|30 tests
|
1. What is a Syntax Directed Translator in computer science engineering? | ![]() |
2. How does a Syntax Directed Translator differ from a traditional compiler? | ![]() |
3. What is the role of semantic actions in a Syntax Directed Translator? | ![]() |
4. What are some advantages of using a Syntax Directed Translator in software development? | ![]() |
5. Can a Syntax Directed Translator handle complex programming languages with advanced features? | ![]() |