/* CalcAST.java A calculator language translator Requires AST.java CalcAST: javac CalcAST.java */ import java.io.*; public class CalcAST { private static StreamTokenizer tokens; private static int token; public static void main(String argv[]) throws IOException { InputStreamReader reader; if (argv.length > 0) reader = new InputStreamReader(new FileInputStream(argv[0])); else reader = new InputStreamReader(System.in); // create the tokenizer: tokens = new StreamTokenizer(reader); tokens.ordinaryChar('.'); tokens.ordinaryChar('-'); tokens.ordinaryChar('/'); // advance to the first token on the input: getToken(); // parse expression and get abstract syntax tree (AST): AST ast = expr(); // check if expression ends with ';' and translate AST to Lisp if (token == (int)';') { System.out.println("Lisp:"); ast.toLisp(); System.out.println(""); } else System.out.println("Syntax error"); } // getToken - advance to the next token on the input private static void getToken() throws IOException { token = tokens.nextToken(); } // expr - parse -> private static AST expr() throws IOException { AST subtotal = term(); return term_tail(subtotal); } // term - parse -> private static AST term() throws IOException { AST subtotal = factor(); return factor_tail(subtotal); } // term_tail - parse -> | empty private static AST term_tail(AST subtotal) throws IOException { if (token == (int)'+') { getToken(); AST termvalue = term(); return term_tail(new AST('+', subtotal, termvalue)); } else if (token == (int)'-') { getToken(); AST termvalue = term(); return term_tail(new AST('-', subtotal, termvalue)); } else return subtotal; } // factor - parse -> '(' ')' | '-' | ID | NUM private static AST factor() throws IOException { if (token == (int)'(') { getToken(); AST ast = expr(); if (token == (int)')') getToken(); else System.out.println("closing ')' expected"); return ast; } else if (token == (int)'-') { getToken(); return new AST('%', factor()); } else if (token == tokens.TT_WORD) { String id = tokens.sval; getToken(); return new AST(id); } else if (token == tokens.TT_NUMBER) { getToken(); return new AST(new Integer((int)tokens.nval)); } else { System.out.println("factor expected"); return new AST(new Integer(0)); } } // factor_tail - parse -> | empty private static AST factor_tail(AST subtotal) throws IOException { if (token == (int)'*') { getToken(); AST factorvalue = factor(); return factor_tail(new AST('*', subtotal, factorvalue)); } else if (token == (int)'/') { getToken(); AST factorvalue = factor(); return factor_tail(new AST('/', subtotal, factorvalue)); } else return subtotal; } }