int main(void) /* #define processor */ { int i, res, c; struct nlist *plist, *temp; char line[MAXLINE], word[MAXWORD], defnword[MAXDEFN], *name, *defn, *p, uchoice[MAXLINE]; while ((res = getword(word, MAXWORD)) != EOF) { if (res == '#') /* # received */ { if (getline1(line, MAXLINE) > 0) { p = line; while (*p == ' ' || *p == '\t') /* skip blanks */ p++; for (i = 0; *p != ' ' && *p != '\t' && *p != '\n'; p++, i++) /*see if 'define' */ word[i] = *p; word[i] = '\0'; if (strcmp(word, "define")) { putchar('#'); printf("%s", line); continue; } while (*p == ' ' || *p == '\t') /* skip blanks */ p++; for (i = 0; *p != ' ' && *p != '\t' && *p != '\n'; p++, i++) /*get name */ word[i] = *p; word[i] = '\0'; name = strdup1(word); } else /* if input not got properly */ return printf("error while processing...\n"); /* install */ { while(*p == ' ' || *p == '\t') /* skip blanks before defn */ p++; for (i = 0; *p != '\n' && *p != EOF; p++, i++) /* get defn */ defnword[i] = *p; defnword[i] = '\0'; defn = strdup1(defnword); install(name, defn); } } else if (isalpha(word[0])) if ((temp = lookup(word)) != NULL) printf("%s", temp->defn); else printf("%s", word); } return 0; }
/* addtree: add a node with w, at or below p */ struct tnode *addtree(struct tnode *p, char *w,int set,int len) { int cond; if (p == NULL) { p = talloc(); p->word = strdup1(w); p->count = 1; p->set=set; p->left = p->right = NULL; } else if ((cond = strcmp1(w, p->word,len)) == 0){ p->set=0; p->count++; } else if (cond ==-1) p->left = addtree(p->left, w,0,len); else if(cond==1) p->right = addtree(p->right, w,0,len); else if(cond==-2){ p->left=addtree(p->left,w,1,len); p->set=1; } else if(cond==2){ p->right=addtree(p->right,w,1,len); p->set=1; } return p; }
/* install: put (name, defn) in hashtab */ struct nlist *install(char *name, char *defn) { struct nlist *np; unsigned hashval; if ((np = lookup(name)) == NULL) /* not found */ { np = (struct nlist *) malloc(sizeof(*np)); if (np == NULL || (np->name = strdup1(name)) == NULL) return NULL; hashval = hash(name); np->next = hashtab[hashval]; hashtab[hashval] = np; } else /* already there */ free((void *) np->defn); /* free previous defn */ if ((np->defn = strdup1(defn)) == NULL) return NULL; return np; }
struct nlist *install(char * name, char * defn) { struct nlist *np; unsigned hashval; if((np = lookup(name)) == NULL) { np = (struct nlist *)malloc(sizeof(*np)); if(np == NULL || (np->name = strdup1(name)) == NULL) return NULL; hashval = hash(name); np->next = hashtab[hashval]; hashtab[hashval] = np; }else free((void *)np->defn); if((np->defn = strdup1(defn)) == NULL) return NULL; return np; }
struct tnode *addtree(struct tnode *p,char *w) { int cond; if (p == NULL) { p = talloc(); p->word = strdup1(w); p->count = 1; p->left = p->right = NULL; } else if ((cond = strcmp(w,p->word)) == 0) p->count++; else if (cond < 0) p->left = addtree(p->left,w); else p->right = addtree(p->right,w); return p; }
struct tnode *newaddtree(struct tnode *p, struct tnode *w) { int cond; if (p == NULL) { p = talloc(); p->word = strdup1(w->word); p->count = w->count; p->right =p->left= NULL; } else if (p->count==w->count) { p->left=newaddtree(p->left,w); } else if (w->count<p->count) p->left = newaddtree(p->left, w); else p->right = newaddtree(p->right, w); return p; }