Question

First off, I'll be forthright in stating that this is a homework question.

I have to build an expression parser using the following code as my basis: Edit:

public class Interpreter {

public static Double parser(ST<String, Double> variables, String[] inputArray)
{       
    Double valueHolder;

    Stack<String> ops = new Stack<String>();
    Stack<Double> vals = new Stack<Double>();

    for(int i = 0; i < inputArray.length; i++)
    {
        String input = inputArray[i];

        if  (input.equals("("))                        ;
        else if (input.equals("+"))     ops.push(input);
        else if (input.equals("-"))     ops.push(input);
        else if (input.equals("*"))     ops.push(input);
        else if (input.equals("/"))     ops.push(input);
        else if (input.equals("sqrt"))  ops.push(input);

        else if (input.equals(")")) 
        {   
            String op = ops.pop();
            double v = vals.pop();
            if      (op.equals("+"))    v = vals.pop() + v;     
            else if (op.equals("-"))    v = vals.pop() - v;
            else if (op.equals("*"))    v = vals.pop() * v;
            else if (op.equals("/"))    v = vals.pop() / v;
            else if (op.equals("sqrt")) v = Math.sqrt(v);
            vals.push(v);
        }
        else if (input.matches("\\D"))
            {
                valueHolder = variables.get(inputArray[i]);
                vals.push(valueHolder);
            }
        else vals.push(Double.parseDouble(input));
    }

    return vals.pop();
}

public static String[] getValue(ST<String, Double> variables, String[] inputArray)
{
    Double keyHolder;
    Double valueHolder;

    for(int i = 0; i < inputArray.length; i++)
    {
        if(variables.contains(inputArray[i]))
            {
                keyHolder = variables.get(inputArray[i]);
                inputArray[i] = keyHolder.toString();
            }
        else if (!variables.contains(inputArray[i]))
        { if (inputArray[i].matches("\\D")) //if letter
            { if (!inputArray[i].equals("=")) //if not "="
                {for (int j = i + 1; j < inputArray.length; j++) //next element
                    { if (inputArray[j].matches("\\D")) //if letter
                        {if (!inputArray[j].matches("=")) //if not "="
                            {

                                //get values and do math
                            }
                        }
                    else if (inputArray[j].matches("\\d")) // if digit
                    { if (j + 1 >= inputArray.length)
                        {
                            valueHolder = Double.parseDouble(inputArray[j]);
                            variables.put(inputArray[i], valueHolder);
                        }
                      else parser(variables, inputArray); //if 
                    }
                    }
                }
            }

        }
    }

    return inputArray;
}

public static void main(String[] args)
{
    ST<String, Double> variables = new ST<String, Double>();

    while(!StdIn.isEmpty())
    {
        String input = StdIn.readLine();
        String[] inputArray = input.split("\\s+");          // read a line and split it by whitespace
        inputArray = getValue(variables, inputArray);       // remove any variables and replace with their associated values
        double y = parser(inputArray);  
        System.out.println(y);
    }

}

}

Console input would be something like:

A = 5
B = 10
C = A + B
D = C * C
print(D)

In this case, the output from the console would be 225. My problem is that I cannot figure out how to split the input between the symbol table's keys and values. The API for inputting keys and values is void put(Key key, Value v) where if a key is set to null, the key is removed. Thanks in advanced.

Was it helpful?

Solution

It is not just string split, you need other checks too, like:

1) Split the string based on = pattern

if(stringValue.matches("="))
{
 String[] valArray = stringValue.split("=");
}
else
{
// do something else
}

This will give you an array of string. Now loop through string array and check for below conditions.

2) Check if there is numeric value present

ie: valArray[].matches("\d");

3) If numeric value is present, check if there is more than 1 occurrences of alphabet numeric values present (to see if there are more than 1 variables present)

This is to check if alphabet is present in any of split strings >> valArray[].matches("\D");

4) Finally, if there is only 1 numeric value and 1 alphanumeric value present, store key and value.

5) If there are more than 1 occurrences of empty variables, then you will need to skip the operation(plus, minus ...) until you have variable value present in key-value array.

You can check this by checking your key-value pair array. You don't store key-value if the value if empty.

6) If = is not present then check for print in the string and do the print operation.

ie:

if(stringValue.matches("print"));
{
//Do something
}

Note: stringValue is your console input line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top