SPCC Exp5
SPCC Exp5
Lab Outcome: - Identify and Validate tokens for given high level language and
Implement synthesis phase of compiler
________________________________
Practical Incharge
EXPERIMENT NO-5
Theory:
Syntax tree
Postfix notation
The semantic rules for generating three-address code from common programming
language constructs are similar to those for constructing syntax trees or for
generating postfix notation.
Graphical Representations:
1.Syntax tree:
A syntax tree depicts the natural hierarchical structure of a source program. A dag
(Directed Acyclic Graph) gives the same information but in a more compact way
because common subexpressions are identified. A syntax tree and dag for the
assignment statement a : = b * - c + b * - c are as follows:
2.Postfix notation:
3. Three-Address Code:
t1 : = y * z
t2 : = x + t1
The reason for the term “three-address code” is that each statement usually
contains three addresses, two for the operands and one for the result.
The use of names for the intermediate values computed by a program allows three
address code to be easily rearranged – unlike postfix notation.
For example,
param x1
param x2
.......
paramxn
callp,n
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String a[] = new String[50];
String op[] = {"+", "-", "*", "/", "%", "^"};
String n[] = {"t0", "t1", "t2", "t3", "t4", "t5"};
String exp;
int count = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the arithmetic Expression: ");
exp = sc.nextLine();
a = exp.split(" ");
System.out.println("Op\targument1\targument2\tresult obtained");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < op.length; j++) {
if (a[i].equals(op[j])) {
if (count == 0) {
System.out.println(a[i] + "\t" + a[i - 1] + "\t\t" + a[i + 1] + "\t");
} else {
System.out.println(a[i] + "\t" + n[count - 1] + "\t\t" + a[i + 1] + "\t\t" + n[count
- 1]);
}
count++;
}
}
}
}
}
Output: