Question: I keep getting this error, I do not know what the issue is : Enter sentence to parse: id*(id+id)$ Exception in thread main java.lang.ArrayIndexOutOfBoundsException: Index
I keep getting this error, I do not know what the issue is :
Enter sentence to parse: id*(id+id)$ Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6 at LRparser.main(LRparser.java:46)
import java.util.*;
import java.util.Scanner;
public class LRParser { private static final int NUM_STATES = 6; private static final int NUM_SYMBOLS = 6; private static final String[] SYMBOLS = {"id", "+", "*", "(", ")", "$"}; // Parsing table generated by the LR parser generator private static final int[][] TABLE = { // id + * ( ) $ {5, -1 , -1 , 4,-1, -1}, {-1, 6, -1 , -1 , -1 , 100}, {-1, -2, 7, 0, -2, -2}, {0, -4, -4, 0, -4, -4}, {5, 0, 0, 4, 0, 0}, {0, -6, -6, 0, -6, -6}, {5, 0, 0, 4, 0, 0}, {5, 0, 0, 4, 0, 0}, {0,6,0,0,11,0}, {0, -1,7,0,-1,-1}, {0, -3, -3, 0, -3, -3}, {0 , -5, -5, 0, -5, -5} }; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter sentence to parse: "); String input = scanner.nextLine(); // Clean up input input = input.replaceAll("\\s", "") + "$"; // Initialize state stack and symbol stack Stack stateStack = new Stack<>(); Stack symbolStack = new Stack<>(); stateStack.push(0); symbolStack.push("$"); // Parse input string int i = 0; while (i < input.length()) { int state = stateStack.peek(); String symbol = input.charAt(i) + ""; int symbolIndex = findSymbolIndex(symbol); int action = TABLE[state][symbolIndex]; if (action > 0) { // Shift stateStack.push(action); symbolStack.push(symbol); i++; } else if (action < 0) { // Reduce int ruleIndex = -action; String ruleSymbol = SYMBOLS[ruleIndex - 1]; int numPop = ruleSymbol.length(); // Pop symbols and states from stack stateStack.pop(); for (int j = 0; j < numPop; j++) { symbolStack.pop(); } // Get new state and symbol from top of stack int newState = stateStack.peek(); int newSymbolIndex = findSymbolIndex(ruleSymbol); if (newSymbolIndex == SYMBOLS.length) { // Handle unrecognized symbols System.out.println("Error: unrecognized symbol " + ruleSymbol); return; } String newSymbol = SYMBOLS[newSymbolIndex]; // Push new symbol and state onto stack symbolStack.push(newSymbol); stateStack.push(TABLE[newState][newSymbolIndex]); } else { // Error System.out.println("Error: invalid input"); return; } // Print stack contents and input after each action System.out.println(stateStack.toString() + " " + symbolStack.toString() + " " + input.substring(i)); } // Check if final state and symbol are valid if (stateStack.peek() == 5 && symbolStack.peek().equals("$")) { System.out.println("Input successfully parsed"); } else { System.out.println("Error: input could not be parsed"); } } private static int findSymbolIndex(String symbol) { for (int i = 0; i < SYMBOLS.length; i++) { if (SYMBOLS[i].equals(symbol)) { return i; } } // Return an out-of-bounds index for unrecognized symbols return SYMBOLS.length; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
