/* * testsemant.cc * by CMB 10/04 * Test driver for semantic analyzer. * * Run as follows: * testparser [-d] * * the optional -d argument turns on debugging output * (flag is semantDebug). * * Parses and semantically analyzes the file given on the command line. * If parsing succeeds continues to semantic analysis. * If we have semantic errors, prints out messages as it goes, but * attempts to recover, and prints out the AST either way. * */ #include "ast.h" #include "SymTab.h" #include "semant.h" #include "SError.h" bool semantDebug = false; // linenum was defined in testlexer.cc, so it needs // to be defined here instead int linenum = 1; #include extern FILE *yyin; // defined in espresso.y extern void initParser(); extern int yyparse(); // defined below char * processCmdLine(int argc, char *argv[]); int main(int argc, char* argv[]) { string fileName = string(processCmdLine(argc, argv)); initParser(); int status = yyparse(); if (status == 1) { // parse was not successful cerr << "Compilation halted due to lex and parse errors" << endl; exit(1); } Semant *semant = new Semant(ast, fileName); // entry point to your semantic analyzer code semant->analyzeProg(); if (semantDebug) { cout << endl; } ast->dump(cout, 0); if (SError::hasErrors()) { cerr << "Compilation halted due to semantic errors" << endl; exit(1); } } char * processCmdLine(int argc, char *argv[]) { char *fileName; if (argc < 2) { cerr << "Error: Missing file to process. Run program as follows:" << endl; cerr << "testparser [-d] " << endl; exit(1); } else if ((argc > 2) && (strcmp(argv[1], "-d") == 0)) { // has both args semantDebug = true; fileName = argv[2]; } else { // argc == 2: has filename only fileName = argv[1]; } yyin = fopen(fileName, "r"); if (yyin == NULL) { cerr << "Error: Could not open input file " << argv[1] << endl; exit(1); } return fileName; }