-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntm_code_gen.java
More file actions
37 lines (30 loc) · 1.21 KB
/
Copy pathIntm_code_gen.java
File metadata and controls
37 lines (30 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class Intm_code_gen {
static int tempCount = 1;
public static void main(String[] args) {
String expression = "a+b*c-d";
generateIntermediateCode(expression);
}
public static void generateIntermediateCode(String expression) {
char[] tokens = expression.toCharArray();
for (int i = 0; i < tokens.length - 2; i += 2) {
char left = tokens[i];
char operator = tokens[i + 1];
char right = tokens[i + 2];
if (operator == '*' || operator == '/') {
String temp = "t" + tempCount++;
System.out.println(temp + " = " + left + " " + operator + " " + right);
tokens[i + 2] = temp.charAt(0);
}
}
for (int i = 0; i < tokens.length - 2; i += 2) {
char left = tokens[i];
char operator = tokens[i + 1];
char right = tokens[i + 2];
if (operator == '+' || operator == '-') {
String temp = "t" + tempCount++;
System.out.println(temp + " = " + left + " " + operator + " " + right);
tokens[i + 2] = temp.charAt(0);
}
}
}
}