int main( int argc, char **argv ) { if( argc == 1 ) { yyin = stdin; } else if( argc == 2 ) { FILE *fp = fopen( argv[1], "r" ); if( fp == NULL ) { fprintf( stderr, "Open file error\n" ); exit(-1); } yyin = fp; } else { fprintf( stderr, "Usage: ./parser [filename]\n" ); exit(0); } symbolTable = (struct SymTable *)malloc(sizeof(struct SymTable)); initSymTab( symbolTable ); getFileName(argv[1]); strncpy(className, fileName, sizeof(className)); output = fopen(strncat(fileName, ".j", 2), "w"); if (!output) { puts("[-] file open error"); exit(1); } yyparse(); /* primary procedure of parser */ if(semError == __TRUE){ fprintf( stdout, "\n|--------------------------------|\n" ); fprintf( stdout, "| There is no syntactic error! |\n" ); fprintf( stdout, "|--------------------------------|\n" ); } else{ fprintf( stdout, "\n|-------------------------------------------|\n" ); fprintf( stdout, "| There is no syntactic and semantic error! |\n" ); fprintf( stdout, "|-------------------------------------------|\n" ); } fclose(output); exit(0); }
int compile(char *fileName) { if (openInputStream(fileName) == IO_ERROR) return IO_ERROR; currentToken = NULL; lookAhead = getValidToken(); initSymTab(); compileProgram(); cleanSymTab(); free(currentToken); free(lookAhead); closeInputStream(); return IO_SUCCESS; }
int main( int argc, char **argv ) { if( argc != 2 ) { fprintf( stdout, "Usage: ./parser [filename]\n" ); exit(0); } FILE *fp = fopen( argv[1], "r" ); if( fp == NULL ) { fprintf( stdout, "Open file error\n" ); exit(-1); } // parse argv[1] and setup fileName const char* slashPos = strrchr(argv[1], '/'); size_t fileNameLength; if( slashPos ) strcpy(fileName, slashPos+1); else strcpy(fileName, argv[1]); fileNameLength = strlen(fileName); if( fileName[fileNameLength-2] != '.' || fileName[fileNameLength-1] != 'p' ) { fprintf( stdout, "input file: %s \tshould be [fileName].p \n", fileName ); exit(-1); } fileName[fileNameLength-2] = '\0'; yyin = fp; // initial symbol table symbolTable = (struct SymTable *)malloc(sizeof(struct SymTable)); initSymTab( symbolTable ); // initial function return recoder yydebug = 0; yyparse(); /* primary procedure of parser */ exit(0); }
/* Function buildSymtab constructs the symbol * table by preorder traversal of the syntax tree */ void buildSymtab(TreeNode * syntaxTree) { initSymTab(); traverse(syntaxTree,insertNode,nullProc); if (TraceAnalyze) { int funcI; for(funcI=0;funcI<MAXFUNCAMT;funcI++) { if(location[funcI]==0) { continue; } fprintf(listing,"\nSymbol table %d:\n\n", funcI); st_setCurFuncNum(funcI); printSymTab(listing); } } a_curFuncNum=0; st_setCurFuncNum(0); }
int main() { Object* obj; initSymTab(); obj = createProgramObject("PRG"); enterBlock(obj->progAttrs->scope); obj = createConstantObject("c1"); obj->constAttrs->value = makeIntConstant(10); declareObject(obj); obj = createConstantObject("c2"); obj->constAttrs->value = makeCharConstant('a'); declareObject(obj); obj = createTypeObject("t1"); obj->typeAttrs->actualType = makeArrayType(10, makeIntType()); declareObject(obj); obj = createVariableObject("v1"); obj->varAttrs->type = makeIntType(); declareObject(obj); obj = createVariableObject("v2"); obj->varAttrs->type = makeArrayType(10, makeArrayType(10, makeIntType())); declareObject(obj); obj = createFunctionObject("f"); obj->funcAttrs->returnType = makeIntType(); declareObject(obj); enterBlock(obj->funcAttrs->scope); obj = createParameterObject("p1", PARAM_VALUE, symtab->currentScope->owner); obj->paramAttrs->type = makeIntType(); declareObject(obj); obj = createParameterObject("p2", PARAM_REFERENCE, symtab->currentScope->owner); obj->paramAttrs->type = makeCharType(); declareObject(obj); exitBlock(); obj = createProcedureObject("p"); declareObject(obj); enterBlock(obj->procAttrs->scope); obj = createParameterObject("v1", PARAM_VALUE, symtab->currentScope->owner); obj->paramAttrs->type = makeIntType(); declareObject(obj); obj = createConstantObject("c1"); obj->constAttrs->value = makeCharConstant('a'); declareObject(obj); obj = createConstantObject("c3"); obj->constAttrs->value = makeIntConstant(10); declareObject(obj); obj = createTypeObject("t1"); obj->typeAttrs->actualType = makeIntType(); declareObject(obj); obj = createTypeObject("t2"); obj->typeAttrs->actualType = makeArrayType(10, makeIntType()); declareObject(obj); obj = createVariableObject("v2"); obj->varAttrs->type = makeArrayType(10, makeIntType()); declareObject(obj); obj = createVariableObject("v3"); obj->varAttrs->type = makeCharType(); declareObject(obj); exitBlock(); exitBlock(); printObject(symtab->program, 0); cleanSymTab(); return 0; }