Intermediate Code generation
Again freestudy9 is presenting new post related to Intermediate Code generation. Additionally, we had tried to make it easy with given example related to Intermediate Code generation.
- In the analysis-synthesis model of a compiler, the front end translates a source program synthesis
into an intermediate representation from which backend generates target code.
- The generation of an intermediate language leads to efficient code generation.
- There are certain advantages of generating machine-independent intermediate code,
1.A compiler for a different machine can be created by attaching a backend for the new machine to an existing front end.
2. A machine independent code optimizer can apply to intermediate code in order to optimize the code generation.
L-Attributed Definitions: Intermediate Code generation
- A syntax-directed definition is L attributed if each inherited attribute of Xj, 1<=j<=n, onL-attributed the right side of A-> X1, X2…Xn depends only on,>
1. The attributes of the symbols X1, X2,…Xj 1 to the left of Xj in the production and X1, X2,…Xj-1
2. The inherited attribute of A.
- Above syntax-directed definition is not L attributed because the inherited attribute Q.i ofL-attributed the grammar symbol Q depends on the attribute R.s of the grammar symbol to its right.
Construction of syntax tree for expression: Intermediate Code generation
- Moreover, We use the following function to create the nodes of the syntax tree for expression with a binary operator. Each function returns a pointer to a newly created node.
1. Mknode(op, left, right) creates an operator node with label op and two fields containing pointers to left and right.
2. Mkleaf(id, entry) creates an identifier node with label id and a field containing an entry, a pointer to the symbol table entry for the identifier.
3. Mkleaf(num, val) creates a number node with label num and a field contain containing Val, the value of the number.
Example: construct sysyntax tree for a-4+c
P1:mkleaf(id, entry for a);
P4:mkleaf(id, entry for c);