tokens

1 2Source code for program “tokens”. File “tokens.c” 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

Views 117 Downloads 5 File size 63KB

Report DMCA / Copyright

DOWNLOAD FILE

Recommend stories

Citation preview

1 2Source

code for program “tokens”. File “tokens.c”

3

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

1

#include #include #include int int int char

Ignore = 0; Mincount = 0; Alpha = 0; MapAllowed[256];

typedef struct tnode { char *contents; int count; struct tnode *left; struct tnode *right; } TNODE; void treeprint(tree) TNODE *tree; { if (tree != NULL) //Si tree es diferente a nulo, entra a la función y pasa a la linea 23 { treeprint(tree->left); if (tree->count > Mincount) printf("%7d\t%s\n", tree->count, tree->contents); treeprint(tree->right); } } TNODE * install(string, tree) char *string; TNODE * tree; //espera dos variables y retorna un nodo { int cond; assert(string != NULL); if (tree == NULL) //Si tree es diferente a nulo, entra a la función y pasa a la linea 42 { {

if (tree = (TNODE *) calloc(1, sizeof(TNODE))) tree->contents = strdup(string); tree->count = 1;

} } Else //Si tree es nulo, entra a la función y pasa a la linea 50 { cond = strcmp(string, tree->contents);

3

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

2

if (cond < 0) //Si cond es menor a 0 tree se dirige a la izquierda tree->left = install(string, tree->left); else if (cond == 0) //Si cond es igual a 0 tree aumenta tree->count++; else tree->right = install(string, tree->right); } return(tree); //La funcion retorna una variable nodo tree } char * getword(ioptr) FILE *ioptr; { static char string[1024]; char *ptr = string; register int c; assert(ioptr != NULL); for (;;) { c = getc(ioptr); if (c == EOF) if (ptr == string) return(NULL); else break; if (!MapAllowed[c]) if (ptr == string) continue; else break; *ptr++ = MapAllowed[c]; } *ptr = '\0'; return(string); } void tokens(ioptr) FILE *ioptr; //primero declara, entra al while y luego sale //Primero la declaracion, no ingresa al while y luego sale { TNODE *root = NULL; char *s; assert(ioptr != NULL); while (s = getword(ioptr)) //en el ciclo s se mantiene mientra que sea igual a getword(ioptr) root = install(s, root); treeprint(root); } int main(argc, argv) int argc; char ** argv; { int c, errcnt = 0; extern char *optarg; while ((c = getopt(argc, argv, "ac:im:")) != EOF) switch(c) {

4

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 }

3

case 'a': Alpha = 0; break; case 'c': while (*optarg) { MapAllowed[*optarg] = *optarg; optarg++; } break; case 'i': Ignore = 1; break; case 'm': Mincount = atoi(optarg); break; default: errcnt++; } if (errcnt) { fprintf(stderr, "Usage: %s [-i] [-c chars] [-m count]\n", *argv); return(1); } for (c = 'a'; c