Parsing Your first task is to parse in input expression string into meaningful u
ID: 3597972 • Letter: P
Question
Parsing Your first task is to parse in input expression string into meaningful units called tokens. Parsing will take an expression as a string and return an array list of the tokens in the expression, such that each token is string. We use an array list because it is more efficient than a linked list in certain situations. Thus, the expression "(27 572)" should be parsed into the list of five tokens: "(", "27", "", "572", ")". Note that white spaces in an expression string must be ignored when parsing We can then use this list of tokens in the eval() method to calculate the value of the expression It is important that for any expressions containing the "++" and "--" operators be stored as a single token rather than as two. Thus, "++25)" should be parsed as "(", "++", "25", ")" rather than "(”, “+", "+", "25", ")". Also note that "25" must be not be parsed as "2", "5". The parsing is done in the Expression constructor. You will need to fill in the missing code where it is indicated You should use the Java StringBuilder class to build each token as you parse the expres sion string. The append) and delete) methods of the StringBuilder class will be helpful in tokenizing the string Evaluation The evaluation is done in the eval method. Again, your task is to fill in the missing code where it is indicated. The easiest approach is to use two stacks, one to store values (named valueStack in the starter code) and another (named operatorStack) to store tokens such as operators or brackcts. We emphasize that you may assume that all expressions are valid; that is, they are syntactically correct and wil evaluate to a single value. HINT: Considering that all expressions are syntactically correct, there is no need to store the opening bracket of an expression in the stacksExplanation / Answer
// Java program to implement Expression parsing
import java.util.ArrayList;
public class Expression {
private ArrayList<String> tokenList; // List containing the tokens
// parameterized constructor to extract the tokens from expression String and adds to tokenList
public Expression(String expressionString) throws IllegalArgumentException
{
tokenList = new ArrayList<String>();
StringBuilder token = new StringBuilder();
// loop to extract tokens from the expressionString and add in tokenList
for(int i=0;i<expressionString.length();i++)
{
if(expressionString.charAt(i)>='1' && expressionString.charAt(i)<='9') // if the token is a number then continue till the end of number
{ int j=i+1;
while(expressionString.charAt(j)>='1' && expressionString.charAt(j)<='9')// loop to extract the entire number
j++;
token = token.append(expressionString.substring(i,j));// token to append the number to string builder
tokenList.add(token.toString()); // add the number token to tokenList
token.delete(0, token.length()); // delete all the characters of token
i=j-1;
}else if(!(expressionString.substring(i,i+1).equals(" "))) // if character is not space add to the tokenList
{
tokenList.add(expressionString.substring(i,i+1));
}
}
}
// function to display the token in the tokenList
public void displayExpressionToken()
{
System.out.print("Tokens: ");
for(int i=0;i<tokenList.size() -1 ;i++)
System.out.print("""+tokenList.get(i)+"", ");
System.out.print("""+tokenList.get(tokenList.size()-1)+"" ");
}
public static void main(String[] args) {
String inputExpression = "(8 * [((27 * 78) + 572) + 87])"; //define the expression String
Expression expression = new Expression(inputExpression); // call the constructor with the inputExpression
System.out.println("Input Expression : "+inputExpression); // display the inputExpression
expression.displayExpressionToken();// display the tokens
}
}
// End of Program