/* Function buildSymtab constructs the symbol * table by preorder traversal of the syntax tree */ void buildSymtab(TreeNode * syntaxTree) { globalScope = sc_create(NULL); sc_push(globalScope); insertIOFunc(); traverse(syntaxTree,insertNode,afterInsertNode); sc_pop(); if (TraceAnalyze) { fprintf(listing,"\nSymbol table:\n\n"); printSymTab(listing); } }
int main (int argc, char* argv[]) { /* --- main function */ int i, k = 0; /* loop variables, buffer */ char *s; /* to traverse options */ char **optarg = NULL; /* option argument */ char *fn_bc = NULL; /* name of classifier file */ char *fn_out = NULL; /* name of output file */ char *blank = NULL; /* blank */ char *fldsep = NULL; /* field separator */ char *recsep = NULL; /* record separator */ int flags = AS_ATT; /* table file write flags */ double lcorr = -DBL_MAX; /* Laplace correction value */ int dwnull = 0; /* distribute weight of null values */ int maxllh = 0; /* max. likelihood est. of variance */ int tplcnt = 1000; /* number of tuples to generate */ long seed; /* seed for random number generator */ int mode; /* classifier setup mode */ prgname = argv[0]; /* get program name for error msgs. */ seed = (long)time(NULL); /* and get a default seed value */ /* --- print startup/usage message --- */ if (argc > 1) { /* if arguments are given */ fprintf(stderr, "%s - %s\n", argv[0], DESCRIPTION); fprintf(stderr, VERSION); } /* print a startup message */ else { /* if no argument given */ printf("usage: %s [options] bcfile " "[-d|-h hdrfile] tabfile\n", argv[0]); printf("%s\n", DESCRIPTION); printf("%s\n", VERSION); printf("-n# number of tuples to generate " "(default: %d)\n", tplcnt); printf("-s# seed for random number generator " "(default: time)\n"); printf("-L# Laplace correction " "(default: as specified in classifier)\n"); printf("-v/V (do not) distribute tuple weight " "for null values\n"); printf("-m/M (do not) use maximum likelihood estimate " "for the variance\n"); printf("-a align fields (default: do not align)\n"); printf("-w do not write field names to the output file\n"); printf("-b/f/r# blank character, field and record separator\n" " (default: \" \", \" \", \"\\n\")\n"); printf("bcfile file containing classifier description\n"); printf("tabfile table file to write\n"); return 0; /* print a usage message */ } /* and abort the program */ /* --- evaluate arguments --- */ for (i = 1; i < argc; i++) { /* traverse arguments */ s = argv[i]; /* get option argument */ if (optarg) { *optarg = s; optarg = NULL; continue; } if ((*s == '-') && *++s) { /* -- if argument is an option */ while (*s) { /* traverse options */ switch (*s++) { /* evaluate option */ case 'n': tplcnt = (int)strtol(s, &s, 0); break; case 's': seed = strtol(s, &s, 0); break; case 'L': lcorr = strtod(s, &s); break; case 'v': dwnull = NBC_ALL; break; case 'V': dwnull |= NBC_DWNULL|NBC_ALL; break; case 'm': maxllh = NBC_ALL; break; case 'M': maxllh |= NBC_MAXLLH|NBC_ALL; break; case 'a': flags |= AS_ALIGN; break; case 'w': flags &= ~AS_ATT; break; case 'b': optarg = ␣ break; case 'f': optarg = &fldsep; break; case 'r': optarg = &recsep; break; default : error(E_OPTION, *--s); break; } /* set option variables */ if (!*s) break; /* if at end of string, abort loop */ if (optarg) { *optarg = s; optarg = NULL; break; } } } /* get option argument */ else { /* if argument is no option */ switch (k++) { /* evaluate non-option */ case 0: fn_bc = s; break; case 1: fn_out = s; break; default: error(E_ARGCNT); break; } /* note filenames */ } } if (optarg) error(E_OPTARG); /* check the option argument */ if (k != 2) error(E_ARGCNT); /* and the number of arguments */ if ((lcorr < 0) && (lcorr > -DBL_MAX)) error(E_NEGLC); /* check the Laplace correction */ if ((flags & AS_ATT) && (flags & AS_ALIGN)) flags |= AS_ALNHDR; /* set align to header flag */ /* --- read Bayes classifier --- */ scan = sc_create(fn_bc); /* create a scanner */ if (!scan) error((!fn_bc || !*fn_bc) ? E_NOMEM : E_FOPEN, fn_bc); attset = as_create("domains", att_delete); if (!attset) error(E_NOMEM); /* create an attribute set */ fprintf(stderr, "\nreading %s ... ", sc_fname(scan)); if ((sc_nexter(scan) < 0) /* start scanning (get first token) */ || (as_parse(attset, scan, AT_ALL) != 0) || (as_attcnt(attset) <= 0)) /* parse attribute set */ error(E_PARSE, sc_fname(scan)); if ((sc_token(scan) == T_ID) /* determine classifier type */ && (strcmp(sc_value(scan), "fbc") == 0)) fbc = fbc_parse(attset, scan); else nbc = nbc_parse(attset, scan); if ((!fbc && !nbc) /* parse the Bayes classifier */ || !sc_eof(scan)) /* and check for end of file */ error(E_PARSE, sc_fname(scan)); sc_delete(scan); scan = NULL; /* delete the scanner */ fprintf(stderr, "[%d attribute(s)] done.\n", as_attcnt(attset)); if ((lcorr >= 0) || dwnull || maxllh) { if (lcorr < 0) /* get the classifier's parameters */ lcorr = (fbc) ? fbc_lcorr(fbc) : nbc_lcorr(nbc); mode = (fbc) ? fbc_mode(fbc) : nbc_mode(nbc); if (dwnull) mode = (mode & ~NBC_DWNULL) | dwnull; if (maxllh) mode = (mode & ~NBC_MAXLLH) | maxllh; /* adapt the estimation parameters */ if (fbc) fbc_setup(fbc, mode, lcorr); else nbc_setup(nbc, mode, lcorr); } /* set up the classifier anew */ /* --- generate database --- */ if (fn_out && *fn_out) /* if an output file name is given, */ out = fopen(fn_out, "w"); /* open output file for writing */ else { /* if no output file name is given, */ out = stdout; fn_out = "<stdout>"; } /* write to std. output */ fprintf(stderr, "writing %s ... ", fn_out); if (!out) error(E_FOPEN, fn_out); if ((flags & AS_ATT) /* if to write a table header */ && (as_write(attset, out, flags) != 0)) error(E_FWRITE, fn_out); /* write the attributes names */ flags = AS_INST | (flags & ~AS_ATT); dseed(seed); /* init. random number generator */ for (i = tplcnt; --i >= 0;) { /* generate random tuples */ if (fbc) fbc_rand(fbc, drand); /* instantiate the */ else nbc_rand(nbc, drand); /* attribute set */ if (as_write(attset, out, flags) != 0) error(E_FWRITE,fn_out); /* write the generated tuple */ } /* to the output file */ if (out != stdout) { /* if not written to stdout */ i = fclose(out); out = NULL;/* close the output file */ if (i != 0) error(E_FWRITE, fn_out); } /* print a success message */ fprintf(stderr, "[%d tuple(s)] done.\n", tplcnt); /* --- clean up --- */ #ifndef NDEBUG if (fbc) fbc_delete(fbc, 1); /* delete full Bayes classifier */ if (nbc) nbc_delete(nbc, 1); /* or naive Bayes classifier */ #endif /* and underlying attribute set */ #ifdef STORAGE showmem("at end of program"); /* check memory usage */ #endif return 0; /* return 'ok' */ } /* main() */
int main(int argc,char** argv) { GrModule_MemInit(); GrModule_GcInit(); GrModule_IntInit(); GrModule_ConstsInit(); Scanner* sc=0; AstObject* root=0; GrModule* module=0; GrString* module_name=0; EgThread* thread=0; EgSframe* sf=0; int ret=0; if(argc!=2) { fprintf(stderr,"usage %s <filename>\n",argv[0]); exit(-1); } sc=sc_create(argv[1]); if(sc==NULL) { fprintf(stderr,"Open file %s failed\n",argv[1]); exit(-1); } yl_set_scanner(sc); ret=yyparse(); if(ret!=0) { fprintf(stderr,"parser <%s> failed\n",argv[1]); goto error; } AstPending_Clear(); root=parser_get_root(); module=Ast_ToModule(root); module_name=GrString_GcNewFlag("main",GRGC_HEAP_STATIC); if(module==NULL||module_name==NULL) { goto error; } GrModule_SetName(module,module_name); thread=EgThread_New(); if(thread==NULL) goto error; sf=EgSframe_NewFromModule(module); if(sf==NULL) goto error; EgThread_PushSframe(thread,sf); sf=NULL; GrGc_Disable(); EgThread_Run(thread); error: if(root) AstTree_Free(root); if(sc) sc_destory(sc); if(thread) EgThread_Delete(thread); // Gr_PrintAreaNum(); GrModule_GcExit(); // Gr_PrintAreaNum(); return 0; }
/* Procedure insertNode inserts * identifiers stored in t into * the symbol table */ static void insertNode( TreeNode * t) { switch (t->nodekind) { case StmtK: switch (t->kind.stmt) { case CompK: if (preserveLastScope) { preserveLastScope = FALSE; } else { Scope scope = sc_create(funcName); sc_push(scope); } t->attr.scope = sc_top(); break; default: break; } break; case ExpK: switch (t->kind.exp) { case IdK: case ArrIdK: case CallK: if (st_lookup(t->attr.name) == -1) /* not yet in table, error */ symbolError(t, "undelcared symbol"); else /* already in table, so ignore location, add line number of use only */ st_add_lineno(t->attr.name,t->lineno); break; default: break; } break; case DeclK: switch (t->kind.decl) { case FuncK: funcName = t->attr.name; if (st_lookup_top(funcName) >= 0) { /* already in table, so it's an error */ symbolError(t,"function already declared"); break; } st_insert(funcName,t->lineno,addLocation(),t); sc_push(sc_create(funcName)); preserveLastScope = TRUE; switch (t->child[0]->attr.type) { case INT: t->type = Integer; break; case VOID: default: t->type = Void; break; } break; case VarK: case ArrVarK: { char *name; if (t->child[0]->attr.type == VOID) { symbolError(t,"variable should have non-void type"); break; } if (t->kind.decl == VarK) { name = t->attr.name; t->type = Integer; } else { name = t->attr.arr.name; t->type = IntegerArray; } if (st_lookup_top(name) < 0) st_insert(name,t->lineno,addLocation(),t); else symbolError(t,"symbol already declared for current scope"); } break; default: break; } break; case ParamK: if (t->child[0]->attr.type == VOID) symbolError(t->child[0],"void type parameter is not allowed"); if (st_lookup(t->attr.name) == -1) { st_insert(t->attr.name,t->lineno,addLocation(),t); if (t->kind.param == NonArrParamK) t->type = Integer; else symbolError(t,"symbol already declared for current scope"); } break; default: break; } }
int main(int argc,char** argv) { if(argc<2) { printf("usage %s [filename]\n",argv[0]); exit(0); } struct scanner* sc=sc_create(argv[1]); int token; int i=0; while(1) { i++; if(i%5==0) { printf("\n"); } token=sc_next_token(sc); if(token==TOKEN_EOF) { break; } if(token==TOKEN_ERR) { goto err; } if(token==TOKEN_ID) { if(symbol_type(sc_token_string(sc))==TOKEN_ID) { printf("{variable,%s} ",sc_token_string(sc)); } else { printf("{keyword,%s} ",sc_token_string(sc)); } continue; } if(token==TOKEN_ANNO) { continue; } if(token==TOKEN_WS) { continue; } if(token==TOKEN_NEWLINE) { printf("{newline} "); continue; } printf("{%s,%s} ",token_name(token),sc_token_string(sc)); }; printf("\n"); return 0; err: printf("err token at line %d\n",sc->s_line); return -1; }