Sie sind auf Seite 1von 79

package ImiPackage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
// a=-(b+c)*m
// a=-(b*10)-(c/d)+g
// a=-88*cd/d
// a=a-(b+c)*(d+e)/f
// a=z+(c*(b+(c-l)))/f
// a=-b*(c+d)

/**
*
* @author OnlyImran
*/
public class ThreeAddressCode {

InputStreamReader ir = new InputStreamReader(System.in);


BufferedReader br = new BufferedReader(ir);
String splitted[];
String expr = "", bodmas = "()/*+-", left = "", right = "", temp = "", t = "T", operators = "", operands =
"", op = "=/*+-";
int tempCount = 1, cntOpen = 0, cntClose = 0, cntOperator = 0;

ArrayList<String> threeLeft = new ArrayList();


ArrayList<String> threeRight = new ArrayList();
ArrayList<Integer> openBrace = new ArrayList();
ArrayList<Integer> closeBrace = new ArrayList();
ArrayList<String> optimizedLeft = new ArrayList();
ArrayList<String> optimizedRight = new ArrayList();
boolean matched = false, gotOperator = false, nestedBrace = false, innerMatched = false;
String s1 = "", s2 = "", s3 = "", subS1 = "", subS2 = "", subS3 = "", arg1 = "", arg2 = "", opr = "";
String[][] quadruple;

public ThreeAddressCode() {
System.out.println("Enter grammar in following format:\nExpr=Expr");
try {
while ((expr = br.readLine()) != null && expr.length() != 0) {
splitted = expr.split("=");
left = splitted[0];
right = splitted[1];
}
} catch (IOException ex) {

}
expr = left + " = " + right;
for (int i = 0; i < expr.length(); i++) {
for (int j = 0; j < op.length(); j++) {
if ((op.charAt(j) + "").equals(expr.charAt(i) + "")) {
operators = operators + " " + expr.charAt(i);

}
}
if (expr.charAt(i) > 95 && expr.charAt(i) < 122) {
operands = operands + " " + expr.charAt(i);
}
}
System.out.println("Operands are: " + operands);
System.out.println("Operators are: " + operators + "\n");
checkOperator();
if (cntOperator > 1) {
if ("-".equals(right.charAt(0) + "") && ("(".equals(right.charAt(1) + ""))) {
chekUnaryParanthesis();
}
if ("-".equals(right.charAt(0) + "")) { //// -b*c or -13*c
checkUnary();
}
for (int i = 0; i < right.length(); i++) {
if ("(".equals(right.charAt(i) + "")) {
openBrace.add(i);
}
if (")".equals(right.charAt(i) + "")) {
closeBrace.add(i);
}
}
// checking for nested braces
if (openBrace.size() >= 1) {

if (openBrace.size() == 1) { // Because if size is exactly 1 then else if will throw error-->


openBrace.get(1);
removeBraces();
checkOperator();
} else if (openBrace.get(1) > closeBrace.get(0)) {
nestedBrace = false; // Not used anywhere, just for notification
removeBraces();
checkOperator();
} else {
nestedBrace = true; // Not used anywhere
removeNested();
checkOperator();
}
}

if (cntOperator > 1) {
while (cntOperator != 1) {
//System.out.println("cnt Operator is: " + cntOperator);
cntOperator--;
for (int i = 0; i < bodmas.length(); i++) {
for (int j = 0; j < right.length(); j++) {
if ((bodmas.charAt(i) + "").equals(right.charAt(j) + "")) {
//System.out.println("matched i=" + j);
matched = true;

// checking for inner match <-- z=b+a*10/20*d+e


for (int k = j - 1; k >= 0; k--) {

for (int l = 0; l < bodmas.length(); l++) {


if ((right.charAt(k) + "").equals(bodmas.charAt(l) + "")) {
//System.out.println("MATCHED INNER");
innerMatched = true;
subS1 = right.substring(k + 1, j); // 10
subS2 = right.charAt(j) + "";

// /

s1 = right.substring(0, k + 1); // b+a*


break;
} else if (k == 0) { // consider input: b*c+d
// System.out.println("Reached End start");
subS1 = right.substring(0, j); // b
subS2 = right.charAt(j) + ""; // *
s1 = ""; //
break;
}
}
if (innerMatched) {
innerMatched = false;
break;
}
}
// checking for inner match --> z=b+a*10/20*d+e
for (int k = j + 1; k < right.length(); k++) {
for (int l = 0; l < bodmas.length(); l++) {
if ((right.charAt(k) + "").equals(bodmas.charAt(l) + "")) {
innerMatched = true;

subS3 = right.substring(j + 1, k); // 20


s3 = right.substring(k, right.length()); // *d+e

} else if (k == right.length() - 1) { // assume as input: b+c*d


subS3 = right.substring(j + 1, right.length()); // d
s3 = "";
}
}
if (innerMatched) {
innerMatched = false;
break;
}
}
s2 = subS1 + subS2 + subS3;
temp = t + tempCount;
threeLeft.add(temp);
threeRight.add(s2);
right = s1 + temp + s3;
// System.out.println("Right=" + right);
tempCount++;

break;
}
}
if (matched) {
matched = false;

break;
}
}
}
checkOperator();
}
} else {
System.out.println("Please enter proper input:");
}
System.out.println("Three Left" + threeLeft);
System.out.println("Three right" + threeRight);
// Printing final output
System.out.println("Printing three address code of:" + expr);
for (int i = 0; i < threeLeft.size(); i++) {
System.out.println(threeLeft.get(i) + " = " + threeRight.get(i));
}
}

private void chekUnaryParanthesis() {


// - ( b * 1 0 ) + d
// 0 1 2 3 4 5 6 7 8

cntOpen = right.indexOf("(");
cntClose = right.indexOf(")");
s1 = right.charAt(0) + ""; // s2 = right.substring(cntOpen + 1, cntClose); // b*c

s3 = right.substring(cntClose + 1, right.length()); // +d
temp = t + tempCount; // T1
threeLeft.add(temp); // T1
threeRight.add(s2); // b*10
right = s1 + temp + s3; // -T1+d
// System.out.println("Right=" + right);
tempCount++;
}

void checkUnary() {
// - T 1 * d
// 0 1 2 3

for (int i = 1; i < right.length(); i++) {


for (int j = 0; j < bodmas.length(); j++) {
if (("" + right.charAt(i)).equals(bodmas.charAt(j) + "")) {
s1 = "";
s2 = right.substring(0, i); // -T1
s3 = right.substring(i, right.length()); // +d
temp = t + tempCount; // T3
threeLeft.add(temp); // T3
threeRight.add(s2); // -T1
right = temp + s3; // T3+d
//

System.out.println("Right=" + right);
tempCount++;
checkOperator();

matched = true;
}
}
if (matched) {
matched = false;
break;
}
}
}

void checkOperator() {
cntOperator = 0;
for (int i = 0; i < right.length(); i++) {
for (int j = 0; j < bodmas.length(); j++) { // open ( and close ) braces are decreased to 0 coz of
cntClose-if (("" + right.charAt(i)).equals(bodmas.charAt(j) + "")) {
cntOperator++;
}
}
}
// System.out.println("Operator count=" + cntOperator);
if (cntOperator == 1 && "-".equals("" + right.charAt(0))) { // a=-b

System.out.println("Single input: ");


threeLeft.add("T1");
threeRight.add(right);
threeLeft.add(left);

threeRight.add("T1");

//threeRight.add(right);

} else if (cntOperator == 1) {
threeLeft.add(left);
threeRight.add(right);

}
}

void removeNested() {
// a + ( c * ( b + c ) ) / f
// 0 1 2 3 4 5 6 7 8 9 10 11 12
for (int i = 0; i < openBrace.size(); i++) {
cntOpen = right.lastIndexOf("("); // 5
cntClose = right.indexOf(")");// 9
s1 = right.substring(0, cntOpen); // a+(c*
s2 = right.substring(cntOpen + 1, cntClose); // b+c
s3 = right.substring(cntClose + 1, right.length()); // )/f

temp = t + tempCount;
threeLeft.add(temp);
threeRight.add(s2);
right = s1 + temp + s3;
// System.out.println("Right=" + right);
tempCount++;
}

10

void removeBraces() {
// a + ( b + c ) * ( d + e ) / f
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
// System.out.println(openBrace.size());
for (int i = 0; i < openBrace.size(); i++) {
cntOpen = right.indexOf("("); // 2
cntClose = right.indexOf(")"); // 6
s1 = right.substring(0, cntOpen); // a+
s2 = right.substring(cntOpen + 1, cntClose); // b+c
s3 = right.substring(cntClose + 1, right.length()); // *(d+e)/f

temp = t + tempCount;
threeLeft.add(temp);
threeRight.add(s2);
right = s1 + temp + s3;
// System.out.println("Right=" + right);
tempCount++;
}
}

public static void main(String args[]) {


ThreeAddressCode q = new ThreeAddressCode();
}
}

11

package ImiPackage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
// a=-(b+c)*m
// a=-(b*10)-(c/d)+g
// a=-88*cd/d
// a=a-(b+c)*(d+e)/f
// a=z+(c*(b+(c-l)))/f
// a=-b*(c+d)

/**
*
* @author OnlyImran
*/
public class Quadruples {

InputStreamReader ir = new InputStreamReader(System.in);


BufferedReader br = new BufferedReader(ir);
String splitted[];
String expr = "", bodmas = "()/*+-", left = "", right = "", temp = "", t = "T", operators = "", operands =
"", op = "=/*+-";
int tempCount = 1, cntOpen = 0, cntClose = 0, cntOperator = 0;

12

ArrayList<String> threeLeft = new ArrayList();


ArrayList<String> threeRight = new ArrayList();
ArrayList<Integer> openBrace = new ArrayList();
ArrayList<Integer> closeBrace = new ArrayList();
ArrayList<String> optimizedLeft = new ArrayList();
ArrayList<String> optimizedRight = new ArrayList();
boolean matched = false, gotOperator = false, nestedBrace = false, innerMatched = false;
String s1 = "", s2 = "", s3 = "", subS1 = "", subS2 = "", subS3 = "", arg1 = "", arg2 = "", opr = "";
String[][] quadruple;

public Quadruples() {
System.out.println("Enter grammar in following format:\nExpr=Expr");
try {
while ((expr = br.readLine()) != null && expr.length() != 0) {
splitted = expr.split("=");
left = splitted[0];
right = splitted[1];
}
} catch (IOException ex) {

}
expr = left + " = " + right;
for (int i = 0; i < expr.length(); i++) {
for (int j = 0; j < op.length(); j++) {
if ((op.charAt(j) + "").equals(expr.charAt(i) + "")) {
operators = operators + " " + expr.charAt(i);

13

}
}
if (expr.charAt(i) > 95 && expr.charAt(i) < 122) {
operands = operands + " " + expr.charAt(i);
}
}
System.out.println("Operands are: " + operands);
System.out.println("Operators are: " + operators + "\n");
checkOperator();
if (cntOperator > 1) {
if ("-".equals(right.charAt(0) + "") && ("(".equals(right.charAt(1) + ""))) {
chekUnaryParanthesis();
}
if ("-".equals(right.charAt(0) + "")) { //// -b*c or -13*c
checkUnary();
}
for (int i = 0; i < right.length(); i++) {
if ("(".equals(right.charAt(i) + "")) {
openBrace.add(i);
}
if (")".equals(right.charAt(i) + "")) {
closeBrace.add(i);
}
}
// checking for nested braces
if (openBrace.size() >= 1) {

14

if (openBrace.size() == 1) { // Because if size is exactly 1 then else if will throw error-->


openBrace.get(1);
removeBraces();
checkOperator();
} else if (openBrace.get(1) > closeBrace.get(0)) {
nestedBrace = false; // Not used anywhere, just for notification
removeBraces();
checkOperator();
} else {
nestedBrace = true; // Not used anywhere
removeNested();
checkOperator();
}
}

if (cntOperator > 1) {
while (cntOperator != 1) {
//System.out.println("cnt Operator is: " + cntOperator);
cntOperator--;
for (int i = 0; i < bodmas.length(); i++) {
for (int j = 0; j < right.length(); j++) {
if ((bodmas.charAt(i) + "").equals(right.charAt(j) + "")) {
//System.out.println("matched i=" + j);
matched = true;

// checking for inner match <-- z=b+a*10/20*d+e


for (int k = j - 1; k >= 0; k--) {

15

for (int l = 0; l < bodmas.length(); l++) {


if ((right.charAt(k) + "").equals(bodmas.charAt(l) + "")) {
//System.out.println("MATCHED INNER");
innerMatched = true;
subS1 = right.substring(k + 1, j); // 10
subS2 = right.charAt(j) + "";

// /

s1 = right.substring(0, k + 1); // b+a*


break;
} else if (k == 0) { // consider input: b*c+d
// System.out.println("Reached End start");
subS1 = right.substring(0, j);
subS2 = right.charAt(j) + "";
s1 = ""; //
break;
}
}
if (innerMatched) {
innerMatched = false;
break;
}
}
// checking for inner match --> z=b+a*10/20*d+e
for (int k = j + 1; k < right.length(); k++) {
for (int l = 0; l < bodmas.length(); l++) {
if ((right.charAt(k) + "").equals(bodmas.charAt(l) + "")) {
innerMatched = true;

16

subS3 = right.substring(j + 1, k); // 20


s3 = right.substring(k, right.length()); // *d+e

} else if (k == right.length() - 1) { // assume as input: b+c*d


subS3 = right.substring(j + 1, right.length()); // 20
s3 = "";
}
}
if (innerMatched) {
innerMatched = false;
break;
}
}
s2 = subS1 + subS2 + subS3;
temp = t + tempCount;
threeLeft.add(temp);
threeRight.add(s2);
right = s1 + temp + s3;
// System.out.println("Right=" + right);
tempCount++;

break;
}
}
if (matched) {
matched = false;

17

break;
}
}
}
checkOperator();
}
} else {
System.out.println("Please enter proper input:");
}
// System.out.println("Three Left" + threeLeft);
// System.out.println("Three right" + threeRight);
// Printing final output
System.out.println("Printing three address code of:" + expr);
for (int i = 0; i < threeLeft.size(); i++) {
System.out.println(threeLeft.get(i) + " = " + threeRight.get(i));
optimizedLeft.add(threeLeft.get(i));
optimizedRight.add(threeRight.get(i));

}
quadruple = new String[optimizedRight.size() + 1][5];
// filling quadruples

for (int i = 0; i < optimizedRight.size() + 1; i++) {


for (int j = 0; j < 5; j++) {
quadruple[i][j] = "";
if (i != 0 && j == 0) {

18

quadruple[i][j] = "(" + (i - 1) + ")";


} else if (i == 0 && j == 1) {
quadruple[i][j] = "OP";
} else if (i == 0 && j == 2) {
quadruple[i][j] = "ARG-1";
} else if (i == 0 && j == 3) {
quadruple[i][j] = "ARG-2";
} else if (i == 0 && j == 4) {
quadruple[i][j] = "RESULT";
}
}
}
for (int i = 1; i < optimizedRight.size() + 1; i++) {
String arg = optimizedRight.get(i - 1);
if ((arg.charAt(0) + "").equals("-")) {
arg = arg.substring(1, arg.length()) + arg.charAt(0);
}
String result = optimizedLeft.get(i - 1);
int x;
for (x = 0; x < arg.length(); x++) {
for (int y = 0; y < bodmas.length(); y++) {
if ((arg.charAt(x) + "").equals(bodmas.charAt(y) + "")) {
innerMatched = true;
break;
}
}

19

if (innerMatched) {
innerMatched = false;
break;
}
}
op = arg.charAt(x) + "";// b*10
arg1 = arg.substring(0, x);
arg2 = arg.substring(x + 1, arg.length());
//System.out.println("ARG1=" + arg1 + " OP=" + op + " ARG2=" + arg2);
for (int j = 0; j < 5; j++) {
if ((arg.charAt(arg.length() - 1) + "").equals("-")) {
quadruple[i][1] = "UMINUS";
}
if (j == 1) {
quadruple[i][j] = op;
} else if (j == 2) {
quadruple[i][j] = arg1;
} else if (j == 3) {
quadruple[i][j] = arg2;
} else if (j == 4) {
quadruple[i][j] = result;
}
}
}

// Printing Quatruple

20

for (int i = 0; i < optimizedRight.size() + 1; i++) {


for (int j = 0; j < 5; j++) {
System.out.print(quadruple[i][j] + "\t");
}
System.out.println("");
}
}

private void chekUnaryParanthesis() {


// - ( b * 1 0 ) + d
// 0 1 2 3 4 5 6 7 8

cntOpen = right.indexOf("(");
cntClose = right.indexOf(")");
s1 = right.charAt(0) + ""; // s2 = right.substring(cntOpen + 1, cntClose); // b*c
s3 = right.substring(cntClose + 1, right.length()); // +d
temp = t + tempCount; // T1
threeLeft.add(temp); // T1
threeRight.add(s2); // b*10
right = s1 + temp + s3; // -T1+d
// System.out.println("Right=" + right);
tempCount++;

21

void checkUnary() {
// - T 1 * d
// 0 1 2 3

for (int i = 1; i < right.length(); i++) {


for (int j = 0; j < bodmas.length(); j++) {
if (("" + right.charAt(i)).equals(bodmas.charAt(j) + "")) {
s1 = "";
s2 = right.substring(0, i); // -T1
s3 = right.substring(i, right.length()); // +d
temp = t + tempCount; // T3
threeLeft.add(temp); // T3
threeRight.add(s2); // -T1
right = temp + s3; // T3+d
//System.out.println("Right=" + right);
tempCount++;
checkOperator();
matched = true;
}
}
if (matched) {
matched = false;
break;
}
}
}

22

void checkOperator() {
cntOperator = 0;
for (int i = 0; i < right.length(); i++) {
for (int j = 0; j < bodmas.length(); j++) { // open ( and close ) braces are decreased to 0 coz of
cntClose-if (("" + right.charAt(i)).equals(bodmas.charAt(j) + "")) {
cntOperator++;
}
}
}
// System.out.println("Operator count=" + cntOperator);
if (cntOperator == 1 && "-".equals("" + right.charAt(0))) { // a=-b

System.out.println("Single input: ");


threeLeft.add("T1");
threeRight.add(right);
threeLeft.add(left);
threeRight.add("T1");

//threeRight.add(right);

} else if (cntOperator == 1) {
threeLeft.add(left);
threeRight.add(right);

}
}

23

void removeNested() {
// a + ( c * ( b + c ) ) / f
// 0 1 2 3 4 5 6 7 8 9 10 11 12
for (int i = 0; i < openBrace.size(); i++) {
cntOpen = right.lastIndexOf("("); // 5
cntClose = right.indexOf(")");// 9
s1 = right.substring(0, cntOpen); // a+(c*
s2 = right.substring(cntOpen + 1, cntClose); // b+c
s3 = right.substring(cntClose + 1, right.length()); // )/f

temp = t + tempCount;
threeLeft.add(temp);
threeRight.add(s2);
right = s1 + temp + s3;
// System.out.println("Right=" + right);
tempCount++;
}
}

void removeBraces() {
// a + ( b + c ) * ( d + e ) / f
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
// System.out.println(openBrace.size());
for (int i = 0; i < openBrace.size(); i++) {
cntOpen = right.indexOf("("); // 2
cntClose = right.indexOf(")"); // 6

24

s1 = right.substring(0, cntOpen); // a+
s2 = right.substring(cntOpen + 1, cntClose); // b+c
s3 = right.substring(cntClose + 1, right.length()); // *(d+e)/f

temp = t + tempCount;
threeLeft.add(temp);
threeRight.add(s2);
right = s1 + temp + s3;
// System.out.println("Right=" + right);
tempCount++;
}
}

public static void main(String args[]) {


Quadruples q = new Quadruples();
}
}

package ImiPackage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

25

public class Triple {

InputStreamReader ir = new InputStreamReader(System.in);


BufferedReader br = new BufferedReader(ir);
String splitted[];
String expr = "", bodmas = "()/*+-", left = "", right = "", temp = "", t = "T", operators = "", operands =
"", op = "=/*+-";
int tempCount = 1, cntOpen = 0, cntClose = 0, cntOperator = 0;
ArrayList<String> threeLeft = new ArrayList();
ArrayList<String> threeRight = new ArrayList();
ArrayList<Integer> openBrace = new ArrayList();
ArrayList<Integer> closeBrace = new ArrayList();
ArrayList<String> optimizedLeft = new ArrayList();
ArrayList<String> optimizedRight = new ArrayList();
boolean matched = false, gotOperator = false, nestedBrace = false, innerMatched = false;
String s1 = "", s2 = "", s3 = "", subS1 = "", subS2 = "", subS3 = "", arg1 = "", arg2 = "", opr = "";
String[][] triple;

public Triple() {
System.out.println("Enter grammar in following format:\nExpr=Expr");
try {
while ((expr = br.readLine()) != null && expr.length() != 0) {
splitted = expr.split("=");
left = splitted[0];
right = splitted[1];
}
} catch (IOException ex) {

26

}
expr = left + " = " + right;
for (int i = 0; i < expr.length(); i++) {
for (int j = 0; j < op.length(); j++) {
if ((op.charAt(j) + "").equals(expr.charAt(i) + "")) {
operators = operators + " " + expr.charAt(i);
}
}
if (expr.charAt(i) > 95 && expr.charAt(i) < 122) {
operands = operands + " " + expr.charAt(i);
}
}
System.out.println("Operands are: " + operands);
System.out.println("Operators are: " + operators + "\n");
checkOperator();
if (cntOperator > 1) {
if ("-".equals(right.charAt(0) + "") && ("(".equals(right.charAt(1) + ""))) {
chekUnaryParanthesis();
}
if ("-".equals(right.charAt(0) + "")) { //// -b*c or -13*c
checkUnary();
}
for (int i = 0; i < right.length(); i++) {
if ("(".equals(right.charAt(i) + "")) {
openBrace.add(i);

27

}
if (")".equals(right.charAt(i) + "")) {
closeBrace.add(i);
}
}
if (openBrace.size() >= 1) {
if (openBrace.size() == 1) { // Because if size is exactly 1 then else if will throw error-->
openBrace.get(1);
removeBraces();
checkOperator();
} else if (openBrace.get(1) > closeBrace.get(0)) {
nestedBrace = false; // Not used anywhere, just for notification
removeBraces();
checkOperator();
} else {
nestedBrace = true; // Not used anywhere
removeNested();
checkOperator();
}
}

if (cntOperator > 1) {
while (cntOperator != 1) {
cntOperator--;
for (int i = 0; i < bodmas.length(); i++) {
for (int j = 0; j < right.length(); j++) {
if ((bodmas.charAt(i) + "").equals(right.charAt(j) + "")) {

28

//System.out.println("matched i=" + j);


matched = true;

// checking for inner match <-- z=b+a*10/20*d+e


for (int k = j - 1; k >= 0; k--) {
for (int l = 0; l < bodmas.length(); l++) {
if ((right.charAt(k) + "").equals(bodmas.charAt(l) + "")) {
innerMatched = true;
subS1 = right.substring(k + 1, j); // 10
subS2 = right.charAt(j) + "";

// /

s1 = right.substring(0, k + 1); // b+a*


break;
} else if (k == 0) { // consider input: b*c+d
subS1 = right.substring(0, j);
subS2 = right.charAt(j) + "";
s1 = ""; //
break;
}
}
if (innerMatched) {
innerMatched = false;
break;
}
}
// checking for inner match --> z=b+a*10/20*d+e
for (int k = j + 1; k < right.length(); k++) {

29

for (int l = 0; l < bodmas.length(); l++) {


if ((right.charAt(k) + "").equals(bodmas.charAt(l) + "")) {
innerMatched = true;
subS3 = right.substring(j + 1, k); // 20
s3 = right.substring(k, right.length()); // *d+e

} else if (k == right.length() - 1) { // assume as input: b+c*d


subS3 = right.substring(j + 1, right.length()); // 20
s3 = "";
}
}
if (innerMatched) {
innerMatched = false;
break;
}
}
s2 = subS1 + subS2 + subS3;
temp = t + tempCount;
threeLeft.add(temp);
threeRight.add(s2);
right = s1 + temp + s3;
// System.out.println("Right=" + right);
tempCount++;

break;
}

30

}
if (matched) {
matched = false;
break;
}
}
}
checkOperator();
}
} else {
System.out.println("Please enter proper input:");
}

// Printing final output


System.out.println("Printing three address code of:" + expr);
for (int i = 0; i < threeLeft.size(); i++) {
System.out.println(threeLeft.get(i) + " = " + threeRight.get(i));
optimizedLeft.add(threeLeft.get(i));
optimizedRight.add(threeRight.get(i));

}
triple = new String[optimizedRight.size() + 1][5];
// filling triples

for (int i = 0; i < optimizedRight.size() + 1; i++) {


for (int j = 0; j < 5; j++) {

31

triple[i][j] = "-";
if (i != 0 && j == 0) {
triple[i][j] = "(" + (i - 1) + ")";
} else if (i == 0 && j == 1) {
triple[i][j] = "OP";
} else if (i == 0 && j == 2) {
triple[i][j] = "ARG-1";
} else if (i == 0 && j == 3) {
triple[i][j] = "ARG-2";
} else if (i == 0 && j == 4) {
triple[i][j] = "RESULT";
}
}
}
for (int i = 1; i < optimizedRight.size() + 1; i++) {
String arg = optimizedRight.get(i - 1);
if ((arg.charAt(0) + "").equals("-")) {
arg = arg.substring(1, arg.length()) + arg.charAt(0);
}
String result = optimizedLeft.get(i - 1);
int x;
for (x = 0; x < arg.length(); x++) {
for (int y = 0; y < bodmas.length(); y++) {
if ((arg.charAt(x) + "").equals(bodmas.charAt(y) + "")) {
innerMatched = true;
break;

32

}
}
if (innerMatched) {
innerMatched = false;
break;
}
}
op = arg.charAt(x) + "";
arg1 = arg.substring(0, x);
arg2 = arg.substring(x + 1, arg.length());
//System.out.println("ARG1=" + arg1 + " OP=" + op + " ARG2=" + arg2);
for (int j = 0; j < 5; j++) {
if ((arg.charAt(arg.length() - 1) + "").equals("-")) {
triple[i][1] = "UMINUS";
}
if (j == 1) {
triple[i][j] = op;
} else if (j == 2) {
triple[i][j] = arg1;
for(int k=1;k<optimizedRight.size();k++){
if(arg1.equals(triple[k][4])){
triple[i][j]=triple[k][0];
}
}
} else if (j == 3) {
triple[i][j] = arg2;

33

for(int k=1;k<optimizedRight.size();k++){
if(arg2.equals(triple[k][4])){
triple[i][j]=triple[k][0];
}
}
} else if (j == 4) {
triple[i][j] = result;
}
}
}

// Printing Triple
for (int i = 0; i < optimizedRight.size() + 1; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(triple[i][j] + "\t");
}
System.out.println("");
}
}

private void chekUnaryParanthesis() {


// - ( b * 1 0 ) + d
// 0 1 2 3 4 5 6 7 8

cntOpen = right.indexOf("(");

34

cntClose = right.indexOf(")");
s1 = right.charAt(0) + ""; // s2 = right.substring(cntOpen + 1, cntClose); // b*c
s3 = right.substring(cntClose + 1, right.length()); // +d
temp = t + tempCount; // T1
threeLeft.add(temp); // T1
threeRight.add(s2); // b*10
right = s1 + temp + s3; // -T1+d
// System.out.println("Right=" + right);
tempCount++;

private void checkUnary() {


// - T 1 * d
// 0 1 2 3

for (int i = 1; i < right.length(); i++) {


for (int j = 0; j < bodmas.length(); j++) {
if (("" + right.charAt(i)).equals(bodmas.charAt(j) + "")) {
s1 = "";
s2 = right.substring(0, i); // -T1
s3 = right.substring(i, right.length()); // +d
temp = t + tempCount; // T3
threeLeft.add(temp); // T3
threeRight.add(s2); // -T1

35

right = temp + s3; // T3+d


//System.out.println("Right=" + right);
tempCount++;
checkOperator();
matched = true;
}
}
if (matched) {
matched = false;
break;
}
}
}

private void checkOperator() {


cntOperator = 0;
for (int i = 0; i < right.length(); i++) {
for (int j = 0; j < bodmas.length(); j++) { // open ( and close ) braces are decreased to 0 coz of
cntClose-if (("" + right.charAt(i)).equals(bodmas.charAt(j) + "")) {
cntOperator++;
}
}
}
// System.out.println("Operator count=" + cntOperator);
if (cntOperator == 1 && "-".equals("" + right.charAt(0))) { // a=-b

36

System.out.println("Single input: ");


threeLeft.add("T1");
threeRight.add(right);
threeLeft.add(left);
threeRight.add("T1");

//threeRight.add(right);

} else if (cntOperator == 1) {
threeLeft.add(left);
threeRight.add(right);

}
}

private void removeNested() {


// a + ( c * ( b + c ) ) / f
// 0 1 2 3 4 5 6 7 8 9 10 11 12
for (int i = 0; i < openBrace.size(); i++) {
cntOpen = right.lastIndexOf("("); // 5
cntClose = right.indexOf(")");// 9
s1 = right.substring(0, cntOpen); // a+(c*
s2 = right.substring(cntOpen + 1, cntClose); // b+c
s3 = right.substring(cntClose + 1, right.length()); // )/f

temp = t + tempCount;
threeLeft.add(temp);
threeRight.add(s2);

37

right = s1 + temp + s3;


// System.out.println("Right=" + right);
tempCount++;
}
}

private void removeBraces() {


// a + ( b + c ) * ( d + e ) / f
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
// System.out.println(openBrace.size());
for (int i = 0; i < openBrace.size(); i++) {
cntOpen = right.indexOf("("); // 2
cntClose = right.indexOf(")"); // 6
s1 = right.substring(0, cntOpen); // a+
s2 = right.substring(cntOpen + 1, cntClose); // b+c
s3 = right.substring(cntClose + 1, right.length()); // *(d+e)/f

temp = t + tempCount;
threeLeft.add(temp);
threeRight.add(s2);
right = s1 + temp + s3;
// System.out.println("Right=" + right);
tempCount++;
}
}

38

public static void main(String args[]) {


Triple triple = new Triple();
}
}

package ImiPackage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
// a=-(b+c)*m
// a=-(b*10)-(c/d)+g
// a=-88*cd/d
// a=a-(b+c)*(d+e)/f
// a=z+(c*(b+(c-l)))/f
// a=-b*(c+d)

/**
*
* @author OnlyImran
*/
public class IndirectTriple {

InputStreamReader ir = new InputStreamReader(System.in);

39

BufferedReader br = new BufferedReader(ir);


String splitted[];
String expr = "", bodmas = "()/*+-", left = "", right = "", temp = "", t = "T", operators = "", operands =
"", op = "=/*+-";
int tempCount = 1, cntOpen = 0, cntClose = 0, cntOperator = 0;
ArrayList<String> threeLeft = new ArrayList();
ArrayList<String> threeRight = new ArrayList();
ArrayList<Integer> openBrace = new ArrayList();
ArrayList<Integer> closeBrace = new ArrayList();
ArrayList<String> optimizedLeft = new ArrayList();
ArrayList<String> optimizedRight = new ArrayList();
boolean matched = false, gotOperator = false, nestedBrace = false, innerMatched = false;
String s1 = "", s2 = "", s3 = "", subS1 = "", subS2 = "", subS3 = "", arg1 = "", arg2 = "", opr = "";
String[][] indirectTriple,statement;

public IndirectTriple() {
System.out.println("Enter grammar in following format:\nExpr=Expr");
try {
while ((expr = br.readLine()) != null && expr.length() != 0) {
splitted = expr.split("=");
left = splitted[0];
right = splitted[1];
}
} catch (IOException ex) {

}
expr = left + " = " + right;

40

for (int i = 0; i < expr.length(); i++) {


for (int j = 0; j < op.length(); j++) {
if ((op.charAt(j) + "").equals(expr.charAt(i) + "")) {
operators = operators + " " + expr.charAt(i);
}
}
if (expr.charAt(i) > 95 && expr.charAt(i) < 122) {
operands = operands + " " + expr.charAt(i);
}
}
System.out.println("Operands are: " + operands);
System.out.println("Operators are: " + operators + "\n");
checkOperator();
if (cntOperator > 1) {
if ("-".equals(right.charAt(0) + "") && ("(".equals(right.charAt(1) + ""))) {
chekUnaryParanthesis();
}
if ("-".equals(right.charAt(0) + "")) { //// -b*c or -13*c
checkUnary();
}
for (int i = 0; i < right.length(); i++) {
if ("(".equals(right.charAt(i) + "")) {
openBrace.add(i);
}
if (")".equals(right.charAt(i) + "")) {
closeBrace.add(i);

41

}
}
// checking for nested braces
if (openBrace.size() >= 1) {
if (openBrace.size() == 1) { // Because if size is exactly 1 then else if will throw error-->
openBrace.get(1);
removeBraces();
checkOperator();
} else if (openBrace.get(1) > closeBrace.get(0)) {
nestedBrace = false; // Not used anywhere, just for notification
removeBraces();
checkOperator();
} else {
nestedBrace = true; // Not used anywhere
removeNested();
checkOperator();
}
}

if (cntOperator > 1) {
while (cntOperator != 1) {
//System.out.println("cnt Operator is: " + cntOperator);
cntOperator--;
for (int i = 0; i < bodmas.length(); i++) {
for (int j = 0; j < right.length(); j++) {
if ((bodmas.charAt(i) + "").equals(right.charAt(j) + "")) {
//System.out.println("matched i=" + j);

42

matched = true;

// checking for inner match <-- z=b+a*10/20*d+e


for (int k = j - 1; k >= 0; k--) {
for (int l = 0; l < bodmas.length(); l++) {
if ((right.charAt(k) + "").equals(bodmas.charAt(l) + "")) {
//System.out.println("MATCHED INNER");
innerMatched = true;
subS1 = right.substring(k + 1, j); // 10
subS2 = right.charAt(j) + "";

// /

s1 = right.substring(0, k + 1); // b+a*


break;
} else if (k == 0) { // consider input: b*c+d
// System.out.println("Reached End start");
subS1 = right.substring(0, j);
subS2 = right.charAt(j) + "";
s1 = ""; //
break;
}
}
if (innerMatched) {
innerMatched = false;
break;
}
}
// checking for inner match --> z=b+a*10/20*d+e

43

for (int k = j + 1; k < right.length(); k++) {


for (int l = 0; l < bodmas.length(); l++) {
if ((right.charAt(k) + "").equals(bodmas.charAt(l) + "")) {
innerMatched = true;
subS3 = right.substring(j + 1, k); // 20
s3 = right.substring(k, right.length()); // *d+e

} else if (k == right.length() - 1) { // assume as input: b+c*d


subS3 = right.substring(j + 1, right.length()); // 20
s3 = "";
}
}
if (innerMatched) {
innerMatched = false;
break;
}
}
s2 = subS1 + subS2 + subS3;
temp = t + tempCount;
threeLeft.add(temp);
threeRight.add(s2);
right = s1 + temp + s3;
// System.out.println("Right=" + right);
tempCount++;

break;

44

}
}
if (matched) {
matched = false;
break;
}
}
}
checkOperator();
}
} else {
System.out.println("Please enter proper input:");
}

// Printing final output


System.out.println("Printing three address code of:" + expr);
for (int i = 0; i < threeLeft.size(); i++) {
System.out.println(threeLeft.get(i) + " = " + threeRight.get(i));
optimizedLeft.add(threeLeft.get(i));
optimizedRight.add(threeRight.get(i));

}
indirectTriple = new String[optimizedRight.size() + 1][5];

// filling indirectTriples

45

for (int i = 0; i < optimizedRight.size() + 1; i++) {


for (int j = 0; j < 5; j++) {
indirectTriple[i][j] = "-";
if (i != 0 && j == 0) {
indirectTriple[i][j] = "(" + (i +13) + ")";
} else if (i == 0 && j == 1) {
indirectTriple[i][j] = "OP";
} else if (i == 0 && j == 2) {
indirectTriple[i][j] = "ARG-1";
} else if (i == 0 && j == 3) {
indirectTriple[i][j] = "ARG-2";
} else if (i == 0 && j == 4) {
indirectTriple[i][j] = "RESULT";
}
}
}
for (int i = 1; i < optimizedRight.size() + 1; i++) {
String arg = optimizedRight.get(i - 1);
if ((arg.charAt(0) + "").equals("-")) {
arg = arg.substring(1, arg.length()) + arg.charAt(0);
}
String result = optimizedLeft.get(i - 1);
int x;
for (x = 0; x < arg.length(); x++) {
for (int y = 0; y < bodmas.length(); y++) {
if ((arg.charAt(x) + "").equals(bodmas.charAt(y) + "")) {

46

innerMatched = true;
break;
}
}
if (innerMatched) {
innerMatched = false;
break;
}
}
op = arg.charAt(x) + "";
arg1 = arg.substring(0, x);
arg2 = arg.substring(x + 1, arg.length());
//System.out.println("ARG1=" + arg1 + " OP=" + op + " ARG2=" + arg2);
for (int j = 0; j < 5; j++) {
if ((arg.charAt(arg.length() - 1) + "").equals("-")) {
indirectTriple[i][1] = "UMINUS";
}
if (j == 1) {
indirectTriple[i][j] = op;
} else if (j == 2) {
indirectTriple[i][j] = arg1;
for(int k=1;k<optimizedRight.size();k++){
if(arg1.equals(indirectTriple[k][4])){ //
indirectTriple[i][j]=indirectTriple[k][0];
}
}

47

} else if (j == 3) {
indirectTriple[i][j] = arg2;
for(int k=1;k<optimizedRight.size();k++){
if(arg2.equals(indirectTriple[k][4])){
indirectTriple[i][j]=indirectTriple[k][0];
}
}
} else if (j == 4) {
indirectTriple[i][j] = result;
}
}
}

// Printing Triple
System.out.println("Printing Indirect triple table\n");
for (int i = 0; i < optimizedRight.size() + 1; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(indirectTriple[i][j] + "\t");
}
System.out.println("");
}

statement = new String[optimizedRight.size()+1][2];

// filling statement

48

for(int i=0;i<optimizedRight.size()+1;i++){
for(int j=0;j<2;j++){
if(i==0){
if(j==0)
statement[i][j]=" ";
else if(j==1){
statement[i][j]="STATEMENT" ;
}
}
else if(i!=0 && j==0){
statement[i][j]="("+(i-1)+")";
}
else if(i!=0 && j!=0){
statement[i][j]="("+(i+13)+")";
}
}
}
System.out.println("Printing reference table\n");
for(int i=0;i<optimizedRight.size()+1;i++){
for(int j=0;j<2;j++){
System.out.print(statement[i][j]+"\t");
}
System.out.println("");
}
System.out.println("");

49

private void chekUnaryParanthesis() {


// - ( b * 1 0 ) + d
// 0 1 2 3 4 5 6 7 8

cntOpen = right.indexOf("(");
cntClose = right.indexOf(")");
s1 = right.charAt(0) + ""; // s2 = right.substring(cntOpen + 1, cntClose); // b*c
s3 = right.substring(cntClose + 1, right.length()); // +d
temp = t + tempCount; // T1
threeLeft.add(temp); // T1
threeRight.add(s2); // b*10
right = s1 + temp + s3; // -T1+d
// System.out.println("Right=" + right);
tempCount++;

private void checkUnary() {


// - T 1 * d
// 0 1 2 3

for (int i = 1; i < right.length(); i++) {


for (int j = 0; j < bodmas.length(); j++) {

50

if (("" + right.charAt(i)).equals(bodmas.charAt(j) + "")) {


s1 = "";
s2 = right.substring(0, i); // -T1
s3 = right.substring(i, right.length()); // +d
temp = t + tempCount; // T3
threeLeft.add(temp); // T3
threeRight.add(s2); // -T1
right = temp + s3; // T3+d
//System.out.println("Right=" + right);
tempCount++;
checkOperator();
matched = true;
}
}
if (matched) {
matched = false;
break;
}
}
}

private void checkOperator() {


cntOperator = 0;
for (int i = 0; i < right.length(); i++) {
for (int j = 0; j < bodmas.length(); j++) { // open ( and close ) braces are decreased to 0 coz of
cntClose-if (("" + right.charAt(i)).equals(bodmas.charAt(j) + "")) {

51

cntOperator++;
}
}
}
// System.out.println("Operator count=" + cntOperator);
if (cntOperator == 1 && "-".equals("" + right.charAt(0))) { // a=-b

System.out.println("Single input: ");


threeLeft.add("T1");
threeRight.add(right);
threeLeft.add(left);
threeRight.add("T1");

//threeRight.add(right);

} else if (cntOperator == 1) {
threeLeft.add(left);
threeRight.add(right);

}
}

private void removeNested() {


// a + ( c * ( b + c ) ) / f
// 0 1 2 3 4 5 6 7 8 9 10 11 12
for (int i = 0; i < openBrace.size(); i++) {
cntOpen = right.lastIndexOf("("); // 5
cntClose = right.indexOf(")");// 9

52

s1 = right.substring(0, cntOpen); // a+(c*


s2 = right.substring(cntOpen + 1, cntClose); // b+c
s3 = right.substring(cntClose + 1, right.length()); // )/f

temp = t + tempCount;
threeLeft.add(temp);
threeRight.add(s2);
right = s1 + temp + s3;
// System.out.println("Right=" + right);
tempCount++;
}
}

private void removeBraces() {


// a + ( b + c ) * ( d + e ) / f
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
// System.out.println(openBrace.size());
for (int i = 0; i < openBrace.size(); i++) {
cntOpen = right.indexOf("("); // 2
cntClose = right.indexOf(")"); // 6
s1 = right.substring(0, cntOpen); // a+
s2 = right.substring(cntOpen + 1, cntClose); // b+c
s3 = right.substring(cntClose + 1, right.length()); // *(d+e)/f

temp = t + tempCount;
threeLeft.add(temp);

53

threeRight.add(s2);
right = s1 + temp + s3;
// System.out.println("Right=" + right);
tempCount++;
}
}

public static void main(String args[]) {


IndirectTriple indirectTriple = new IndirectTriple();
}
}

// b*a*c/20+d
// a/b+c*d/e
// a+b*c-d/e-f
// a*(b+c)
// (a+b)*c-e/(d+f)
// (a*(b+c))/d
// Stack the high priority operator and remove the lower prioritize operator
package ImiPackage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

54

import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @author Imran
*/
public class InfixToPostfix {

InputStreamReader ir = new InputStreamReader(System.in);


BufferedReader br = new BufferedReader(ir);
String splitted[];
boolean matched = false, gotOperator = false, nestedBrace = false, innerMatched = false;
String postFix = "", expr = "";
String precedence = "^/*+-";
//01234
ArrayList<String> stack = new ArrayList<>();
int cntOperator = 0;

public InfixToPostfix() {
System.out.println("Enter grammar :");
try {
expr = br.readLine();

} catch (IOException ex) {


Logger.getLogger(ThreeAddressCode.class.getName()).log(Level.SEVERE, null, ex);
}

55

System.out.println("Expr " + expr);


for (int i = 0; i < expr.length(); i++) {
String ch = expr.charAt(i) + "";
if ((expr.charAt(i) >= 97 && expr.charAt(i) <= 122) || (expr.charAt(i) >= 48 && expr.charAt(i) <=
57)) {
postFix = postFix + expr.charAt(i);

} else if (ch.equals("(") || ch.equals(")") || ch.equals("/") || ch.equals("*") || ch.equals("+") ||


ch.equals("-") || ch.equals("^"))
{

String stackElement = "";


int stackLast = stack.size() - 1;

if (!stack.isEmpty() && !ch.equals("(") && !ch.equals(")")) {


stackElement = stack.get(stackLast);
int idx1 = precedence.indexOf(ch);
int idx2 = precedence.indexOf(stackElement);
// System.out.println("comparing " + ch + " with " + stackElement);

if (idx1 < idx2 && !"(".equals(stackElement)) {


//System.out.println(ch + " has higher precedence than " + stackElement);
stack.add(ch);
System.out.println(postFix + "\t\t" + stack);
} else if (idx1 > idx2 && !"(".equals(stackElement)) {
//System.out.println(ch + " has lower precedence than " + stackElement);
for (int j = stackLast; j >= 0; j--) {

56

int stackIdx = precedence.indexOf(stack.get(j));


if (idx1 > stackIdx) {
postFix = postFix + stack.get(j);
stack.remove(j);
System.out.println(postFix + "\t\t" + stack);
stack.add(ch);
System.out.println(postFix + "\t\t" + stack);
if (j != stackLast) {
stack.remove(j); // removing same type operator
System.out.println(postFix + "\t\t" + stack);
}

}
}

} else if (!"(".equals(stackElement)) {
//System.out.println(ch + " has equal precedence " + stackElement);
for (int j = stackLast; j >= 0; j--) {
int stackIdx = precedence.indexOf(stack.get(j));
if (idx1 == stackIdx) {
postFix = postFix + stack.get(j);
stack.remove(j);
System.out.println(postFix + "\t\t" + stack);
if (j != stackLast) {
stack.remove(j);
System.out.println(postFix + "\t\t" + stack);

57

}
stack.add(ch);
System.out.println(postFix + "\t\t" + stack);
}
}
} else if ("(".equals(stackElement)) {
stack.add(ch);
System.out.println(postFix + "\t\t" + stack);

} else if (ch.equals("(")) {
stack.add("(");
System.out.println(postFix + "\t\t" + stack);
} else if (ch.equals(")")) {
postFix = postFix + stack.get(stack.size() - 1);
stack.remove(stack.size() - 1);
stack.remove(stack.size() - 1);
System.out.println(postFix + "\t\t" + stack);
}
if (stack.isEmpty() && !ch.equals(")")) { //Solve the problem for ) in-> (300+23)*(43-21)/
(84+7)
stack.add(ch);
System.out.println(postFix + "\t\t" + stack);
}

58

}
// System.out.println("Stack-> " + stack);
int stackLast = stack.size() - 1;
int postLen = postFix.length();
if (stack.size() == 1) {
postFix = postFix + stack.get(stackLast);
//stack.clear();
System.out.println(postFix + "\t\t" + stack);
} else if (stack.get(stackLast).equals(stack.get(stackLast - 1))) {
postFix = postFix.substring(0, postLen - 1) + stack.get(stackLast) + postFix.substring(postLen - 1)
+ stack.get(stackLast);
//stack.clear();
System.out.println(postFix + "\t\t" + stack);
} else {
for (int i = stackLast; i >= 0; i--) {
postFix = postFix + stack.get(i);
//stack.clear();
System.out.println(postFix + "\t\t" + stack);
}
}
System.out.println("Post Fix is: " + postFix);
}

public static void main(String[] args) {


InfixToPostfix p = new InfixToPostfix();
}

59

package ImiPackage;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

// abc-+de-fg-h+/*
// abc+*d/
/**
*
* @author Imran
*/

public class PostfixToInfix {


InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
String infix = "",expr="",op="/*+-";
ArrayList<String> stack = new ArrayList<>();

public PostfixToInfix() {
System.out.println("Enter grammar :");

60

try {
expr = br.readLine();

} catch (IOException ex) {


Logger.getLogger(ThreeAddressCode.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Expr " + expr);
for (int i = 0; i < expr.length(); i++) {
String ch = expr.charAt(i) + "";
if ((expr.charAt(i) >= 97 && expr.charAt(i) <= 122)) {
stack.add(ch);
System.out.println(stack);
}
else if(ch.equals("/") || ch.equals("*") || ch.equals("+") || ch.equals("-")){
String lastElement=stack.get(stack.size()-1);
String secondLastElement=stack.get(stack.size()-2);
stack.remove(stack.size()-1);
stack.remove(stack.size()-1);

if(ch.equals("*") || ch.equals("/")|| ch.equals("-")|| ch.equals("+")){


if(getCharCount(secondLastElement)>1){
secondLastElement = "("+secondLastElement+")";
}

if(getCharCount(lastElement)>1){
lastElement = "("+lastElement+")";

61

}
String newElement = secondLastElement+ch+lastElement;
stack.add(newElement);
System.out.println(stack);
}

}
System.out.println("Converted Infix is: "+stack.get(0));

}
private int getCharCount(String str){
int count=str.length();
return count;
}
public static void main(String args[]){
PostfixToInfix pti = new PostfixToInfix();
}

package ImiPackage;

62

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
// 13+5* ans=20
// 246+*5/ ans=4
/**
*
* @author Imran
*/

public class EvaluationOfPostfix {


InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
String postFix = "",expr="";
ArrayList<String> stack = new ArrayList<>();

public EvaluationOfPostfix() {
System.out.println("Enter grammar :");
try {
expr = br.readLine();

} catch (IOException ex) {


Logger.getLogger(ThreeAddressCode.class.getName()).log(Level.SEVERE, null, ex);

63

}
System.out.println("Expr " + expr);
for (int i = 0; i < expr.length(); i++) {
String ch = expr.charAt(i) + "";
if ((expr.charAt(i) >= 97 && expr.charAt(i) <= 122) || (expr.charAt(i) >= 48 && expr.charAt(i) <=
57)) {
stack.add(ch);
System.out.println(stack);
}
else if(ch.equals("/") || ch.equals("*") || ch.equals("+") || ch.equals("-")){
int result=0;
int int2=Integer.parseInt(stack.get(stack.size()-1));
stack.remove(stack.size()-1);
System.out.println(stack);
int int1 = Integer.parseInt(stack.get(stack.size()-1));
stack.remove(stack.size()-1);
System.out.println(stack);
if(ch.equals("+")){
result = int1+int2;
}
else if(ch.equals("*")){
result = int1*int2;
}
else if(ch.equals("/")){
result = int1/int2;
}
else if(ch.equals("-")){

64

result = int1-int2;
}
stack.add(result+"");
System.out.println(stack);
}

}
System.out.println("Evaluated value is: \t"+stack.get(0));
}
public static void main(String args[]){
EvaluationOfPostfix eop = new EvaluationOfPostfix();
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package ImiPackage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

65

import java.util.ArrayList;

/**
*
* @author Imran
*/
public class LoopDetection {
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);
String input="";
ArrayList <String> al = new ArrayList();
ArrayList <String> be = new ArrayList();
ArrayList <Integer> beIdx = new ArrayList();
String splitted[];
boolean init_node=true;
public LoopDetection() throws IOException {
System.out.println("Enter the flow of nodes in a-b format and enter \"done\" at the end:");
while(true){

input = br.readLine();
if(input.equalsIgnoreCase("done")){
break;
}
else
{
splitted = input.split("-");

66

if(init_node){
al.add(splitted[0]);
init_node=false;
}
al.add(splitted[1]);
}
}
System.out.println("Enter Back edges in b-a format and enter \"done\" at the end:");
while(true){
input = br.readLine();
if(
input.equalsIgnoreCase("done")){
break;
}
else
{
splitted=input.split("-");
be.add(splitted[0]);
be.add(splitted[1]);
}
}

System.out.println("Flow of graph is : "+al);


System.out.println("Back edges are : "+be+"\n");

for(int i=0;i<be.size();i++){

67

int count=0,flag=0;
String b=be.get(i);
for(int j=0;j<al.size();j++){
String flow = al.get(j);
if(b.equals(flow)){
count++;

}
}
beIdx.add(count);
}
System.out.println("beIdx : "+beIdx);
for(int i=0;i<beIdx.size();i=i+2){
if(beIdx.get(i)==1 && beIdx.get(i+1)==1){
System.out.println("Loop exist between: "+be.get(i)+" and "+be.get(i+1));
}
else{
System.out.println("Loop does not exist between: "+be.get(i)+" and "+be.get(i+1));
}
}
}
public static void main(String args[]) throws IOException{
LoopDetection ld = new LoopDetection();
}
}

68

package ImiPackage;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
t1=c*d
t2=e/f
t3=t1/t2
t4=a+b
t5=t1+t4
z=t5
done

Result =
MOV c,R0
MUL d,R0
MOV e,R1
DIV f,R1
DIV R1,R0
MOV a,R1
ADD b,R1
ADD R1,R0
STORE

69

----------------------------t1=a+b
t2=c*d
t3=t1-t2
done

Result =
MOV a,R0
ADD b,R0
MOV c,R1
MUL d,R1
SUB R1,R0

t1=a+b
t2=c*d
t3=t1-k
done
*/

public class CodeSequence {

String input;
InputStreamReader is = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(is);
ArrayList<String> left = new ArrayList<>();
ArrayList<String> right = new ArrayList<>();

70

String splitted[];
String result = "";
int flag = 0;

public CodeSequence() throws IOException {


System.out.println("Enter the three address code in a=b OP c format: and done at the end.");
while (true) {
input = br.readLine();

if (input.equalsIgnoreCase("done")) {
break;
} else {
splitted = input.split("=");
left.add(splitted[0]);
right.add(splitted[1]);
}
}
int rConut = 0; // Used to swtich registers.
System.out.println("Left " + left);
System.out.println("right " + right);
int opIdx = 0;
for (int i = 0; i < right.size(); i++) { // *************************************
flag = 0;
rConut = i % 2;
String args = right.get(i);
String tempStr = "";

71

for (int j = 0; j < left.size() - 1; j++) { // ********************************


tempStr = left.get(j);
//System.out.println("Finding "+tempStr+" in "+args);
if (!args.contains(tempStr)) {
} else if (args.contains(tempStr)) {
flag++;
System.out.println("Yes Contain");
}
}

if (flag == 0) { // ********************************************************
// System.out.println("Not found going inside to find MOV");
if (args.contains("-")) {
splitted = args.split("-");
String arg1 = splitted[0];
String arg2 = splitted[1];
result = result + "MOV " + arg1 + "," + "R" + rConut + "\n";
result = result + "SUB " + arg2 + ",R" + rConut + "\n";
} else if (args.contains("*")) {
splitted = args.split("\\*");
String arg1 = splitted[0];
String arg2 = splitted[1];
result = result + "MOV " + arg1 + "," + "R" + rConut + "\n";
result = result + "MUL " + arg2 + ",R" + rConut + "\n";
} else if (args.contains("/")) {
splitted = args.split("\\/");

72

String arg1 = splitted[0];


String arg2 = splitted[1];
result = result + "MOV " + arg1 + "," + "R" + rConut + "\n";
result = result + "DIV " + arg2 + ",R" + rConut + "\n";
} else if (args.contains("+")) {
splitted = args.split("\\+");
String arg1 = splitted[0];
String arg2 = splitted[1];
result = result + "MOV " + arg1 + "," + "R" + rConut + "\n";
result = result + "ADD " + arg2 + ",R" + rConut + "\n";
}
} else if (flag > 0) {
if (args.contains("-")) {
splitted = args.split("-");
String arg1 = splitted[0];
String arg2 = splitted[1];
if (flag == 1) {
result = result + "SUB " + arg2 + ",R" + rConut + "\n";
} else if (flag == 2) {
int localRcount = (i + 1) % 2;
result = result + "SUB " + "R" + localRcount + ",R" + rConut + "\n";
}
}
if (args.contains("+")) {
splitted = args.split("\\+");
String arg1 = splitted[0];

73

String arg2 = splitted[1];


if (flag == 1) {
result = result + "ADD " + arg2 + ",R" + rConut + "\n";
} else if (flag == 2) {
int localRcount = (i + 1) % 2;
result = result + "ADD " + "R" + localRcount + ",R" + rConut + "\n";
}
}
if (args.contains("*")) {
splitted = args.split("\\*");
String arg1 = splitted[0];
String arg2 = splitted[1];
if (flag == 1) {
result = result + "MUL " + arg2 + ",R" + rConut + "\n";
} else if (flag == 2) {
int localRcount = (i + 1) % 2;
result = result + "MUL " + "R" + localRcount + ",R" + rConut + "\n";
}
}
if (args.contains("/")) {
splitted = args.split("\\/");
String arg1 = splitted[0];
String arg2 = splitted[1];
if (flag == 1) {
result = result + "DIV " + arg2 + ",R" + rConut + "\n";
} else if (flag == 2) {

74

int localRcount = (i + 1) % 2;
result = result + "DIV " + "R" + localRcount + ",R" + rConut + "\n";
}
}

}
result = result + "STORE R" + rConut + "," + left.get(right.size() - 1);
System.out.println("Printing Code sequence \n" + result);

public static void main(String args[]) throws IOException {


CodeSequence cs = new CodeSequence();
}

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package ImiPackage;

75

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
*
* @author Imran
*/
public class ErrorRepair {
String string,operator="*/+-",bank="0123456789zxcvbnmasdfghjklpoiuytrewq";
int openBrace=0,closeBrace=0, closeDiff=0,openDiff=0,operatorCnt=0,operandCnt=0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public ErrorRepair() throws IOException {
System.out.println("Enter the errorneous string.");
string=br.readLine();
checkOp(string);
braceCount(string);

}
void braceCount(String str){
StringBuilder sb = new StringBuilder(str);
if(str.contains("(") || str.contains(")")){
for(int i=0;i<str.length();i++){
if((str.charAt(i)+"").equals("(")){
openBrace++;

76

}
else if((str.charAt(i)+"").equals(")")){
closeBrace++;
}
}
if(openBrace>closeBrace){
openDiff=openBrace-closeBrace;
System.out.println("Repairing unmatched braces: ");
}
else if(closeBrace>openBrace){
closeDiff=closeBrace-openBrace;
System.out.println("Repaing ummatched braces: ");
}
}
if(closeDiff>0){

for(int i=0;i<closeDiff;i++){
int lastIdx = str.lastIndexOf(")");
System.out.println(sb.deleteCharAt(str.lastIndexOf(")")));
System.out.println(") at "+lastIdx+" index is removed.");
}
}
if(openDiff>0){
for(int i=0;i<openDiff;i++){
int lastIdx = str.lastIndexOf("(");
System.out.println(sb.deleteCharAt(lastIdx));

77

System.out.println("( at "+lastIdx+" index is removed.");


}
}

}
public void checkOp(String str){

for(int i=0;i<str.length();i++){
String ch=str.charAt(i)+"";
for(int j=0;j<operator.length();j++){
if((operator.charAt(j)+"").equals(ch)){
operatorCnt++;
}
}
for(int j=0;j<bank.length();j++){
if((bank.charAt(j)+"").equals(ch)){
operandCnt++;
}
}
}
if(operatorCnt!=operandCnt-1){
System.out.println("Operator miss match");
}
//System.out.println("operator"+operatorCnt);
//System.out.println("operand="+operandCnt);
}

78

public static void main(String[] args) throws IOException {


ErrorRepair er = new ErrorRepair();
}

79

Das könnte Ihnen auch gefallen