three address code generation program in java

Solutions on MaxInterview for three address code generation program in java by the best coders in the world

showing results for - "three address code generation program in java"
Trace
31 Jun 2018
1/*
2	Program to implement Three Address Code in Java
3	Author: Surajit Karmakar		Author Link: https://www.facebook.com/surajit.3528
4	www.pracspedia.com
5*/
6import java.io.*;
7
8class ThreeAddressCode
9{
10	private static final char[][] precedence = {
11		{'/', '1'},
12		{'*', '1'},
13		{'+', '2'},
14		{'-', '2'}
15	};
16	
17	private static int precedenceOf(String t)
18	{
19		char token = t.charAt(0);
20		for (int i=0; i < precedence.length; i++)
21		{
22			if (token == precedence[i][0])
23			{
24				return Integer.parseInt(precedence[i][1]+"");
25			}
26		}
27		return -1;
28	}
29	
30	public static void main(String[] args) throws Exception
31	{
32		int i, j, opc=0;
33		char token;
34		boolean processed[];
35		String[][] operators = new String[10][2];
36		String expr="", temp;
37		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
38		System.out.print("\nEnter an expression: ");
39		expr = in.readLine();
40		processed = new boolean[expr.length()];
41		for (i=0; i < processed.length; i++)
42		{
43			processed[i] = false;
44		}
45		for (i=0; i < expr.length(); i++)
46		{
47			token = expr.charAt(i);
48			for (j=0; j < precedence.length; j++)
49			{
50				if (token==precedence[j][0])
51				{
52					operators[opc][0] = token+"";
53					operators[opc][1] = i+"";
54					opc++;
55					break;
56				}
57			}
58		}
59		System.out.println("\nOperators:\nOperator\tLocation");
60		for (i=0; i < opc; i++)
61		{
62			System.out.println(operators[i][0] + "\t\t" + operators[i][1]);
63		}
64		//sort
65		for (i=opc-1; i >= 0; i--)
66		{
67			for (j=0; j < i; j++)
68			{
69				if (precedenceOf(operators[j][0]) > precedenceOf(operators[j+1][0]))
70				{
71					temp = operators[j][0];
72					operators[j][0] = operators[j+1][0];
73					operators[j+1][0] = temp;
74					temp = operators[j][1];
75					operators[j][1] = operators[j+1][1];
76					operators[j+1][1] = temp;
77				}				
78			}
79		}
80		System.out.println("\nOperators sorted in their precedence:\nOperator\tLocation");
81		for (i=0; i < opc; i++)
82		{
83			System.out.println(operators[i][0] + "\t\t" + operators[i][1]);
84		}
85		System.out.println();
86		for (i=0; i < opc; i++)
87		{
88			j = Integer.parseInt(operators[i][1]+"");
89			String op1="", op2="";
90			if (processed[j-1]==true)
91			{
92				if (precedenceOf(operators[i-1][0]) == precedenceOf(operators[i][0]))
93				{
94					op1 = "t"+i;
95				}
96				else
97				{
98					for (int x=0; x < opc; x++)
99					{
100						if ((j-2) == Integer.parseInt(operators[x][1]))
101						{
102							op1 = "t"+(x+1)+"";
103						}
104					}
105				}
106			}
107			else
108			{
109				op1 = expr.charAt(j-1)+"";
110			}
111			if (processed[j+1]==true)
112			{
113				for (int x=0; x < opc; x++)
114				{
115					if ((j+2) == Integer.parseInt(operators[x][1]))
116					{
117						op2 = "t"+(x+1)+"";
118					}
119				}
120			}
121			else
122			{
123				op2 = expr.charAt(j+1)+"";
124			}
125			System.out.println("t"+(i+1)+" = "+op1+operators[i][0]+op2);
126			processed[j] = processed[j-1] = processed[j+1] = true;
127		}
128	}
129}
130
similar questions