import java.util.*;
import java.io.*;
public class RE_to_ET {
static final int NUMBER = 0, OPERATOR = 1, CONJ = 2; // values for kind.
private String infixExp; // input infix expression
private String postfixExp = ""; // output postfix expression
Stack<String> stringStack; // create stack for inpix to postfix
private Stack<Node> chkStack;
private String output = "";
String scan = "";
int x = 400;
int y = 200;
int size = 0;
double chk = 0;
char chk2 = ' ';
static int lx = 2, s = 0;
//------------------Create Stack Postfix----------------------------------------------------
public void Postfix(String in){
infixExp = in;
stringStack = new Stack<String>();
}
//------------------Input to Postfix---------------------------------------------------------
public String In2PostExp()throws IOException {
for (int j = 0; j < infixExp.length(); j++) {
char ch = infixExp.charAt(j);
String chx = String.valueOf(ch);
switch (ch) {
case '|': gotOper(chx, 1); break;
case '.': gotOper(chx, 2); break;
case '(': stringStack.push(chx); break;
case ')': gotParen(); break;
default:
postfixExp = postfixExp + " " + chx;
break;
}
}
while (!stringStack.isEmpty()) {
String chx = stringStack.pop();
postfixExp = postfixExp +" "+ chx;
}
FileWriter svg = new FileWriter("ET.svg",true);
BufferedWriter out = new BufferedWriter(svg);
out.write("<?xml version=\"1.0\" standalone=\"no\"?>\n");
out.write("<svg viewBox=\"0 0 1024 1024\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n");
out.close();
Post_to_ET(postfixExp);
FileWriter svg3 = new FileWriter("ET.svg",true);
BufferedWriter out3 = new BufferedWriter(svg3);
out3.write("</svg>");
out3.close();
return postfixExp;
}
//------------------Got Operator---------------------------------------------------------
public void gotOper(String opThis, int prec1) {
while (!stringStack.isEmpty()) {
String chx = stringStack.pop();
char opTop = chx.charAt(0);
if (opTop == '(') {
stringStack.push(chx); break;
} else {
int prec2;
if (opTop == '|') { prec2 = 1;
} else { prec2 = 2; }
if (prec2 < prec1){
stringStack.push(chx); break;
}else if (prec2 == prec1){
stringStack.push(chx); break;
} else { postfixExp = postfixExp + " " + chx; }
}
}
stringStack.push(opThis);
}
//------------------gotParen--------------------------------------------------------------
public void gotParen(){
while (!stringStack.isEmpty()) {
String chx = stringStack.pop();
char ch = chx.charAt(0);
if (ch == '(') {break;
} else { postfixExp = postfixExp + " " + chx; }
}
}
//------------------Infix input---------------------------------------------------------
public static void main(String[] args)throws IOException {
//String input = "(0+1?)";
//String input = "(0+1?|01)";
String input = "(0|1)+((10|01)?1)*00";
//String input = "(000|110+)10";
//String input = "(0|1)*101(0|1)*";
//String input = "0(01|10)+0";
//String input = "1100?1";
//----------------Create-New-input--------------------------------------------------------
String Ninput = "";
for (int i =0; i < input.length(); i++ ){
char ch1 = input.charAt(i);
char ch2; String ST;
if((i+1) < input.length()){ ch2 = input.charAt(i+1); }else{ ch2='x'; }
switch (ch1) {
case '0':case '1':
ST = String.valueOf(ch1); Ninput = Ninput+ST;
switch (ch2) {
case '0':case '1':case '(': Ninput = Ninput+"."; break;
default: break;
}
break;
case '*':case '+':case '?':
ST = String.valueOf(ch1); Ninput = Ninput+ST;
switch (ch2) {
case 'x': case '|': case ')': break;
default: Ninput = Ninput+"."; break;
}
break;
case '(':case '|':
String ST2 = String.valueOf(ch1); Ninput = Ninput+ST2; break;
case ')':
ST = String.valueOf(ch1); Ninput = Ninput+ST;
switch (ch2) {
case '*':case '+':case '?': break;
case '0':case '1':case '(': Ninput = Ninput+"."; break;
default: break;
}
break;
default: break;
}
}
System.out.println("Input:"+input);
System.out.println("New-Input:"+Ninput);
//----------------Create-Postfix--------------------------------------------------------
RE_to_ET p = new RE_to_ET();
p.Postfix(Ninput);
System.out.println("Postfix: " + p.In2PostExp() );
}
public void Post_to_ET(String in)throws IOException {
chkStack = new Stack<Node>();
for (int j = 0; j < in.length(); j++) {
char c = in.charAt(j);
char cn;
if ((j+1) < in.length()){
cn = in.charAt(j+1);
}else{
cn = 'y';
}
switch (c) {
case '|':
case '.':
Node t1 = chkStack.pop();
Node t2 = chkStack.pop();
Node t3 = new Node(c, t1, t2);
chkStack.push(t3);
break;
case '+':
case '*':
case '?':
Node t4 = chkStack.pop();
Node t5 = new Node(c, t4);
chkStack.push(t5);
break;
case ' ':
break;
default: // must be an operand
Node t6 = new Node(c);
chkStack.push(t6);
break;
}
}
Node t = chkStack.pop();
noderTest(t,x,y);
}
public static void noderTest(Node node,int xc ,int yc)throws IOException{
Node t = node;
FileWriter svg2 = new FileWriter("ET.svg",true);
BufferedWriter out2 = new BufferedWriter(svg2);
if ( t.kind == NUMBER) {
out2.write("<circle cx=\""+xc+"\" cy=\""+yc+"\" r=\"30\" stroke='black' stroke-width='2' fill='white' fill-opacity='.85' />\n");
out2.write("<text class='Label' x=\""+(xc-2)+"\" y=\""+(yc+5)+"\">"+t.op+"</text>\n");
out2.close();
}
else if (t.kind == CONJ) {
out2.write("<circle cx=\""+xc+"\" cy=\""+yc+"\" r=\"30\" stroke='black' stroke-width='2' fill='white' fill-opacity='.85' />\n");
out2.write("<text class='Label' x=\""+(xc-2)+"\" y=\""+(yc+5)+"\">"+t.con+"</text>\n");
out2.write("<line x1 = \""+(xc)+"\" y1 = \""+(yc+30)+"\" x2 = \""+(xc)+"\" y2 = \""+(yc+70)+"\" stroke = \"black\" stroke-width = \"3\"/>\n");
if (t.down != null){
noderTest(t.down,(xc),(yc+100));
}
}
else {
out2.write("<circle cx=\""+xc+"\" cy=\""+yc+"\" r=\"30\" stroke='black' stroke-width='2' fill='white' fill-opacity='.85' />\n");
out2.write("<text class='Label' x=\""+(xc-2)+"\" y=\""+(yc+5)+"\">"+t.op+"</text>\n");
}
if (t.left != null) {
lx++;
if (s == 1){
lx++;
}
s = 0;
out2.write("<line x1 = \""+(xc-30)+"\" y1 = \""+(yc+10)+"\" x2 = \""+(xc-(1200/lx)+30 )+"\" y2 = \""+(yc+90)+"\" stroke = \"black\" stroke-width = \"3\"/>\n");
noderTest(t.left,(xc-1200/lx),(yc +100));
}
if (node.right != null) {
xc = xc +(1200/lx);
yc += 100;
out2.write("<line x1 = \""+(xc-20)+"\" y1 = \""+(yc-20)+"\" x2 = \""+(xc-