/* * testparser.cc * by CMB 9/04 * Test driver for parser. * * Parses the file given on the command line. * If it succeeds prints out the resulting AST. * If we have parse errors, prints a message and does not * print the tree. * */ #include "ast.h" extern int yyparse(); // when it reduces to the start symbol, yyparse puts the root of the tree // in this variable extern Program *ast; // linenum and stringtab were defined in testlexer.cc, so they need // to be defined here instead int linenum = 1; #include extern FILE *yyin; // defined in espresso.y extern void initParser(); // defined below void processCmdLine(int argc, char *argv[]); int main(int argc, char* argv[]) { processCmdLine(argc, argv); initParser(); int status = yyparse(); if (status == 0) { // parse was successful ast->dump(cout, 0); } else { cerr << "Compilation halted due to lex and parse errors" << endl; } } // NOTE: yyin is the file variable defined by and used by flex void processCmdLine(int argc, char *argv[]) { if (argc < 2) { cerr << "Error: Missing file to process. Run program as follows:" << endl; cerr << "testparser " << endl; exit(1); } else { yyin = fopen(argv[1], "r"); if (yyin == NULL) { cerr << "Error: Could not open input file " << argv[1] << endl; exit(1); } } }