简体   繁体   中英

Java Shunting Yard Algorithm OutOfMemory Error

I want to convert from infix to postfix notation, therefore i would like to use the Shunting Yard Algorithm. I parse the String token for token and use a Stack to store Operators. But when i run my program i got following Exception:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at  ShuntingYardAlgorithm.toPostfix( ShuntingYardAlgorithm.java:46)
at  ShuntingYardAlgorithm.main( ShuntingYardAlgorithm.java:59)

Edit: It seems that my method OperantValues always return -1, but i dont' know how to fix it.

Here is my Code:

public class ShuntingYardAlgorithm {

static int OperantValues(char c) {

    switch (c) {
    case '+':
        return 1;
    case '-':
        return 1;
    case '*':
        return 2;
    case '/':
        return 2;
    }
    return -1;
}

public static String toPostfix(String expr) {

    Stack<Character> stack = new Stack<Character>();
    StringBuilder sb = new StringBuilder();

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

        char c = expr.charAt(i);

        if (Character.isDigit(c)) {
            sb.append(" " + c);
        }

        if (c == '(') {
            stack.push('(');
        }

        else if (c == ')') {

            while (!stack.isEmpty() && stack.peek() != '(')
                sb.append("" + stack.pop());

            stack.pop();

        } else {// Operator

            while (!stack.isEmpty() && OperantValues(c) <= 
 OperantValues(stack.peek()))
                sb.append(c);
            stack.push(c);
        }
    }

    while (!stack.isEmpty())
        sb.append(stack.pop());
    return sb.toString();
 }

public static void main(String[] args) {

    String expr = "(2+3)*((4+7)*3) ";
    System.out.println(toPostfix(expr));
 }

}

Why i got this Exception?

Any help would be appreciated.

Two issues with this code:

  1. Execution should only enter one branch of the if -chain, so there is an else missing:

     if (Character.isDigit(c)) { sb.append(" " + c); } else // insert else here if (c == '(') { stack.push('('); } else if ... 
  2. The code does not handle spaces. To do this, add an extra clause:

      ... stack.pop(); } else if (OperantValues(c) > -1) { // Operator while (!stack.isEmpty() && OperantValues(c) <= OperantValues(stack.peek())) sb.append(c); stack.push(c); } else // other characters including space { continue; // we'll ignore them for now } 

    This was also the cause of OperantValues(c) "always" returning -1 , because the code got stuck on the space at the end of your input string.

Applying the above fixes does the trick. The output becomes:

 2 3+ 4 7+ 3**

As expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM