import java.io.FileReader;
import java.io.PrintWriter;

public class PA0 {
    private static void usage() {
        System.err.println(
        "Specify input file in program arguments");
    }

    public static void main(String args[]) {
        if(args.length < 1) {         
            usage();
            System.exit(1);
        }
        
        // filename should be the last command line option
        String filename = args[args.length-1];
        
        try {
            // construct the lexer, 
            // the lexer will be the same for all of the parsers
            Yylex lexer = new Yylex(new FileReader(filename));

            // create the AST
            parser p = new parser(lexer);
            ast.node.Node ast_root = (ast.node.Node)p.parse().value;

            if(ast_root == null) {
                System.err.println("AST was not generated.");
                System.err.println("Modify your PA0.cup file to generate one");
                System.exit(1);
            }

            // Print the AST as a .dot file:
            java.io.PrintStream astout =
              new java.io.PrintStream(
                  new java.io.FileOutputStream(filename + ".ast.dot"));
            ast_root.accept(new DotVisitor(new PrintWriter(astout)));
            System.out.println("Printing AST to " + filename + ".ast.dot");
            
            // Unparse the AST
            ast_root.accept(new UnparseVisitor(System.out));
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }  
    }
}
