What Is Syntax Directed Translation
What Is Syntax Directed Translation
We use it for semantic analysis, and SDT(Syntax Directed Translation) constructs the parse
tree with Grammar and Semantic action. In grammar, the need to decide who has the highest
priority will be done first, and in semantic action, the determination of what type of action is done
by grammar.
Application of Syntax Directed Translation
We use SDT(Syntax Directed Translation) for
• Executing Arithmetic Expressions
• Conversion from infix to postfix expression
• Conversion from infix to prefix expression
• For Binary to decimal conversion
• Counting the number of Reductions
• Creating a Syntax tree
• Generating intermediate code
• Storing information into the symbol table
• Type checking
Example
Here, we will cover an example of the application of SDT(Syntax Directed Translation) for a better
understanding of the SDT application uses. Let’s consider an example of an arithmetic expression,
and then you will see how we construct an SDT.
For Example: 2*(4+5)
AST(Abstract Syntax Tree) is a condensed form of an SDT tree. It has operators at internal nodes,
not at leaves. Syntactic details like parenthesis, commas, semi-commas, etc., are omitted in AST.
AST is better to structure for later compiler stages since it omits details with the source language.
It only contains information about the essential structure of the program.
For Example: 2*(4+5)
A DAG has leaves corresponding to atomic operands and interior nodes corresponding to
operators. A node N in a DAG has more than one parent if N represents a common sub
expression, so a DAG represents expressions concisely. It gives clues to compiler about the
generating efficient code to evaluate expressions.
Example 1: Given the grammar below, for the input string id + id * id , the parse tree, syntax
tree and the DAG are as shown.
1. Boolean Expression:
Statements called boolean expressions have results that can either be true or
false. An expression that may only be evaluated as true or false is called a
boolean expression after the mathematician George Boole. Let’s examine some
examples in everyday language:
When the label L is encountered for the first time in the source program, whether in a
declaration or as the destination of a forward goto, it is entered into the symbol table,
and a symbolic table for L is generated.
It is important to note that the use of labels and goto statements can make code less
readable and harder to maintain. They can lead to complex control flow and make it
difficult to understand the logical structure of a program. Therefore, it is generally
recommended to use structured control flow constructs, such as loops and conditional
statements, instead of relying heavily on goto statements and labels.
• Makelist (i): This function creates a new list that contains only the index i, which
corresponds to an instruction in the array. Additionally, the function returns a
pointer to the newly generated list.
• Merge (p1, p2): The Merge function concatenates the lists pointed to by p1 and
p2, resulting in a single list that combines the jumps from both lists. The function
then returns a pointer to the concatenated list.
• Backpatch (p, i): The Backpatch function is responsible for inserting the index i
as the target label for each instruction on the list pointed to by p. This operation
ensures that the jumps in the list are redirected to the appropriate target location
represented by the index i.
To illustrate this, let’s consider the concept of backpatching using boolean expressions
and a production rules table. Backpatching involves updating previously generated
code with the correct target addresses or labels.
Step 2: We have to find the TAC(Three address code) for the given expression using
backpatching:
A < B OR C < D AND P < Q
Step 3: Now we will make the parse tree for the expression:
Boolean expressions are an integral part of control statements as they provide the
conditions that govern the flow of control. These expressions evaluate to either true or
false, and their values determine whether certain statements or blocks of code are
executed.
One of the primary purposes of boolean expressions is to modify the flow of control. By
using conditional statements such as if, the execution of specific statements or blocks
of code can be controlled based on the truth value of a boolean expression. For
example, in the statement "if (A) B," if the boolean expression A evaluates to true, the
program will execute the statement B.
Boolean expressions also serve to compute logical values within a program. During the
process of bottom-up parsing, code generation for boolean expressions can be
accomplished using translation techniques. Non-terminal markers in the grammar
trigger semantic actions that capture the index of the next instruction to be generated
at the appropriate moment.
This translation mechanism ensures that the generated code properly handles boolean
expressions and accurately reflects their logical values. By incorporating boolean
expressions and control statements, programmers can create dynamic and conditional
execution paths in their programs, leading to more versatile and efficient code.
Applications of Backpatching
• Conditional statements: Backpatching is frequently used in the code generation
of conditional statements, such as if-else and switch-case statements. During the
parsing and code generation phases, backpatching helps in setting the correct
target addresses for the conditional branches based on the evaluation of the
associated boolean expressions.
• Loop constructs: Backpatching plays a vital role in generating code for loop
constructs, such as while and for loops. It ensures that the loop control
statements, such as the loop condition and loop body, are properly connected
and that the correct target addresses are set for loop entry and exit points.