/** Name: tdparse Description: parser class for tdparse project Programmer: Spencer R. Shimko Date: 03-25-2003 **/ import java.util.*; public class parser{ static LinkedList results = new LinkedList(); // holds results to be returned static Hashtable s_vars = new Hashtable(); // hashtable to store .see vars /** parse: performs parsing receives: LL of input line returns: LL of output lines (error or interperted output lines) **/ public static LinkedList parse(LinkedList input){ String line; // will hold input line while parsing String[] p_line; // line after parsing String[] t_line; // temp line //begin parsing input line by line for (int x=0; x1){// to save alot of cycles if there aren't paren... ignore this for (int y=0;y1){ // if expression conatins ")" // we can now evaluate part of the expression b/c we have ")" temp=parseTerm(t_line[0]); // null check if (temp!=null){ e_line[y]=temp.toString().concat(t_line[1]); }else { return null;} } } while (t_line.length != 1); // do until there are no more ")" in this expression } // end for loop finding ")" and evaluating // the parentheses have been accounted for and removed... // the string can now be put back together again // When putting the string back together we must keep in mind // that the first element in the array was not inside a parentheses // so we do that one last for (int y=(e_line.length-1);y>=1;y--) e_line[y-1].concat(e_line[y]); temp=parseTerm(e_line[1]); if (temp!=null){ ex=e_line[0].concat(temp.toString()); }else {return null;} }// end if paren found return parseTerm(ex); }// end method parseExpr /** parseTerm: prase arithmetic expression without parentheses receives: input string returns: Float value or null if invalid expression **/ private static Float parseTerm(String t){ String[] p_line; // same as above... stores line at various states of parsing String[] t_line; // same as above... stores line at various states of parsing String signs=""; //store order of signs to be done // now we should just have variable names or values in p_line // so to avoid unnecessary cycles again check these for validity // here we split on any sign p_line=t.split("\\s*[+\\-*/]\\s*"); for(int x=0;x2){// if there are too many decimals results.add("Error: "+ p_line[x] + " too many decimals!"); return null; } t_line=p_line[x].split("[^0-9\\.]"); // parse number to see if it has nonnumeric if (t_line.length>1){ // if the numeric expression contains non-numeric/decimal results.add("Error: "+ p_line[x] + " numeric expression contains weird character!"); return null; } } else { // the variable name was found... replace with declared value // swaps varible's name for the actually value t=t.replaceFirst(p_line[x],(String)s_vars.get(p_line[x])); p_line[x]=(String)s_vars.get(p_line[x]); } }// end for looping through possible terms // compress numeric terms into smaller array for easier indexing int temp=0; t_line=p_line; for (int y=0;y0){ // if it is a valid number t_line[temp]=p_line[y]; temp++; } p_line=t_line; // the p_line will store a very concise array of numeric expressions ///////////////// by this point all strings in p_line are valid numeric expressions ///////////////// lets do some arithmetic but we have to go back to the original ///////////////// string t to get the symbols if ((t.charAt(0)=='-')||(isChar(t.charAt(0)))||(isNum(t.charAt(0)))){ // if the first char is a unary minus t_line=t.split("[0-9\\.]"); for (int z=0; z1){ // incorrect number format results.add("Error: "+ t_line[z] + " not a valid term!"); return null; }// end if trying to match unary minus } // end for loop retreieving operators if (t.charAt(0)=='-') p_line[0]=String.valueOf("-").concat(p_line[0]); } // end if first digit is negative else { results.add("Error: incorrect numeric expression... start invalid!"); return null; } // now we need to loop through the operations doing div.and mult. first L->R while ((p_line.length>1)&&(signs.length()>1)) // used to restart for loop when arrays are resized for(int z=0; zR while ((p_line.length>1)&&(signs.length()>1))// used to restart for loop when arrays are resize for(int z=0; zR return (Float.valueOf(p_line[0])); // p_line[0] will contain the eval. expression }// end parseTerm method /** isChar: is the parameter an alpha character? receives: character returns: true if the parameter is a alpha char, false otherwise **/ private static boolean isChar(char x){ for (int y='a';y<='z';y++) if (x==y) return true; for (int y='A';y<='Z';y++) if (x==y) return true; return false; } // end method isChar /** isNum: is the parameter a numeric receives: character returns: true if the parameter is numeric **/ private static boolean isNum(char x){ for (int y='0';y<='9';y++) if (x==y) return true; return false; } // end method isNum }// end class parser