extern struct InstrSeq *FuncDec(char *name, struct InstrSeq *code) { char nname[100]; snprintf(nname, 100, "_%s", name); struct InstrSeq *fcode; fcode = GenInstr(nname, NULL, NULL, NULL, NULL); fcode = AppendSeq(fcode, code); fcode = AppendSeq(fcode, GenInstr(NULL, "jr", "$ra", NULL, NULL)); DestroySymTab(locTable); locTable = NULL; return fcode; }
int main(int argc, char * argv[]) { char *testFileName; FILE *fd; struct SymTab *theTable, *firstCopy, *secondCopy; struct SymEntry *anEntry; struct Attributes *anAttr; char buffer[16]; int val1, val2; if (argc != 2) { fprintf(stderr,"usage: SymTabDriver test-data-file\n"); exit(1); } testFileName = argv[1]; fd = fopen(testFileName,"r"); if (!fd) ErrorExit("Can't open input file.\n"); if (!(theTable = CreateSymTab(5))) ErrorExit("Failed to alloc first table.\n"); /* Read lines consisting of a name string and an integer from std input. If name already present increment value2, if new allocate storage for attribute structure and fill in fields. */ while (fscanf(fd,"%15s %d",buffer,&val1) != EOF) { printf("Find: %15s ",buffer); (FindName(theTable,buffer)) ? fprintf(stdout," Present -") : fprintf(stdout," Not Present -"); if (EnterName(theTable,buffer,&anEntry)) { fprintf(stdout," Enter: Present: %15s\n",GetName(anEntry)); anAttr = (struct Attributes *) GetAttr(anEntry); anAttr->value2++; anAttr->value1 = MAX(((struct Attributes *) GetAttr(anEntry))->value1,val1); } else { fprintf(stdout," Enter: Entered: %15s\n",GetName(anEntry)); anAttr = malloc(sizeof(struct Attributes)); anAttr->value1 = val1; anAttr->value2 = 1; SetAttr(anEntry,anAttr); } } fprintf(stdout,"\nContents of Original Table\n"); DisplayTable(theTable); DisplayStatistics(theTable); if (!(firstCopy = CopyTable(theTable,1))) ErrorExit("Failed to alloc first copy table.\n"); DestroySymTab(theTable); fprintf(stdout,"\nContents of First Copy Table\n"); DisplayTable(firstCopy); DisplayStatistics(firstCopy); if (!(secondCopy = CopyTable(firstCopy,100))) ErrorExit("Failed to alloc first copy table.\n"); DestroySymTab(firstCopy); fprintf(stdout,"\nContents of Second Copy Table\n"); DisplayTable(secondCopy); DisplayStatistics(secondCopy); VerifyCounts(secondCopy); FreeAllAttr(secondCopy); DestroySymTab(secondCopy); return 0; }