// Programmer: Austin Guthals // Program: A java program that parses a simple arithmetic expression. /* Here are the production rules in EBNF form --> {(+|-) } --> {(*|/) } --> | ( ) --> {(|) --> a|b|c|d|....|x|y|z|A|B|C|D|....|Y|Z{(a|b|c|..|z|A|...|Z)} --> 0|1|2|3|4|5|6|7|8|9{(0|1|2|3|4|5|6|7|8|9)} note: These production rules require spaces between each lexeme */ import java.io.*; import java.lang.*; import java.util.*; import structure.*; public class parser3 { static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); static boolean error = true; // used for an id error flag static boolean error2 = false; static boolean error3 = true; static boolean error4 = true; static boolean parenTest = true; // used to make sure if ( is present then ) is too static boolean nullTest = false; // used to make sure an id was entered static boolean done = false; // signal for last token static String tokenTest; static String token; static String token1; // used as a buffer static StringTokenizer data; static int numberTokens; public static void main(String args[]) throws IOException { ReadStream r = new ReadStream(); boolean cntnue = true; String cont = new String(); while(cntnue) { System.out.println("Enter an arithmetic expression"); String expression = new String(); expression = keyboard.readLine(); data = new StringTokenizer(expression); numberTokens = data.countTokens(); tokenTest = new String("# "); lexical(); expression(); if(((error && nullTest) && error3) && error4) System.out.println("valid expression"); else System.out.println("invalid expression"); if(!nullTest) System.out.println("error2 nothing entered"); if(!error) System.out.println("error1 invalid id or unknown operator"); if(!error4) System.out.println("error3 ) entered without ( in front"); if(!error3) System.out.println("error4 ( entered without )"); error = true; error3 = true; error4 = true; System.out.println("Would you like to quit ?"); System.out.println("Enter a q to quit, enter any other key to continue."); cont = keyboard.readLine(); if(cont.equals("q") || cont.equals("Q")) cntnue = false; else cntnue = true; } } public static void lexical() { // lexical reads lexemes and creates corresponding tokens if(data.hasMoreTokens()) { tokenTest = new String("#"); //tokenTest can't be null nullTest = true; //tokenTest is like a boolean, it will be a flag token = data.nextToken(); if(token.equals("(")) tokenTest = new String("leftParen"); else if(token.equals(")")) tokenTest = new String("rightParen"); else if(token.equals("+")) tokenTest = new String("plusCode"); else if(token.equals("-")) tokenTest = new String("minusCode"); else if(token.equals("*")) tokenTest = new String("multCode"); else if(token.equals("/")) tokenTest = new String("divCode"); else id(); } else done = true; } public static void expression() { term(); while(tokenTest.equals("plusCode")||tokenTest.equals("minusCode")) { lexical(); term(); if(done) break; } } public static void term() { factor(); while(tokenTest.equals("multCode") || tokenTest.equals("divCode")) { lexical(); factor(); if(done) break; } } public static void factor() { if(tokenTest.equals("idCode")) { lexical(); if((tokenTest.equals("rightParen")) && parenTest) { System.out.println("error4"); error4 = false; } return; } else if(tokenTest.equals("leftParen")) { parenTest = false; lexical(); expression(); if(tokenTest.equals("rightParen")) { parenTest = true; lexical(); return; } else { error3 = false; // no right parenthesis entered } } else // token was not operator or id error4 = false; // there was a ) without matching ) in front } public static void id() { error = false; // if error = false at end of method then error2 = false; // token is not a valid identifier int tokenSize = token.length(); token1 = new String(token); String alphabet = new String("abcdefghijklmnopqrstuvwxyz"); char[] alph = alphabet.toCharArray(); String alphabetcaps = new String(alphabet.toUpperCase()); char[] alphcaps = alphabetcaps.toCharArray(); String alphanumeric = new String(alphabet.concat("0123456789")); alphanumeric = new String(alphanumeric.concat(alphabetcaps)); char[] alphnum = alphanumeric.toCharArray(); char[] tkn = token1.toCharArray(); for (int i=0; i < 26; i++) { if((tkn[0]==alph[i]) || (tkn[0]==alphcaps[i])) { error2 = true; } } if(!error2) error = false; // the charecter was not in the alphabet else error = true; if(tokenSize >= 2) { for (int i=1; i < tokenSize; i++) { error2 = false; for (int q = 0; q < 62; q++) { if(tkn[i] == alphnum[q]) { error2 = true; } } if(!error2) // the charecter was not an alphanumeric error = false; } } if(error) { tokenTest = new String("idCode"); } } }