/* driver3.c * driver program for testing the PASC parser with semantic analysis */ #include #include "tree.h" int outtree = 0; /* flag -- print out syntax tree or not? */ int outtable = 0; /* flag -- print out symbol table or not? */ int trace = 0; /* flag -- trace parser */ int traceST = 0; /* flag -- trace semantic analysis */ int traceGen = 0; /* flag -- trace semantic analysis */ char codeName[80] = {"code.mar"}; /* name of the generated asm file */ FILE *treelst; /* file used to save the syntax tree */ FILE *tablelst; /* file used to save the symbol table */ extern tree Root; /* syntax tree root, imported form tree.c */ extern void init_table(); /* hash table initialization, imported from table.c */ extern int error_count; /* # of semantic errors found */ extern int DataOffset(); /* code generation routines, imported from gen.c */ extern int GenAll(); extern int dump(); /* output code to a file, imported from emit.c */ main(argc, argv) int argc; char *argv[]; { /* check command line switchs */ argc--; argv++; while (argc > 0) { if (!strcmp(*argv, "-h")) { /* command usage help */ printf("Usage: "); printf("pasc [-f codename [-tp [-ts [-tg "); printf("[-tr [treefile] [-tb [tablefile]]]]]]] < file.pasc\n"); return; } else if (!strcmp(*argv, "-f")) { /* output asm code file name */ if (argc > 1) { strcpy(codeName, *++argv); argc--; } } else if (!strcmp(*argv, "-tp")) { /* trace parser actions */ trace = 1; } else if (!strcmp(*argv, "-ts")) { /* trace semantic analysis */ traceST = 1; } else if (!strcmp(*argv, "-tg")) { /* trace code generation */ traceGen = 1; } else if (!strcmp(*argv, "-tr")) { /* print syntax tree */ outtree = 1; if (argc > 1) { if ((treelst = fopen(*++argv, "w")) == NULL) { printf("Can't open file '%s' for output. ", *argv); printf("Stdout is assumed\n\n"); treelst = stdout; } argc--; } else { printf("Tree file not specified. Stdout is assumed\n\n"); treelst = stdout; } } else if (!strcmp(*argv, "-tb")) { /* print symbol table */ outtable = 1; if (argc > 1) { if ((tablelst = fopen(*++argv, "w")) == NULL) { printf("Can't open file '%s' for output. ", *argv); printf("Stdout is assumed\n\n"); tablelst = stdout; } argc--; } else { printf("Table file not specified. Stdout is assumed\n\n"); tablelst = stdout; } } argc--; argv++; } init_table(); /* initialize hash table */ if (!yyparse()) { seman(Root); if (error_count == 0) { DataOffset(); if (outtree) {printtree(Root,0); fclose(treelst);} if (outtable) {STPrint(); fclose(tablelst);} GenAll(); generate_code(codeName); printf("Program accepted\n"); return; } } printf("Program rejected\n\n"); }