I know it sounds easy but I'm rather tired right now and am, hopefully, nearly done with this assignment, another parser, and I need it to merely convert every time TokenHandler.getNextToken() equals some number and convert that and change it to "n".
I'm trying to change all the numbers in the input string to "n"s.
So instead of
123+135*142$
it will have
n+n*n$
The method "validate" should check every string against the input string (the improved input string with n's instead of numbers, and also the top of the state stack by calling checker(TokenHandler.getNextToken(getString()), states.peek()) and setting some boolean to true and returning false if checker ever returns false (Yes I'm aware that right now validate is currently declared as void. I'll fix that later.) and returning true if "tokens" stack has only E on it and "states" stack has 1 on top and 0 as the second value on the stack and nothing else below it.
But how do I deal with converting the numbers in the original input String into "n"'s? Also, TokenHandler had to be reset to take in the value n, or else deleting an "n" would have thrown an error I think, but anyway, now I can use setString() I think to change the numbers into n's but how do I do that? Also, how do I make sure it stops executing and returns true once it reaches the "acceptable" condition mentioned earlier? The E and 0 1 situation.
Here's the code:
import java.util.StringTokenizer; import java.io.*; import java.util.*; public class LR1Parser { private Stack<String> tokens; private Stack<Integer> states; private String tokenString; private static class TokenHandler { private String str; private char nextchar; public static String getNextToken(String tokenStream) { if (tokenStream == null) return null; String numberString = ""; int index = 0; char c = tokenStream.charAt(0); if ( c == '+') { return "+"; } else if (c == '*') { return "*"; } else if (c == '(') { return "("; } else if (c == ')') { return ")"; } else if (c == '0' || c == '1' || c == '2' || c == '3' || c == '4'|| c == '5' || c == '6' || c == '7'|| c == '8' || c == '9') { char c2 = 'a'; numberString = numberString + String.valueOf(c); if (tokenStream.length() == 1) { return numberString; } else { c2 = tokenStream.charAt(1); if (c2 == '0' || c2 == '1' || c2 == '2' || c2 == '3' || c2 == '4'|| c2 == '5' || c2 == '6' || c2 == '7'|| c2 == '8' || c2 == '9') { numberString = numberString + getNextToken(tokenStream.substring(1)); } } return numberString; } else if ( c == '$') { return "$"; } else if (c == 'n') return 'n'; else { return ("Bad String!"); } } public static String deleteToken(String tokenStream) { String temp = getNextToken(tokenStream); System.out.println(tokenStream); int number = temp.length(); String temp2 = tokenStream.substring(number); //tokenStream = temp2; System.out.println(temp2); return temp2; } } public LR1Parser(String input) { tokens = new Stack<String>(); states = new Stack<Integer>(); setString(input); states.push(0); } public void setString(String tokenString) { this.tokenString = tokenString; } public String getString() { return tokenString; } public String read(String input) { } public void validate(String str) { } public boolean checker (String token, Integer number) { if (token.equals("n") ) { if (number == 0 || number == 4 || number == 6 || number == 7) { states.push(5); tokens.push("n"); setString(TokenHandler.deleteToken(getString())); return true; } else return false; } else if (token.equals("+")) { if (number == 1 || number == 8) { states.push(6); tokens.push("+"); setString(TokenHandler.deleteToken(getString())); return true; } else if (number == 2) { if (tokens.peek().equals("T")) { tokens.pop(); tokens.push("E"); states.pop(); if (states.peek() == 0) states.push(1); else if (states.peek() == 4) states.push(8); return true; } else { System.out.println("Error!"); return false; } } else if (number == 3) { if (tokens.peek().equals("F")) { tokens.pop(); states.pop(); if (states.peek() == 0 || states.peek() == 4) states.push(2); else if (states.peek() == 6) states.push(9); return true; } else { System.out.println("Error!"); return false; } } else if (number == 5) { if (tokens.peek().equals("n")) { tokens.pop(); states.pop(); if (states.peek() == 0 || states.peek() == 4 || states.peek() == 6) states.push(3); else if (states.peek() == 7) states.push(10); return true; } else { System.out.println("Error!"); return false; } } else if (number == 8) { states.push(6); tokens.push("+"); setString(TokenHandler.deleteToken(getString())); return true; } else if (number == 9) { if (tokens.peek().equals("T")) { // begin if tokens.pop(); states.pop(); if (tokens.peek().equals("+")) { // begin if tokens.pop(); states.pop(); if (tokens.peek().equals("E")) { // begin if tokens.pop(); states.pop(); tokens.push("E"); if (states.peek() == 0) states.push(1); else if (states.peek() == 4) states.push(8); return true; } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end if else { // begin else System.out.println("Error!"); return false; } // end else } else if (number == 10) { if (tokens.peek().equals("F")) { tokens.pop(); states.pop(); if (tokens.peek().equals("*")) { tokens.pop(); states.pop(); if (tokens.peek().equals("T")) { tokens.pop(); states.pop(); tokens.push("T"); if(states.peek() == 0 || states.peek() == 4) states.push(2); else if (states.peek() == 6) states.push(9); return true; } else { // begin else System.out.println("Error!"); return false; } // end else } else { // begin else System.out.println("Error!"); return false; } // end else } else { // begin else System.out.println("Error!"); return false; } // end else } else if (number == 11) { if (tokens.peek().equals(")")) { tokens.pop(); states.pop(); if (tokens.peek().equals("E")) { tokens.pop(); states.pop(); if(tokens.peek().equals("(")) { tokens.pop(); states.pop(); tokens.push("F"); if (states.peek() == 0 || states.peek() == 4 || states.peek() == 6) states.push(3); else if (states.peek() == 7) states.push(10); return true; } else { // begin else System.out.println("Error!"); return false; } // end else } else { // begin else System.out.println("Error!"); return false; } // end else } else { // begin else System.out.println("Error!"); return false; } // end else } else { // begin else System.out.println("Error!"); return false; } // end else } else if (token.equals("*")) { // begin else if if (number == 2 || number == 9) { // begin if states.push(7); setString(TokenHandler.deleteToken(getString())); return true; } // end if else if (number == 3) { // begin else if if (tokens.peek().equals("F")) { // begin if states.pop(); tokens.pop(); tokens.push("T"); if (states.peek() == 0 || states.peek() == 4) states.push(2); else if (states.peek() == 6) states.push(9); return true; } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end else if else if (number == 5) { // begin else if if (tokens.peek().equals("n")) { // begin if tokens.pop(); states.pop(); tokens.push("F"); if (states.peek() == 0 || states.peek() == 4 || states.peek() == 6) states.push(3); else if (states.peek() == 7) states.push(10); return true; } // end if else {// begin else System.out.println("Error!"); return false; } // end else } // end else if else if (number == 10) { // begin else if if (tokens.peek().equals("F")) { // begin if tokens.pop(); states.pop(); if (tokens.peek().equals("*")) {// begin if tokens.pop(); states.pop(); if (tokens.peek().equals("T")) { // begin if states.pop(); tokens.pop(); tokens.push("T"); if (states.peek() == 0 || states.peek() == 4) states.push(2); else if (states.peek() == 6) states.push(9); return true; } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end else if else if (number == 11) { // begin else if if (tokens.peek().equals(")")) { // begin if states.pop(); tokens.pop(); if (tokens.peek().equals("E")) { // begin if states.pop(); tokens.pop(); if (tokens.peek().equals("(")) { // begin if states.pop(); tokens.pop(); tokens.push("F"); if (states.peek() == 0 || states.peek() == 4 || states.peek() == 6) states.push(3); else if (states.peek() == 7) states.push(10); return true; } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end else if else { // begin else System.out.println("Error!"); return false; } // end else } // end else if else if (token.equals("(")) { // begin else if if (number == 2 || number == 4 || number == 6 || number == 7) { // begin if states.push(4); tokens.push("*"); setString(TokenHandler.deleteToken(getString())); return true; } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end else if else if (token.equals(")")) { // begin else if if (number == 2) { // begin if if (tokens.peek().equals("T")) { // begin if states.pop(); tokens.pop(); tokens.push("E"); if (states.peek() == 0) states.push(1); else if (states.peek() == 4) states.push(8); return true; } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end if else if (number == 3) { // begin else if if (tokens.peek().equals("F")) { // begin if tokens.pop(); states.pop(); tokens.push("T"); if (states.peek() == 0 || states.peek() == 4) states.push(2); else if (states.peek() == 6) states.push(9); return true; } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end else if else if (tokens.peek().equals("n")) { // begin else if tokens.pop(); states.pop(); tokens.push("F"); if (states.peek() == 0 || states.peek() == 4 || states.peek() ==6) states.push(3); else if (states.peek() == 7) states.push(10); return true; } // end else if else if (number == 8) { // begin else if tokens.push(")"); states.push(11); setString(TokenHandler.deleteToken(getString())); return true; } // end else if else if (number == 9) { if (tokens.peek().equals("T")) { // begin if tokens.pop(); states.pop(); if (tokens.peek().equals("+")) { // begin if tokens.pop(); states.pop(); if (tokens.peek().equals("E")) { // begin if tokens.pop(); states.pop(); tokens.push("E"); if (states.peek() == 0) states.push(1); else if (states.peek() == 4) states.push(8); return true; } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end if else { // begin else System.out.println("Error!"); return false; } // end else } else if (number == 10) { if (tokens.peek().equals("F")) { tokens.pop(); states.pop(); if (tokens.peek().equals("*")) { tokens.pop(); states.pop(); if (tokens.peek().equals("T")) { tokens.pop(); states.pop(); tokens.push("T"); if(states.peek() == 0 || states.peek() == 4) states.push(2); else if (states.peek() == 6) states.push(9); return true; } else { // begin else System.out.println("Error!"); return false; } // end else } else { // begin else System.out.println("Error!"); return false; } // end else } else { // begin else System.out.println("Error!"); return false; } // end else } else if (number == 11) { if (tokens.peek().equals(")")) { tokens.pop(); states.pop(); if (tokens.peek().equals("E")) { tokens.pop(); states.pop(); if(tokens.peek().equals("(")) { tokens.pop(); states.pop(); tokens.push("F"); if (states.peek() == 0 || states.peek() == 4 || states.peek() == 6) states.push(3); else if (states.peek() == 7) states.push(10); return true; } else { // begin else System.out.println("Error!"); return false; } // end else } else { // begin else System.out.println("Error!"); return false; } // end else } else { // begin else System.out.println("Error!"); return false; } // end else } else { // begin else System.out.println("Error!"); return false; } // end else } // end else if else if (token.equals("$")) { // begin else if if (number == 1) { // begin if System.out.println("Accepted!"); return true; } // end if else if (number == 2) { // begin else if if (tokens.peek().equals("T")) { // begin if states.pop(); tokens.pop(); tokens.push("E"); if (states.peek() == 0) states.push(1); else if (states.peek() == 4) states.push(8); return true; } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end if else if (number == 3) { // begin else if if (tokens.peek().equals("F")) { // begin if tokens.pop(); states.pop(); tokens.push("T"); if (states.peek() == 0 || states.peek() == 4) states.push(2); else if (states.peek() == 6) states.push(9); return true; } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end else if else if (tokens.peek().equals("n")) { // begin else if tokens.pop(); states.pop(); tokens.push("F"); if (states.peek() == 0 || states.peek() == 4 || states.peek() ==6) states.push(3); else if (states.peek() == 7) states.push(10); return true; } // end else if else if (number == 9) { if (tokens.peek().equals("T")) { // begin if tokens.pop(); states.pop(); if (tokens.peek().equals("+")) { // begin if tokens.pop(); states.pop(); if (tokens.peek().equals("E")) { // begin if tokens.pop(); states.pop(); tokens.push("E"); if (states.peek() == 0) states.push(1); else if (states.peek() == 4) states.push(8); return true; } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end if else { // begin else System.out.println("Error!"); return false; } // end else } // end if else { // begin else System.out.println("Error!"); return false; } // end else } else if (number == 10) { if (tokens.peek().equals("F")) { tokens.pop(); states.pop(); if (tokens.peek().equals("*")) { tokens.pop(); states.pop(); if (tokens.peek().equals("T")) { tokens.pop(); states.pop(); tokens.push("T"); if(states.peek() == 0 || states.peek() == 4) states.push(2); else if (states.peek() == 6) states.push(9); return true; } else { // begin else System.out.println("Error!"); return false; } // end else } else { // begin else System.out.println("Error!"); return false; } // end else } else { // begin else System.out.println("Error!"); return false; } // end else } else if (number == 11) { if (tokens.peek().equals(")")) { tokens.pop(); states.pop(); if (tokens.peek().equals("E")) { tokens.pop(); states.pop(); if(tokens.peek().equals("(")) { tokens.pop(); states.pop(); tokens.push("F"); if (states.peek() == 0 || states.peek() == 4 || states.peek() == 6) states.push(3); else if (states.peek() == 7) states.push(10); return true; } else { // begin else System.out.println("Error!"); return false; } // end else } else { // begin else System.out.println("Error!"); return false; } // end else } else { // begin else System.out.println("Error!"); return false; } // end else } else { // begin else System.out.println("Error!"); return false; } // end else } // end else if else { // begin else System.out.println("Error!"); return false; } // end else } // end method public static void main(String[] args) { LR1Parser parser; String stuffs = "123+5"; String stuffs2 = TokenHandler.deleteToken(stuffs); System.out.println(stuffs2); try { if (!(args[0].equals("$"))) { parser = new LR1Parser(args[0] + "$"); } else { System.out.println("Is valid."); // if it's only a $, used to avoid a problem of non-validity for args[0] = "$" } // parser = new LL1Parser("+123"); } catch(ArrayIndexOutOfBoundsException aioobe) { System.out.println("args[0] doesn't exist!"); System.exit(0); } } }