Skip to content

Instantly share code, notes, and snippets.

@awgtek
Created November 16, 2016 04:31
Show Gist options
  • Save awgtek/97da1289fb39e78f4ed6229a0d1f94ed to your computer and use it in GitHub Desktop.
Save awgtek/97da1289fb39e78f4ed6229a0d1f94ed to your computer and use it in GitHub Desktop.
A Sub Expression Parser - useful to extract the expressions from regex groupings where the regex is a series of "OR" clauses that translate to options in a drop down etc.
package optionsparser;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;
public class SubExpressionParser {
public static void main(String[] args) {
String input = "(^a bc$)|(xyz)|(123)";
String output;
InToPost theTrans = new InToPost(input);
output = theTrans.doTrans();
System.out.println("Postfix is " + output + '\n');
System.out.println("evaluated: " + Postfix.postfixEvaluate(output));
}
public static class InToPost {
private Stack theStack;
private String input;
private String output = "";
public InToPost(String in) {
input = in;
int stackSize = input.length();
theStack = new Stack(stackSize);
}
public String doTrans() {
for (int j = 0; j < input.length(); j++) {
char ch = input.charAt(j);
switch (ch) {
case '|':
gotOper(ch, 1);
break;
case '(':
theStack.push(ch);
break;
case ')':
gotParen(ch);
output = output + ",;.;,";
break;
default:
output = output + ch;
break;
}
}
while (!theStack.isEmpty()) {
output = output + theStack.pop() + ",;.;,";
}
System.out.println(output);
return output;
}
public void gotOper(char opThis, int prec1) {
while (!theStack.isEmpty()) {
char opTop = theStack.pop();
if (opTop == '(') {
theStack.push(opTop);
break;
} else {
int prec2;
if (opTop == '|')// || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) {
theStack.push(opTop);
break;
} else
output = output + opTop + ",;.;,";
}
}
theStack.push(opThis);
}
public void gotParen(char ch) {
while (!theStack.isEmpty()) {
char chx = theStack.pop();
if (chx == '(')
break;
else
output = output + chx;
}
}
public static void main(String[] args) throws IOException {
String input = "(^a bc$)|(xyz)|(123)";
String output;
InToPost theTrans = new InToPost(input);
output = theTrans.doTrans();
System.out.println("Postfix is " + output + '\n');
System.out.println("evaluated: " + Postfix.postfixEvaluate(output));
}
class Stack {
private int maxSize;
private char[] stackArray;
private int top;
public Stack(int max) {
maxSize = max;
stackArray = new char[maxSize];
top = -1;
}
public void push(char j) {
stackArray[++top] = j;
}
public char pop() {
return stackArray[top--];
}
public char peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
}
}
public static class Postfix {
public static void main(String[] args) {
System.out.println(postfixEvaluate("^a bc$,;.;,xyz,;.;,|,;.;,123,;.;,|,;.;,"));
}
public static String postfixEvaluate(String exp) {
Stack<String> s = new Stack<String> ();
Scanner tokens = new Scanner(exp).useDelimiter("\\s*,;.;,\\s*");;
while (tokens.hasNext()) {
if (!tokens.hasNext("\\|")) {
s.push(tokens.next());
} else {
String num2 = s.pop();
String num1 = s.pop();
String op = tokens.next();
s.push(num2 + ",;.;," + num1);
}
}
return s.pop();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment