diff --git a/choose.c b/choose.c index 378f37f..370443f 100644 --- a/choose.c +++ b/choose.c @@ -15,52 +15,52 @@ mpz_t oracle; mpz_t nextOracle; void choose_setIterationLong(unsigned long i) { - iteration = i; - chooseMode = MODE_LONG; + iteration = i; + chooseMode = MODE_LONG; } void choose_setIterationFile(FILE *file) { - mpz_inp_str(oracle,file,10); - fclose(file); - chooseMode = MODE_MP; + mpz_inp_str(oracle, file, 10); + fclose(file); + chooseMode = MODE_MP; } void choose_setIterationString(char *string) { - if(mpz_set_str(oracle,string,10)) { - fprintf(stderr,"error: iteration string is not a number\n"); - exit(-1); - } - if(!strcmp(string,"0")) { /* gmp doesn't like 0? */ - iteration = 0L; - chooseMode = MODE_LONG; - } else if(mpz_fits_ulong_p(oracle)) { - iteration = mpz_get_ui(oracle); - mpz_clear(oracle); - chooseMode = MODE_LONG; - } else { - mpz_init(nextOracle); - chooseMode = MODE_MP; - } + if (mpz_set_str(oracle, string, 10)) { + fprintf(stderr, "error: iteration string is not a number\n"); + exit(-1); + } + if (!strcmp(string, "0")) { /* gmp doesn't like 0? */ + iteration = 0L; + chooseMode = MODE_LONG; + } else if (mpz_fits_ulong_p(oracle)) { + iteration = mpz_get_ui(oracle); + mpz_clear(oracle); + chooseMode = MODE_LONG; + } else { + mpz_init(nextOracle); + chooseMode = MODE_MP; + } } unsigned long choose_next(unsigned long nChoices) { - unsigned long choice; - if(nChoices == 0) { - fprintf(stderr,"zero choices!\n"); - } - switch(chooseMode) { - case MODE_LONG: - choice = iteration % nChoices; - iteration /= nChoices; - break; - case MODE_MP: - choice = mpz_fdiv_q_ui(nextOracle,oracle,nChoices); - mpz_swap(oracle,nextOracle); - break; - case MODE_RANDOM: - default: - choice = random() % nChoices; - break; - } - return choice; + unsigned long choice; + if (nChoices == 0) { + fprintf(stderr, "zero choices!\n"); + } + switch (chooseMode) { + case MODE_LONG: + choice = iteration % nChoices; + iteration /= nChoices; + break; + case MODE_MP: + choice = mpz_fdiv_q_ui(nextOracle, oracle, nChoices); + mpz_swap(oracle, nextOracle); + break; + case MODE_RANDOM: + default: + choice = random() % nChoices; + break; + } + return choice; } diff --git a/dict.c b/dict.c index 2adc19e..619e52d 100644 --- a/dict.c +++ b/dict.c @@ -14,198 +14,198 @@ /* create a new dictionary entry (key + value) */ DICT_ENTRY *dictEntry_new(char *key, void *value) { - DICT_ENTRY *nde = (DICT_ENTRY *)malloc(sizeof(DICT_ENTRY)); - nde->key = strdup(key); - nde->value = value; - return nde; + DICT_ENTRY *nde = (DICT_ENTRY *) malloc(sizeof(DICT_ENTRY)); + nde->key = strdup(key); + nde->value = value; + return nde; } void dictEntry_free(DICT_ENTRY *entry) { - free(entry->key); - free(entry); + free(entry->key); + free(entry); } void dictEntry_freeData(DICT_ENTRY *entry, Destructor d) { - d(entry->value); - free(entry->key); - free(entry); + d(entry->value); + free(entry->key); + free(entry); } DICT *dict_new() { - DICT *nd = calloc(1,sizeof(DICT)); - nd->entries = list_new(); - return nd; + DICT *nd = calloc(1, sizeof(DICT)); + nd->entries = list_new(); + return nd; } void dict_free(DICT *d) { - /* recursively free a DICT */ - int i; - long il, len; - for(i = 0; i < 26; i++) { - if(d->children[i]) { - dict_free(d->children[i]); - } - } - len = list_length(d->entries); - for(il = 0; il < len; il++) { - DICT_ENTRY *e = (DICT_ENTRY *)list_get(d->entries,il); - dictEntry_free(e); - } - list_free(d->entries); - free(d); + /* recursively free a DICT */ + int i; + long il, len; + for (i = 0; i < 26; i++) { + if (d->children[i]) { + dict_free(d->children[i]); + } + } + len = list_length(d->entries); + for (il = 0; il < len; il++) { + DICT_ENTRY *e = (DICT_ENTRY *) list_get(d->entries, il); + dictEntry_free(e); + } + list_free(d->entries); + free(d); } void dict_freeValues(DICT *d, Destructor destructo) { - /* recursively free a DICT */ - int i; - long il, len; - for(i = 0; i < 26; i++) { - if(d->children[i]) { - dict_freeValues(d->children[i], destructo); - } - } - len = list_length(d->entries); - for(il = 0; il < len; il++) { - DICT_ENTRY *e = (DICT_ENTRY *)list_get(d->entries,il); - dictEntry_freeData(e, destructo); - } - list_free(d->entries); - free(d); + /* recursively free a DICT */ + int i; + long il, len; + for (i = 0; i < 26; i++) { + if (d->children[i]) { + dict_freeValues(d->children[i], destructo); + } + } + len = list_length(d->entries); + for (il = 0; il < len; il++) { + DICT_ENTRY *e = (DICT_ENTRY *) list_get(d->entries, il); + dictEntry_freeData(e, destructo); + } + list_free(d->entries); + free(d); } void _dict_put(DICT *d, char *key, char *orig_key, void *data) { - char *kp = key; - int ix; + char *kp = key; + int ix; #ifdef DICT_DEBUG - printf("putting %s\n", key); /* debug */ + printf("putting %s\n", key); /* debug */ #endif - /* if the key is empty, we've found or created the appropriate - leaf node and now just need to append an entry to it */ - if(!*kp) { - DICT_ENTRY *newEntry; - newEntry = dictEntry_new(orig_key, data); + /* if the key is empty, we've found or created the appropriate + leaf node and now just need to append an entry to it */ + if (!*kp) { + DICT_ENTRY *newEntry; + newEntry = dictEntry_new(orig_key, data); #ifdef DICT_DEBUG - printf("adding new entry %s (%p)\n", orig_key, newEntry); - fflush(stdout); + printf("adding new entry %s (%p)\n", orig_key, newEntry); + fflush(stdout); #endif - list_add(d->entries, newEntry); - return; - } - /* otherwise we need to skip non-alpha chars */ - while(!isalpha(*kp)) { - kp++; - if(!*kp) { /* last char is non-alpha */ + list_add(d->entries, newEntry); + return; + } + /* otherwise we need to skip non-alpha chars */ + while (!isalpha(*kp)) { + kp++; + if (!*kp) { /* last char is non-alpha */ #ifdef DICT_DEBUG - printf("last char of %s is non-alpha\n",orig_key); + printf("last char of %s is non-alpha\n",orig_key); #endif - /* add to this node and quit */ - _dict_put(d, kp, orig_key, data); - return; - } - } - ix = tolower(*kp) - 'a'; - /* if the appropriate subtrie does not exist, create it */ - if(!d->children[ix]) { + /* add to this node and quit */ + _dict_put(d, kp, orig_key, data); + return; + } + } + ix = tolower(*kp) - 'a'; + /* if the appropriate subtrie does not exist, create it */ + if (!d->children[ix]) { #ifdef DICT_DEBUG - printf("creating subtree for %c\n",*kp); /* debug */ + printf("creating subtree for %c\n",*kp); /* debug */ #endif - d->children[ix] = dict_new(); - } - /* recurse */ - _dict_put(d->children[ix],kp+1,orig_key,data); + d->children[ix] = dict_new(); + } + /* recurse */ + _dict_put(d->children[ix], kp + 1, orig_key, data); } void dict_put(DICT *d, char *key, void *data) { - _dict_put(d, key, key, data); + _dict_put(d, key, key, data); } void *_dict_get(DICT *d, char *key, char *orig_key) { - char *kp = key; - int ix; - /* skip nonalphas */ - while(*kp && !isalpha(*kp)) { - kp++; - } - if(!*kp) { - /* do a linear search of this node's entries */ - int i, len; - len = list_length(d->entries); + char *kp = key; + int ix; + /* skip nonalphas */ + while (*kp && !isalpha(*kp)) { + kp++; + } + if (!*kp) { + /* do a linear search of this node's entries */ + int i, len; + len = list_length(d->entries); #ifdef DICT_DEBUG - printf("searching %d entries for %s\n",len,orig_key); /* debug */ + printf("searching %d entries for %s\n",len,orig_key); /* debug */ #endif - /* search from the end, so that new values shadow old */ - for(i = len-1; i >= 0; i--) { - DICT_ENTRY *de = (DICT_ENTRY *)list_get(d->entries,i); - if(!strcmp(orig_key, de->key)) { - return de->value; - } - } - return NULL; - } + /* search from the end, so that new values shadow old */ + for (i = len - 1; i >= 0; i--) { + DICT_ENTRY *de = (DICT_ENTRY *) list_get(d->entries, i); + if (!strcmp(orig_key, de->key)) { + return de->value; + } + } + return NULL; + } #ifdef DICT_DEBUG - printf("getting %s\n",kp); /* debug */ + printf("getting %s\n",kp); /* debug */ #endif - ix = tolower(*kp) - 'a'; - if(!d->children[ix]) - return NULL; - return _dict_get(d->children[ix], kp+1, orig_key); + ix = tolower(*kp) - 'a'; + if (!d->children[ix]) + return NULL; + return _dict_get(d->children[ix], kp + 1, orig_key); } LIST *_dict_getAll(DICT *d, char *key, char *orig_key) { - char *kp = key; - int ix; - /* skip nonalphas */ - while(*kp && !isalpha(*kp)) { - kp++; - } + char *kp = key; + int ix; + /* skip nonalphas */ + while (*kp && !isalpha(*kp)) { + kp++; + } #ifdef DICT_DEBUG - printf("getting %s\n",kp); /* debug */ + printf("getting %s\n",kp); /* debug */ #endif - if(!*kp) { - LIST *all; - int i, len; - all = list_new(); - len = list_length(d->entries); - for(i = 0; i < len; i++) { - DICT_ENTRY *de = (DICT_ENTRY *)list_get(d->entries,i); - list_add(all, de->value); - } - return all; - } - ix = tolower(*kp) - 'a'; - if(!d->children[ix]) - return NULL; - return _dict_get(d->children[ix], kp+1, orig_key); + if (!*kp) { + LIST *all; + int i, len; + all = list_new(); + len = list_length(d->entries); + for (i = 0; i < len; i++) { + DICT_ENTRY *de = (DICT_ENTRY *) list_get(d->entries, i); + list_add(all, de->value); + } + return all; + } + ix = tolower(*kp) - 'a'; + if (!d->children[ix]) + return NULL; + return _dict_get(d->children[ix], kp + 1, orig_key); } void *dict_get(DICT *d, char *key) { - return _dict_get(d, key, key); + return _dict_get(d, key, key); } LIST *dict_getAll(DICT *d, char *key) { - return _dict_getAll(d, key, key); + return _dict_getAll(d, key, key); } void dict_traverse(DICT *d, TraversalAction action, void *action_arg) { - int i; - if(!d) - return; - if(d->entries) { - int len; - len = list_length(d->entries); + int i; + if (!d) + return; + if (d->entries) { + int len; + len = list_length(d->entries); #ifdef DICT_DEBUG - printf("%d entries\n", len); + printf("%d entries\n", len); #endif - for(i = 0; i < len; i++) { - DICT_ENTRY *de = (DICT_ENTRY *)list_get(d->entries,i); + for (i = 0; i < len; i++) { + DICT_ENTRY *de = (DICT_ENTRY *) list_get(d->entries, i); #ifdef DICT_DEBUG - printf("entry %d = %p\n", i, de); + printf("entry %d = %p\n", i, de); #endif - action(de->key, de->value, action_arg); - } - } - for(i = 0; i < 26; i++) - if(d->children[i]) - dict_traverse(d->children[i], action, action_arg); + action(de->key, de->value, action_arg); + } + } + for (i = 0; i < 26; i++) + if (d->children[i]) + dict_traverse(d->children[i], action, action_arg); } diff --git a/dict.h b/dict.h index 8dd305b..48c5e5f 100644 --- a/dict.h +++ b/dict.h @@ -13,13 +13,13 @@ typedef void (*TraversalAction)(char *key, void *value, void *arg); typedef struct _dict_entry { - char *key; - void *value; + char *key; + void *value; } DICT_ENTRY; typedef struct _dict_node { - LIST *entries; /* entries (of type DICT_ENTRY) */ - struct _dict_node *children[26]; + LIST *entries; /* entries (of type DICT_ENTRY) */ + struct _dict_node *children[26]; } DICT; extern DICT *dict_new(); diff --git a/grambit.c b/grambit.c index f2f7afd..25c1936 100644 --- a/grambit.c +++ b/grambit.c @@ -8,270 +8,283 @@ static GRAMBIT *grambit_new(int type); /* create a new grammatical bit */ GRAMBIT *grambit_new(int t) { - GRAMBIT *g = (GRAMBIT *)calloc(1, sizeof(GRAMBIT)); - g->type = t; - g->l = NULL; - g->choices = list_new(); - g->min_x = 1; - g->max_x = 1; - g->source = NULL; - g->trans = NULL; - return g; + GRAMBIT *g = (GRAMBIT *) calloc(1, sizeof(GRAMBIT)); + g->type = t; + g->l = NULL; + g->choices = list_new(); + g->min_x = 1; + g->max_x = 1; + g->source = NULL; + g->trans = NULL; + return g; } void choices_free(LIST *choices) { - /* each choice is a list of terms, i.e. grambits */ - list_freeData(choices, (Destructor)grambit_free); + /* each choice is a list of terms, i.e. grambits */ + list_freeData(choices, (Destructor) grambit_free); } /* destroy a grammatical bit */ void grambit_free(GRAMBIT *g) { - free(g->l); - if(g->choices) { - list_freeData(g->choices, (Destructor)choices_free); - } - if(g->rx_rx) free(g->rx_rx); - if(g->rx_rep) free(g->rx_rep); - if(g->source) grambit_free(g->source); - if(g->trans) grambit_free(g->trans); - free(g); + free(g->l); + if (g->choices) { + list_freeData(g->choices, (Destructor) choices_free); + } + if (g->rx_rx) + free(g->rx_rx); + if (g->rx_rep) + free(g->rx_rep); + if (g->source) + grambit_free(g->source); + if (g->trans) + grambit_free(g->trans); + free(g); } /* create a new literal */ GRAMBIT *literal_new(char *text) { - GRAMBIT *ng = grambit_new(LITERAL_T); - ng->l = strdup(text); - return ng; + GRAMBIT *ng = grambit_new(LITERAL_T); + ng->l = strdup(text); + return ng; } /* create a new label */ GRAMBIT *label_new(char *text) { - GRAMBIT *ng = literal_new(text); - ng->type = LABEL_T; - return ng; + GRAMBIT *ng = literal_new(text); + ng->type = LABEL_T; + return ng; } /* create a new rule with the given label and choices. - choices is a list of lists because each choice is a sequence. - if choices are NULL, create a new empty list so that you - can add choices with rule_addChoice */ + choices is a list of lists because each choice is a sequence. + if choices are NULL, create a new empty list so that you + can add choices with rule_addChoice */ GRAMBIT *rule_new(char *label, LIST *choices, int scope) { - GRAMBIT *ng = grambit_new(RULE_T); - if(label) { - ng->l = strdup(label); - } - if(choices) { - list_free(ng->choices); - ng->choices = choices; - } - ng->scope = scope; - return ng; + GRAMBIT *ng = grambit_new(RULE_T); + if (label) { + ng->l = strdup(label); + } + if (choices) { + list_free(ng->choices); + ng->choices = choices; + } + ng->scope = scope; + return ng; } /* create a new rule with the given label, arguments and choices. - choices is a list of lists because each choice is a sequence. - if choices are NULL, create a new empty list so that you - can add choices with rule_addChoice */ -GRAMBIT *rule_newWithArguments(char *label, LIST *labels, LIST *choices, int scope) { - GRAMBIT *nr; - LIST *choice, *terms; - int i, len; - - /* the rhs of this type of rule is a single choice consisting of - 1. positional variable bindings, followed by - 2. an anon choice wrapping the original rhs */ - terms = list_new(); /* single choice for rhs */ - choice = list_addToNew(terms); /* rhs */ - - /* for each positional argument in the lhs */ - len = list_length(labels); - for(i = 0; i < len; i++) { - GRAMBIT *binding; - char *argName, *varName; - - argName = (char *)list_get(labels,i); /* positional arg name (e.g., "foo") */ - varName = calloc(MAX_VAR_LENGTH,sizeof(char)); - sprintf(varName,"_%d",i+1); /* positional var name (e.g., "_2") */ - - /* create a binding equivalent to e.g., {foo=_2} */ - binding = binding_new(argName,label_new(varName),LOCAL_SCOPE); - free(varName); - - /* append this binding to the head of the rule's rhs */ - list_add(terms, binding); - } - - /* append the original rhs of the rule as an anon choice to the tail of the new rule's rhs */ - list_add(terms,choice_new(choices)); - list_add(choice,terms); - - nr = rule_new(label, choice, LOCAL_SCOPE); - - free(label); + choices is a list of lists because each choice is a sequence. + if choices are NULL, create a new empty list so that you + can add choices with rule_addChoice */ +GRAMBIT *rule_newWithArguments(char *label, LIST *labels, LIST *choices, + int scope) { + GRAMBIT *nr; + LIST *choice, *terms; + int i, len; + + /* the rhs of this type of rule is a single choice consisting of + 1. positional variable bindings, followed by + 2. an anon choice wrapping the original rhs */ + terms = list_new(); /* single choice for rhs */ + choice = list_addToNew(terms); /* rhs */ + + /* for each positional argument in the lhs */ + len = list_length(labels); + for (i = 0; i < len; i++) { + GRAMBIT *binding; + char *argName, *varName; + + argName = (char *) list_get(labels, i); /* positional arg name (e.g., "foo") */ + varName = calloc(MAX_VAR_LENGTH, sizeof(char)); + sprintf(varName, "_%d", i + 1); /* positional var name (e.g., "_2") */ + + /* create a binding equivalent to e.g., {foo=_2} */ + binding = binding_new(argName, label_new(varName), LOCAL_SCOPE); + free(varName); + + /* append this binding to the head of the rule's rhs */ + list_add(terms, binding); + } + + /* append the original rhs of the rule as an anon choice to the tail of the new rule's rhs */ + list_add(terms, choice_new(choices)); + list_add(choice, terms); - return nr; + nr = rule_new(label, choice, LOCAL_SCOPE); + + free(label); + + return nr; } GRAMBIT *call_new(char *label, LIST *arguments) { - LIST *choices = list_new(); - LIST *assignments = list_new(); - int len=list_length(arguments), i; - /* rewrite the choices as a single choice consisting of a list - of assignments to positional variables */ - for(i = 0; i < len; i++) { - LIST *rhs = list_new(); - GRAMBIT *ass; - char *varName = calloc(MAX_VAR_LENGTH,sizeof(char)); - sprintf(varName,"_%d",i+1); /* lhs of assignment */ - list_add(rhs,list_get(arguments,i)); /* rhs of assignment */ - ass = assignment_new(varName, rhs, LOCAL_SCOPE); - list_add(assignments, ass); - } - /* now add the label to the *end* of the assignments */ - list_add(assignments,label_new(label)); - free(label); - /* now construct the single choice */ - list_add(choices,assignments); - return choice_new(choices); + LIST *choices = list_new(); + LIST *assignments = list_new(); + int len = list_length(arguments), i; + /* rewrite the choices as a single choice consisting of a list + of assignments to positional variables */ + for (i = 0; i < len; i++) { + LIST *rhs = list_new(); + GRAMBIT *ass; + char *varName = calloc(MAX_VAR_LENGTH, sizeof(char)); + sprintf(varName, "_%d", i + 1); /* lhs of assignment */ + list_add(rhs, list_get(arguments, i)); /* rhs of assignment */ + ass = assignment_new(varName, rhs, LOCAL_SCOPE); + list_add(assignments, ass); + } + /* now add the label to the *end* of the assignments */ + list_add(assignments, label_new(label)); + free(label); + /* now construct the single choice */ + list_add(choices, assignments); + return choice_new(choices); } /* copy the given grambit */ GRAMBIT *grambit_copy(GRAMBIT *g) { - int i,j; - GRAMBIT *ng = grambit_new(g->type); - ng->scope = g->scope; - if(g->l) { - ng->l = strdup(g->l); - } - if(g->choices) { - for(i = 0; i < list_length(g->choices); i++) { - LIST *cs = (LIST *)list_get(g->choices,i); - LIST *nc = list_new(); - for(j = 0; j < list_length(cs); j++) { - list_add(nc, grambit_copy((GRAMBIT *)list_get(cs, j))); - } - list_add(ng->choices, nc); - } - } - ng->min_x = g->min_x; - ng->max_x = g->max_x; - if(g->rx_rx) { ng->rx_rx = strdup(g->rx_rx); } - if(g->rx_rep) { ng->rx_rep = strdup(g->rx_rep); } - if(g->source) { ng->source = grambit_copy(g->source); } - if(g->trans) { ng->trans = grambit_copy(g->trans); } - return ng; + int i, j; + GRAMBIT *ng = grambit_new(g->type); + ng->scope = g->scope; + if (g->l) { + ng->l = strdup(g->l); + } + if (g->choices) { + for (i = 0; i < list_length(g->choices); i++) { + LIST *cs = (LIST *) list_get(g->choices, i); + LIST *nc = list_new(); + for (j = 0; j < list_length(cs); j++) { + list_add(nc, grambit_copy((GRAMBIT *) list_get(cs, j))); + } + list_add(ng->choices, nc); + } + } + ng->min_x = g->min_x; + ng->max_x = g->max_x; + if (g->rx_rx) { + ng->rx_rx = strdup(g->rx_rx); + } + if (g->rx_rep) { + ng->rx_rep = strdup(g->rx_rep); + } + if (g->source) { + ng->source = grambit_copy(g->source); + } + if (g->trans) { + ng->trans = grambit_copy(g->trans); + } + return ng; } /* create a new assignment with the given label and choices. - choices is a list of lists because each choice is a sequence. - if choices are NULL, create a new empty list so that you - can add choices with assignment_addChoice */ + choices is a list of lists because each choice is a sequence. + if choices are NULL, create a new empty list so that you + can add choices with assignment_addChoice */ GRAMBIT *assignment_new(char *label, LIST *choices, int scope) { - GRAMBIT *ng = rule_new(label, choices, scope); - ng->type = ASSIGNMENT_T; - return ng; + GRAMBIT *ng = rule_new(label, choices, scope); + ng->type = ASSIGNMENT_T; + return ng; } /* create a new assignment of a label to a single choice. */ GRAMBIT *binding_new(char *label, GRAMBIT *choice, int scope) { - return assignment_new(label, list_addToNew(list_addToNew(choice)), scope); + return assignment_new(label, list_addToNew(list_addToNew(choice)), scope); } GRAMBIT *rxsub_new(char *rx, char *rep) { - GRAMBIT *ng = grambit_new(RXSUB_T); - ng->rx_rx = strdup(rx); - ng->rx_rep = strdup(rep); - return ng; + GRAMBIT *ng = grambit_new(RXSUB_T); + ng->rx_rx = strdup(rx); + ng->rx_rep = strdup(rep); + return ng; } GRAMBIT *mapping_new(char *rx, GRAMBIT *g) { - GRAMBIT *ng = grambit_new(MAPPING_T); - ng->rx_rx = strdup(rx); - ng->trans = g; - return ng; + GRAMBIT *ng = grambit_new(MAPPING_T); + ng->rx_rx = strdup(rx); + ng->trans = g; + return ng; } GRAMBIT *rxmatch_new(char *rx, GRAMBIT *g) { - GRAMBIT *ng = grambit_new(RXMATCH_T); - ng->rx_rx = strdup(rx); - ng->trans = g; - return ng; + GRAMBIT *ng = grambit_new(RXMATCH_T); + ng->rx_rx = strdup(rx); + ng->trans = g; + return ng; } GRAMBIT *trans_new(GRAMBIT *s, GRAMBIT *t) { - GRAMBIT *ng = grambit_new(TRANS_T); - ng->source = s; - ng->trans = t; - return ng; + GRAMBIT *ng = grambit_new(TRANS_T); + ng->source = s; + ng->trans = t; + return ng; } GRAMBIT *choice_new(LIST *choices) { - GRAMBIT *ng = rule_new(NULL, choices, 0); - ng->type = CHOICE_T; - return ng; + GRAMBIT *ng = rule_new(NULL, choices, 0); + ng->type = CHOICE_T; + return ng; } void grambit_addChoice(GRAMBIT *g, LIST *choice) { - list_add(g->choices, choice); + list_add(g->choices, choice); } /* return a rule's label */ char *rule_getLabel(RULE *g) { - return g->l; + return g->l; } /* return a rule's choices (a list of lists) */ LIST *rule_getChoices(RULE *g) { - return g->choices; + return g->choices; } /* append a string to the end of a literal */ void literal_append(GRAMBIT *g, char *suffix) { - char *ns = gstr_cat(g->l,suffix); - free(g->l); - g->l = ns; + char *ns = gstr_cat(g->l, suffix); + free(g->l); + g->l = ns; } /* for debugging */ void grambit_print(GRAMBIT *g, FILE *fp) { - long i, j; - char *l = g->l ? g->l : "(null)"; - fprintf(fp, "["); - switch(g->type) { - case LABEL_T: - fprintf(fp, "label: %s]", l); - return; - case LITERAL_T: - fprintf(fp, "literal: \"%s\"]", l); - return; - case RULE_T: - fprintf(fp, "rule: %s -> ", l); - break; - case ASSIGNMENT_T: - fprintf(fp, "assignment: %s = ", l); - break; - case CHOICE_T: - fprintf(fp, "choice:"); - break; - case RXSUB_T: - fprintf(fp, "rxsub:"); - break; - case TRANS_T: - fprintf(fp, "trans:"); - break; - default: - fprintf(fp, "unknown"); - return; - } - fflush(fp); - for(i = 0; i < list_length(g->choices); i++) { - LIST *cs = (LIST *)list_get(g->choices,i); - for(j = 0; j < list_length(cs); j++) { - grambit_print((GRAMBIT *)list_get(cs, j), fp); - fprintf(fp, ", "); + long i, j; + char *l = g->l ? g->l : "(null)"; + fprintf(fp, "["); + switch (g->type) { + case LABEL_T: + fprintf(fp, "label: %s]", l); + return; + case LITERAL_T: + fprintf(fp, "literal: \"%s\"]", l); + return; + case RULE_T: + fprintf(fp, "rule: %s -> ", l); + break; + case ASSIGNMENT_T: + fprintf(fp, "assignment: %s = ", l); + break; + case CHOICE_T: + fprintf(fp, "choice:"); + break; + case RXSUB_T: + fprintf(fp, "rxsub:"); + break; + case TRANS_T: + fprintf(fp, "trans:"); + break; + default: + fprintf(fp, "unknown"); + return; + } + fflush(fp); + for (i = 0; i < list_length(g->choices); i++) { + LIST *cs = (LIST *) list_get(g->choices, i); + for (j = 0; j < list_length(cs); j++) { + grambit_print((GRAMBIT *) list_get(cs, j), fp); + fprintf(fp, ", "); + } + fprintf(fp, " | "); } - fprintf(fp, " | "); - } - fprintf(fp, "]"); - fflush(fp); + fprintf(fp, "]"); + fflush(fp); } diff --git a/grambit.h b/grambit.h index d798eb9..f173db6 100644 --- a/grambit.h +++ b/grambit.h @@ -24,17 +24,17 @@ #define GLOBAL_SCOPE 2 /* first ancestor grammar without a parent */ typedef struct _grambit { - int type; /* what type of grambit */ - char *l; /* label or literal */ - int scope; /* scope of rule/assignment */ - LIST *choices; /* choices (for choice, rule or assignment) */ - int min_x; /* minimum number of times (for label, literal, or choice) */ - int max_x; /* max " " " ... */ - char *rx_rx; /* regex to replace */ - char *rx_rep; /* string to replace it with */ - /* rx_rx is also used for simple mappings */ - struct _grambit *source; /* source grambit for transformation */ - struct _grambit *trans; /* trans grambit for transformation or mapping */ + int type; /* what type of grambit */ + char *l; /* label or literal */ + int scope; /* scope of rule/assignment */ + LIST *choices; /* choices (for choice, rule or assignment) */ + int min_x; /* minimum number of times (for label, literal, or choice) */ + int max_x; /* max " " " ... */ + char *rx_rx; /* regex to replace */ + char *rx_rep; /* string to replace it with */ + /* rx_rx is also used for simple mappings */ + struct _grambit *source; /* source grambit for transformation */ + struct _grambit *trans; /* trans grambit for transformation or mapping */ } GRAMBIT; /* synonyms for grambit for readability of code */ @@ -71,7 +71,7 @@ extern void literal_append(GRAMBIT *, char *); /* append a string to the end of extern void grambit_print(GRAMBIT *, FILE *); /* for debugging */ /* maximum number of digits in number of positional arguments. - 8 is ridiculously high allowing for 99999999 positional arguments. */ + 8 is ridiculously high allowing for 99999999 positional arguments. */ #define MAX_VAR_LENGTH 8 #endif diff --git a/grammar.c b/grammar.c index 86df0f1..3dfe12c 100644 --- a/grammar.c +++ b/grammar.c @@ -13,271 +13,273 @@ int stackDepth = 0; GRAMMAR *grammar_binding(GRAMMAR *gram, char *label); GRAMMAR *grammar_new() { - GRAMMAR *ng = calloc(1,sizeof(GRAMMAR)); - ng->parent = NULL; - ng->contents = dict_new(); - return ng; + GRAMMAR *ng = calloc(1, sizeof(GRAMMAR)); + ng->parent = NULL; + ng->contents = dict_new(); + return ng; } /* destroy a grammar */ void grammar_free(GRAMMAR *g) { - dict_freeValues(g->contents,(Destructor)grambit_free); - free(g); + dict_freeValues(g->contents, (Destructor) grambit_free); + free(g); } static char *getLabel(char *packagedLabel); void grammar_add(GRAMMAR *g, RULE *r) { - char *label = rule_getLabel(r); - /* if the scope is lexical, add to this grammar */ - if(r->scope == LOCAL_SCOPE) { - dict_put(g->contents,label,r); - } else if(r->scope == NON_LOCAL_SCOPE) { - /* otherwise walk up the grammar stack */ - GRAMMAR *frame = NULL; - frame = grammar_binding(g, label); - if(!frame) { /* no binding. walk up to top frame */ - frame = g; - while(frame->parent) { - frame = frame->parent; - } - } - /* now we're at the right frame. */ - dict_put(frame->contents,label,r); - } else if(r->scope == GLOBAL_SCOPE) { - /* walk up to the top frame */ - GRAMMAR *frame = g; - while(frame->parent) { - frame = frame->parent; - } - dict_put(frame->contents,label,r); - } + char *label = rule_getLabel(r); + /* if the scope is lexical, add to this grammar */ + if (r->scope == LOCAL_SCOPE) { + dict_put(g->contents, label, r); + } else if (r->scope == NON_LOCAL_SCOPE) { + /* otherwise walk up the grammar stack */ + GRAMMAR *frame = NULL; + frame = grammar_binding(g, label); + if (!frame) { /* no binding. walk up to top frame */ + frame = g; + while (frame->parent) { + frame = frame->parent; + } + } + /* now we're at the right frame. */ + dict_put(frame->contents, label, r); + } else if (r->scope == GLOBAL_SCOPE) { + /* walk up to the top frame */ + GRAMMAR *frame = g; + while (frame->parent) { + frame = frame->parent; + } + dict_put(frame->contents, label, r); + } } void grammar_addAll(GRAMMAR *g, LIST *l) { - int i, len = list_length(l); - for(i = 0; i < len; i++) { - grammar_add(g,(RULE *)list_get(l,i)); - } + int i, len = list_length(l); + for (i = 0; i < len; i++) { + grammar_add(g, (RULE *) list_get(l, i)); + } } static char *getLabel(char *packagedLabel) { - int i; - char *p; - for(p = packagedLabel, i = 0; *p && *p != '.'; p++, i++) - ; - if(*p) { /* found a '.' */ - GSTR *g = gstr_new(""); - gstr_append(g, packagedLabel + i + 1); - return gstr_detach(g); - } else { /* didn't--this isn't a packaged label */ - return NULL; - } + int i; + char *p; + for (p = packagedLabel, i = 0; *p && *p != '.'; p++, i++) + ; + if (*p) { /* found a '.' */ + GSTR *g = gstr_new(""); + gstr_append(g, packagedLabel + i + 1); + return gstr_detach(g); + } else { /* didn't--this isn't a packaged label */ + return NULL; + } } /* walk the grammar stack to find the grammar with a binding for the given label */ GRAMMAR *grammar_binding(GRAMMAR *gram, char *label) { - char *noPackage = NULL; - GRAMMAR *frame = NULL; - RULE *r = (RULE *)dict_get(gram->contents,label); - if(r) { - return gram; - } - /* not found. try the parent grammar */ - if(gram->parent) { - frame = grammar_binding(gram->parent, label); - } - if(frame) { - return frame; - } - /* not found. try it without the package (if any) */ - noPackage = getLabel(label); - if(noPackage) { - return grammar_binding(gram, noPackage); - } - /* OK, there really is no such binding. */ - return NULL; + char *noPackage = NULL; + GRAMMAR *frame = NULL; + RULE *r = (RULE *) dict_get(gram->contents, label); + if (r) { + return gram; + } + /* not found. try the parent grammar */ + if (gram->parent) { + frame = grammar_binding(gram->parent, label); + } + if (frame) { + return frame; + } + /* not found. try it without the package (if any) */ + noPackage = getLabel(label); + if (noPackage) { + return grammar_binding(gram, noPackage); + } + /* OK, there really is no such binding. */ + return NULL; } RULE *grammar_lookUp(GRAMMAR *gram, char *label) { - char *noPackage = NULL; - RULE *r = NULL; - if(!dynamic) { - gram = grammar_binding(gram, label); - } - if(gram) { - r = (RULE *)dict_get(gram->contents, label); - if(r) return r; - noPackage = getLabel(label); /* this is redundant but that's no biggie */ - if(!noPackage) { - /* this is fatal, because it's inconsistent with the - grammar_binding results in hand */ - fprintf(stderr,"binding disappeared for %s\n",label); - exit(-1); - } - r = (RULE *)dict_get(gram->contents, noPackage); - if(r) return r; - } - return NULL; + char *noPackage = NULL; + RULE *r = NULL; + if (!dynamic) { + gram = grammar_binding(gram, label); + } + if (gram) { + r = (RULE *) dict_get(gram->contents, label); + if (r) + return r; + noPackage = getLabel(label); /* this is redundant but that's no biggie */ + if (!noPackage) { + /* this is fatal, because it's inconsistent with the + grammar_binding results in hand */ + fprintf(stderr, "binding disappeared for %s\n", label); + exit(-1); + } + r = (RULE *) dict_get(gram->contents, noPackage); + if (r) + return r; + } + return NULL; } LIST *expand_choice(GRAMMAR *gram, LIST *c) { - int i; - LIST *l = list_new(); - for(i = 0; i < list_length(c); i++) { - GRAMBIT *g = (GRAMBIT *)list_get(c,i); - list_appendAndFree(l,grammar_expand(gram,g)); - } - return l; + int i; + LIST *l = list_new(); + for (i = 0; i < list_length(c); i++) { + GRAMBIT *g = (GRAMBIT *) list_get(c, i); + list_appendAndFree(l, grammar_expand(gram, g)); + } + return l; } LIST *grammar_expand(GRAMMAR *parentGram, GRAMBIT *g) { - RULE *r; - LIST *result; - int x, j; - GRAMMAR *gram; + RULE *r; + LIST *result; + int x, j; + GRAMMAR *gram; - stackDepth++; + stackDepth++; - if(maxStackDepth > 0 && stackDepth > maxStackDepth) { - fprintf(stderr,"warning: stack depth exceeded\n"); - result = list_new(); - return result; - } + if (maxStackDepth > 0 && stackDepth > maxStackDepth) { + fprintf(stderr, "warning: stack depth exceeded\n"); + result = list_new(); + return result; + } - if(dynamic) { - gram = parentGram; - } else { - /* create a new grammar stack frame */ - gram = grammar_new(); - gram->parent = parentGram; - } + if (dynamic) { + gram = parentGram; + } else { + /* create a new grammar stack frame */ + gram = grammar_new(); + gram->parent = parentGram; + } - result = list_new(); + result = list_new(); - x = choose_next((g->max_x - g->min_x) + 1) + g->min_x; + x = choose_next((g->max_x - g->min_x) + 1) + g->min_x; - for(j = 0; j < x; j++) { - switch(g->type) { - case LITERAL_T: - case TRANS_T: - list_add(result,literal_new(grambit_toString(gram, g))); - break; - case LABEL_T: + for (j = 0; j < x; j++) { + switch (g->type) { + case LITERAL_T: + case TRANS_T: + list_add(result, literal_new(grambit_toString(gram, g))); + break; + case LABEL_T: #ifdef DEBUG - { - int i; - for(i = 0; i < stackDepth-1; i++) - fputc('>',stdout); - grambit_print(g,stdout); - printf("\n"); - } + { + int i; + for(i = 0; i < stackDepth-1; i++) + fputc('>',stdout); + grambit_print(g,stdout); + printf("\n"); + } #endif - r = grammar_lookUp(gram,g->l); - if(r) { - LIST *cs; - LIST *c, *ex; - cs = (LIST *)rule_getChoices(r); - c = (LIST *)list_get(cs, choose_next(list_length(cs))); - ex = expand_choice(gram,c); + r = grammar_lookUp(gram, g->l); + if (r) { + LIST *cs; + LIST *c, *ex; + cs = (LIST *) rule_getChoices(r); + c = (LIST *) list_get(cs, choose_next(list_length(cs))); + ex = expand_choice(gram, c); #ifdef DEBUG - { - int i; - for(i = 0; i < stackDepth-1; i++) - fputc('<',stdout); - for(i = 0; i < ex->length; i++) { - GRAMBIT *gb = (GRAMBIT *)list_get(ex,i); - grambit_print(gb,stdout); - } - printf("\n"); - } + { + int i; + for(i = 0; i < stackDepth-1; i++) + fputc('<',stdout); + for(i = 0; i < ex->length; i++) { + GRAMBIT *gb = (GRAMBIT *)list_get(ex,i); + grambit_print(gb,stdout); + } + printf("\n"); + } #endif - list_appendAndFree(result,ex); - } else { - /* rule not found; produce the rule name */ - fprintf(stderr,"rmutt: warning: rule not found: %s\n",g->l); - list_add(result,literal_new(g->l)); - } - break; - case RULE_T: - /* copy the rule to the grammar */ - grammar_add(parentGram,grambit_copy(g)); - break; - case ASSIGNMENT_T: - /* produce the rhs, and then create a new literal - rule with the result as its only choice, and - add it to the grammar */ - if(1) { - RULE *r; - GRAMBIT *lit; - LIST *onlyChoice; - LIST *choices; - char *str; - /* first, produce the rhs */ - str = grammar_produce(gram,choice_new(rule_getChoices(g))); - /* now create a single term and single choice */ - onlyChoice = list_new(); - lit = literal_new(str); - free(str); - list_add(onlyChoice,lit); - choices = list_new(); - list_add(choices,onlyChoice); - /* make that the rhs of a new rule */ - r = rule_new(rule_getLabel(g),choices,g->scope); - /* add the rule to the grammar */ - grammar_add(parentGram,r); - } - break; - case CHOICE_T: - if(1) { - LIST *c; - LIST *cs; - cs = (LIST *)choice_getChoices(g); - c = (LIST *)list_get(cs, choose_next(list_length(cs))); - list_appendAndFree(result,expand_choice(gram,c)); - } - break; - case RXSUB_T: - case MAPPING_T: - case RXMATCH_T: - list_add(result,g); - break; - default: - fprintf(stderr,"rmutt: fatal: illegal grambit type %d\n",g->type); - exit(-1); - } - } + list_appendAndFree(result, ex); + } else { + /* rule not found; produce the rule name */ + fprintf(stderr, "rmutt: warning: rule not found: %s\n", g->l); + list_add(result, literal_new(g->l)); + } + break; + case RULE_T: + /* copy the rule to the grammar */ + grammar_add(parentGram, grambit_copy(g)); + break; + case ASSIGNMENT_T: + /* produce the rhs, and then create a new literal + rule with the result as its only choice, and + add it to the grammar */ + if (1) { + RULE *r; + GRAMBIT *lit; + LIST *onlyChoice; + LIST *choices; + char *str; + /* first, produce the rhs */ + str = grammar_produce(gram, choice_new(rule_getChoices(g))); + /* now create a single term and single choice */ + onlyChoice = list_new(); + lit = literal_new(str); + free(str); + list_add(onlyChoice, lit); + choices = list_new(); + list_add(choices, onlyChoice); + /* make that the rhs of a new rule */ + r = rule_new(rule_getLabel(g), choices, g->scope); + /* add the rule to the grammar */ + grammar_add(parentGram, r); + } + break; + case CHOICE_T: + if (1) { + LIST *c; + LIST *cs; + cs = (LIST *) choice_getChoices(g); + c = (LIST *) list_get(cs, choose_next(list_length(cs))); + list_appendAndFree(result, expand_choice(gram, c)); + } + break; + case RXSUB_T: + case MAPPING_T: + case RXMATCH_T: + list_add(result, g); + break; + default: + fprintf(stderr, "rmutt: fatal: illegal grambit type %d\n", g->type); + exit(-1); + } + } - stackDepth--; + stackDepth--; - if(!dynamic) { - /* release the local frame */ - grammar_free(gram); - } + if (!dynamic) { + /* release the local frame */ + grammar_free(gram); + } - return result; + return result; } char *grammar_produce(GRAMMAR *gram, GRAMBIT *g) { - LIST *result; - int i; - GSTR *str; + LIST *result; + int i; + GSTR *str; - result = grammar_expand(gram,g); - str = gstr_new(""); - for(i = 0; i < result->length; i++) { - char *s; - GRAMBIT *g = (GRAMBIT *)list_get(result,i); - s = grambit_toString(gram, g); + result = grammar_expand(gram, g); + str = gstr_new(""); + for (i = 0; i < result->length; i++) { + char *s; + GRAMBIT *g = (GRAMBIT *) list_get(result, i); + s = grambit_toString(gram, g); #ifdef DEBUG - printf("produced %s\n",s); + printf("produced %s\n",s); #endif - gstr_append(str,s); - free(s); - } - list_free(result); - return gstr_detach(str); + gstr_append(str, s); + free(s); + } + list_free(result); + return gstr_detach(str); } /** @@ -286,65 +288,66 @@ char *grammar_produce(GRAMMAR *gram, GRAMBIT *g) { * a transformation or call to an external script, or what have you */ char *grambit_toString(GRAMMAR *gram, GRAMBIT *g) { - switch(g->type) { - case LITERAL_T: - return strdup(g->l); - case TRANS_T: - if(1){ - char *str; - LIST *tl; - int i; - /* first, expand the source grambit into a string */ - str = grammar_produce(gram, g->source); + switch (g->type) { + case LITERAL_T: + return strdup(g->l); + case TRANS_T: + if (1) { + char *str; + LIST *tl; + int i; + /* first, expand the source grambit into a string */ + str = grammar_produce(gram, g->source); #ifdef DEBUG - printf("transforming \"%s\"",str); + printf("transforming \"%s\"",str); #endif - /* now expand the (list of) transformation(s) */ - tl = grammar_expand(gram, g->trans); - /* now apply them in series */ - for(i = 0; i < tl->length; i++) { - char *ns; - ns = transform(gram, str, (GRAMBIT *)list_get(tl,i)); + /* now expand the (list of) transformation(s) */ + tl = grammar_expand(gram, g->trans); + /* now apply them in series */ + for (i = 0; i < tl->length; i++) { + char *ns; + ns = transform(gram, str, (GRAMBIT *) list_get(tl, i)); #ifdef DEBUG - printf("%s",ns); + printf("%s",ns); #endif - free(str); - str = ns; - } + free(str); + str = ns; + } #ifdef DEBUG - printf("\n"); + printf("\n"); #endif - return str; - } - } - return NULL; + return str; + } + } + return NULL; } /* apply a transformation to a string */ char *transform(GRAMMAR *gram, char *str, GRAMBIT *trans) { - switch(trans->type) { - case RXSUB_T: + switch (trans->type) { + case RXSUB_T: #ifdef DEBUG - printf(">/%s/%s/",trans->rx_rx,trans->rx_rep); + printf(">/%s/%s/",trans->rx_rx,trans->rx_rep); #endif - return regsub(str,trans->rx_rx,trans->rx_rep,REG_EXTENDED); - case MAPPING_T: - if(!strcmp(str,trans->rx_rx)) { - return(grammar_produce(gram, trans->trans)); - } else { - return(strdup(str)); - } - case RXMATCH_T: { - char **result = regmatch(str,trans->rx_rx,1,REG_EXTENDED,0); - if(result==NULL) { - return strdup(str); - } else { - free(*result); - free(result); - return grammar_produce(gram,trans->trans); - }} - } - fprintf(stderr,"error: illegal transformation\n"); - return NULL; + return regsub(str, trans->rx_rx, trans->rx_rep, REG_EXTENDED); + case MAPPING_T: + if (!strcmp(str, trans->rx_rx)) { + return (grammar_produce(gram, trans->trans)); + } else { + return (strdup(str)); + } + case RXMATCH_T: { + char **result = regmatch(str, trans->rx_rx, 1, REG_EXTENDED, 0); + if (result == NULL) { + return strdup(str); + } else { + free(*result); + free(result); + return grammar_produce(gram, trans->trans); + } + } + } + fprintf(stderr, "error: illegal transformation\n"); + return NULL; } diff --git a/grammar.h b/grammar.h index cbcd1a5..b1cd739 100644 --- a/grammar.h +++ b/grammar.h @@ -3,8 +3,8 @@ #include "grambit.h" typedef struct _grammar { - struct _grammar *parent; - DICT *contents; + struct _grammar *parent; + DICT *contents; } GRAMMAR; /* rmutt grammar implementation */ @@ -17,7 +17,7 @@ extern void grammar_addAll(GRAMMAR *, LIST *); /* add rules to a grammar */ extern RULE *grammar_lookUp(GRAMMAR *, char *); /* look up a named rule in the grammar */ /* expand the grambit of the grammar into a sequence of - grambits, selected according to the rules of the grammar */ + grambits, selected according to the rules of the grammar */ extern LIST *grammar_expand(GRAMMAR *, GRAMBIT *g); /* do the same thing but actually produce a string */ @@ -27,4 +27,4 @@ extern char *grammar_produce(GRAMMAR *, GRAMBIT *g); extern char *grambit_toString(GRAMMAR *, GRAMBIT *); /* apply a transformation to a string in the context of a grammar */ -extern char *transform(GRAMMAR *g, char *,GRAMBIT *); +extern char *transform(GRAMMAR *g, char *, GRAMBIT *); diff --git a/gstr.c b/gstr.c index a3e9c8c..c9f2ac0 100644 --- a/gstr.c +++ b/gstr.c @@ -7,73 +7,75 @@ #define GROW_BY 128 GSTR *gstr_new(char *d) { - GSTR *g = (GSTR *)malloc(sizeof(GSTR)); - g->size = 0; - g->len = 0; - g->buf = NULL; - gstr_append(g, d); - return g; + GSTR *g = (GSTR *) malloc(sizeof(GSTR)); + g->size = 0; + g->len = 0; + g->buf = NULL; + gstr_append(g, d); + return g; } void gstr_appendn(GSTR *g, char *b, size_t len) { - if(!b) return; - if(!g->buf) { - g->buf = malloc(len); - g->size = len; - g->len = len; - strncpy(g->buf,b,len); - } else { - if(g->size < g->len + len + 1) { - g->buf = realloc(g->buf, (g->size = g->len + len + GROW_BY)); - } - strncpy(g->buf+g->len,b,len); - g->len += len; - } + if (!b) + return; + if (!g->buf) { + g->buf = malloc(len); + g->size = len; + g->len = len; + strncpy(g->buf, b, len); + } else { + if (g->size < g->len + len + 1) { + g->buf = realloc(g->buf, (g->size = g->len + len + GROW_BY)); + } + strncpy(g->buf + g->len, b, len); + g->len += len; + } } void gstr_append(GSTR *g, char *d) { - size_t len; - if(!d) return; - len = strlen(d); - if(!g->buf) { - g->buf = strdup(d); - g->size = len+1; - g->len = len; - } else { - if(g->size < g->len + len + 1) { - g->buf = realloc(g->buf, (g->size = g->len + len + GROW_BY)); - } - strcpy(g->buf+g->len,d); - g->len += len; - } + size_t len; + if (!d) + return; + len = strlen(d); + if (!g->buf) { + g->buf = strdup(d); + g->size = len + 1; + g->len = len; + } else { + if (g->size < g->len + len + 1) { + g->buf = realloc(g->buf, (g->size = g->len + len + GROW_BY)); + } + strcpy(g->buf + g->len, d); + g->len += len; + } } void gstr_appendc(GSTR *g, char c) { - char cs[2]; - cs[0] = c; - cs[1] = '\0'; - gstr_append(g,cs); + char cs[2]; + cs[0] = c; + cs[1] = '\0'; + gstr_append(g, cs); } char *gstr_detach(GSTR *g) { - char *result; - result = realloc(g->buf,g->len+1); - free(g); - return result; + char *result; + result = realloc(g->buf, g->len + 1); + free(g); + return result; } char *gstr_cat(char *a, char *b) { - GSTR *g = gstr_new(a); - gstr_append(g,b); - return gstr_detach(g); + GSTR *g = gstr_new(a); + gstr_append(g, b); + return gstr_detach(g); } /* concatenate and free */ char *gstr_catf(char *a, char *b) { - GSTR *g = gstr_new(a); - gstr_append(g,b); - return gstr_detach(g); - free(a); - free(b); + GSTR *g = gstr_new(a); + gstr_append(g, b); + return gstr_detach(g); + free(a); + free(b); } diff --git a/gstr.h b/gstr.h index 6a86aee..9365e62 100644 --- a/gstr.h +++ b/gstr.h @@ -4,17 +4,17 @@ #include typedef struct _gstr { - size_t size; - size_t len; - char *buf; + size_t size; + size_t len; + char *buf; } GSTR; /* growable string implementation */ extern GSTR *gstr_new(char *); /* create a new growable string */ -extern void gstr_appendn(GSTR *,char *, size_t); /* append n characters to the end of a growable string */ -extern void gstr_append(GSTR *,char *); /* append a null-terminated string to the end of a growable string */ -extern void gstr_appendc(GSTR *,char); /* append a single character to the end of a growable string */ +extern void gstr_appendn(GSTR *, char *, size_t); /* append n characters to the end of a growable string */ +extern void gstr_append(GSTR *, char *); /* append a null-terminated string to the end of a growable string */ +extern void gstr_appendc(GSTR *, char); /* append a single character to the end of a growable string */ extern char *gstr_detach(GSTR *); /* return a copy of a growable string and free the growable string */ extern char *gstr_cat(char *, char *); /* concatenate two strings and return that as a growable string */ extern char *gstr_catf(char *, char *); /* same as gstr_cat but frees the strings */ diff --git a/list.c b/list.c index 04d0931..d9fadce 100644 --- a/list.c +++ b/list.c @@ -4,80 +4,80 @@ #define LIST_INIT_CAPACITY 20 LIST *list_new() { - LIST *nl = (LIST *)malloc(sizeof(LIST)); - nl->data = calloc(LIST_INIT_CAPACITY,sizeof(void *)); - nl->capacity = LIST_INIT_CAPACITY; - nl->length = 0; - return nl; + LIST *nl = (LIST *) malloc(sizeof(LIST)); + nl->data = calloc(LIST_INIT_CAPACITY, sizeof(void *)); + nl->capacity = LIST_INIT_CAPACITY; + nl->length = 0; + return nl; } void list_free(LIST *l) { - free(l->data); - free(l); + free(l->data); + free(l); } void list_freeData(LIST *l, Destructor d) { - long i, len; - len = list_length(l); - for(i = 0; i < len; i++) { - d(list_get(l, i)); - } - list_free(l); + long i, len; + len = list_length(l); + for (i = 0; i < len; i++) { + d(list_get(l, i)); + } + list_free(l); } long list_length(LIST *l) { - return l->length; + return l->length; } void *list_get(LIST *l, long i) { - return l->data[i]; + return l->data[i]; } void list_add(LIST *l, void *data) { - if(l->length >= l->capacity) { - long i; - void **np; - size_t newcap = l->capacity * 2; - np = calloc(newcap,sizeof(void *)); - for(i = 0; i < l->length; i++) { - np[i] = l->data[i]; - } - free(l->data); - l->data = np; - l->capacity = newcap; - } - l->data[l->length++] = data; + if (l->length >= l->capacity) { + long i; + void **np; + size_t newcap = l->capacity * 2; + np = calloc(newcap, sizeof(void *)); + for (i = 0; i < l->length; i++) { + np[i] = l->data[i]; + } + free(l->data); + l->data = np; + l->capacity = newcap; + } + l->data[l->length++] = data; } void list_append(LIST *a, LIST *b) { - int i; - for(i = 0; i < b->length; i++) - list_add(a,list_get(b,i)); + int i; + for (i = 0; i < b->length; i++) + list_add(a, list_get(b, i)); } void list_appendAndFree(LIST *a, LIST *b) { - int i; - for(i = 0; i < b->length; i++) - list_add(a,list_get(b,i)); - list_free(b); + int i; + for (i = 0; i < b->length; i++) + list_add(a, list_get(b, i)); + list_free(b); } void *list_getRand(LIST *l) { - long i; - i = random() % list_length(l); - return list_get(l,i); + long i; + i = random() % list_length(l); + return list_get(l, i); } LIST *list_addToNew(void *element) { - LIST *theList = list_new(); - list_add(theList, element); - return theList; + LIST *theList = list_new(); + list_add(theList, element); + return theList; } LIST *list_reverse(LIST *list) { - long len = list_length(list), i; - LIST *reversed = list_new(); - for(i = len-1; i >= 0; i--) { - list_add(reversed,list_get(list,i)); - } - return reversed; + long len = list_length(list), i; + LIST *reversed = list_new(); + for (i = len - 1; i >= 0; i--) { + list_add(reversed, list_get(list, i)); + } + return reversed; } diff --git a/list.h b/list.h index e07f37d..fea0177 100644 --- a/list.h +++ b/list.h @@ -6,9 +6,9 @@ /* generic list support */ typedef struct _list { - long length; /* how many elements are in the list */ - long capacity; /* out of how many possible */ - void **data; /* the items */ + long length; /* how many elements are in the list */ + long capacity; /* out of how many possible */ + void **data; /* the items */ } LIST; extern LIST *list_new(); /* create a list */ diff --git a/main.c b/main.c index 11ec7e9..6bbc08f 100644 --- a/main.c +++ b/main.c @@ -23,140 +23,148 @@ int maxStackDepth = -1; int dynamic = 0; void version() { - fprintf(stderr,"rmutt version %s\n",RMUTT_VERSION); + fprintf(stderr, "rmutt version %s\n", RMUTT_VERSION); } void usage() { - version(); - fprintf(stderr,"usage: rmutt -[bdeirsv] grammar\n"); - fprintf(stderr," -s number max stack depth\n"); - fprintf(stderr," -r number random seed\n"); - fprintf(stderr," -i number iteration\n"); - fprintf(stderr," -I file read the iteration from a file\n"); - fprintf(stderr," -e name name of rule to expand (default: first)\n"); - fprintf(stderr," -b name=value bind name to value in the grammar\n"); - fprintf(stderr," -d dynamic variable scoping (default: lexical)\n"); - fprintf(stderr," -v print version number and exit\n"); + version(); + fprintf(stderr, "usage: rmutt -[bdeirsv] grammar\n"); + fprintf(stderr, " -s number max stack depth\n"); + fprintf(stderr, " -r number random seed\n"); + fprintf(stderr, " -i number iteration\n"); + fprintf(stderr, " -I file read the iteration from a file\n"); + fprintf(stderr, + " -e name name of rule to expand (default: first)\n"); + fprintf(stderr, " -b name=value bind name to value in the grammar\n"); + fprintf(stderr, + " -d dynamic variable scoping (default: lexical)\n"); + fprintf(stderr, " -v print version number and exit\n"); } int main(int argc, char **argv) { - GRAMBIT *t; - char *result; - FILE *in = stdin; - char *rte = NULL; - char c; - LIST *bindings = list_new(); - int i, len; - - srandom(time(NULL)); - - while((c = getopt(argc, argv, "vs:r:i:I:e:db:")) != EOF) { - i = 0; - switch(c) { - case 'v': - version(); - exit(1); - case 's': - /* maximum stack depth */ - maxStackDepth = atoi(optarg); - break; - case 'r': - srandom(atoi(optarg)); - break; - case 'i': - choose_setIterationString(optarg); - break; - case 'I': { - FILE *of = fopen(optarg,"r"); - if(!of) { - fprintf(stderr,"error: unable to open iteration file %s\n",optarg); - exit(-1); - } - choose_setIterationFile(of); - } break; - case 'e': - rte = strdup(optarg); - break; - case 'b': { - int i; - char *name, *value; - - for(i=0; *(optarg+i) != '='; i++) { - if(!*(optarg+i)) { - usage(); - exit(-1); - } - } - if(!*(optarg+i+1)) { usage(); } - - name = (char *) calloc(sizeof(char),i+1); - strncpy(name, optarg, i); - value = (char *)strdup(optarg+i+1); - - list_add(bindings, binding_new(name, literal_new(value), GLOBAL_SCOPE)); - } break; - case 'd': - dynamic++; - break; - default: - usage(); - exit(-1); - } - } - - if(argc-optind == 1) { - in = fopen(argv[optind],"r"); - if(!in) { - fprintf(stderr,"error: unable to open file %s\n",argv[optind]); - exit(-1); - } - } - yyin = in; + GRAMBIT *t; + char *result; + FILE *in = stdin; + char *rte = NULL; + char c; + LIST *bindings = list_new(); + int i, len; + + srandom(time(NULL)); + + while ((c = getopt(argc, argv, "vs:r:i:I:e:db:")) != EOF) { + i = 0; + switch (c) { + case 'v': + version(); + exit(1); + case 's': + /* maximum stack depth */ + maxStackDepth = atoi(optarg); + break; + case 'r': + srandom(atoi(optarg)); + break; + case 'i': + choose_setIterationString(optarg); + break; + case 'I': { + FILE *of = fopen(optarg, "r"); + if (!of) { + fprintf(stderr, "error: unable to open iteration file %s\n", + optarg); + exit(-1); + } + choose_setIterationFile(of); + } + break; + case 'e': + rte = strdup(optarg); + break; + case 'b': { + int i; + char *name, *value; + + for (i = 0; *(optarg + i) != '='; i++) { + if (!*(optarg + i)) { + usage(); + exit(-1); + } + } + if (!*(optarg + i + 1)) { + usage(); + } + + name = (char *) calloc(sizeof(char), i + 1); + strncpy(name, optarg, i); + value = (char *) strdup(optarg + i + 1); + + list_add(bindings, binding_new(name, literal_new(value), + GLOBAL_SCOPE)); + } + break; + case 'd': + dynamic++; + break; + default: + usage(); + exit(-1); + } + } + + if (argc - optind == 1) { + in = fopen(argv[optind], "r"); + if (!in) { + fprintf(stderr, "error: unable to open file %s\n", argv[optind]); + exit(-1); + } + } + yyin = in; #ifdef DEBUG - fprintf(stderr,"parsing\n"); + fprintf(stderr,"parsing\n"); #endif - /* call the parser (puts result in "grammar") */ - yyparse(); + /* call the parser (puts result in "grammar") */ + yyparse(); #ifdef DEBUG - fprintf(stderr,"done\n"); - fflush(stdout); + fprintf(stderr,"done\n"); + fflush(stdout); #endif - if(!grammar) { + if (!grammar) { #ifdef DEBUG - fprintf(stderr,"no grammar returned from yyparse\n"); - fflush(stdout); + fprintf(stderr,"no grammar returned from yyparse\n"); + fflush(stdout); #endif - exit(-1); - } + exit(-1); + } - /* add the bindings from the command line */ - len = list_length(bindings); - for(i = 0; i < len; i++) { - grammar_add(grammar,list_get(bindings,i)); - } + /* add the bindings from the command line */ + len = list_length(bindings); + for (i = 0; i < len; i++) { + grammar_add(grammar, list_get(bindings, i)); + } - /* if there's no top rule, the grammar is empty */ - if(!topRule) { - fprintf(stderr,"rmutt error: empty grammar\n"); - exit(-1); - } + /* if there's no top rule, the grammar is empty */ + if (!topRule) { + fprintf(stderr, "rmutt error: empty grammar\n"); + exit(-1); + } - /* determine which rule to expand */ - if(rte) { - t = label_new(rte); /* the one from the command line, or */ - } else { - t = label_new(topRule); /* the (default) top rule */ - } + /* determine which rule to expand */ + if (rte) { + t = label_new(rte); /* the one from the command line, or */ + } else { + t = label_new(topRule); /* the (default) top rule */ + } - /* produce the output */ - result = grammar_produce(grammar,t); + /* produce the output */ + result = grammar_produce(grammar, t); - fputs(result,stdout); + fputs(result, stdout); - /*free(result); - grambit_free(t);*/ + /*free(result); + grambit_free(t);*/ - return 0; + return 0; } diff --git a/rxutil.c b/rxutil.c index eb5395e..beaf1a6 100644 --- a/rxutil.c +++ b/rxutil.c @@ -8,67 +8,66 @@ #include "gstr.h" char *rx_interpolate(char *orig, char *replace_o, regmatch_t *match) { - char key[5]; - int i; - char *replace = strdup(replace_o); - i = 1; - while(match[i].rm_so != -1) { - char *ns; - char *rep; - GSTR *rep_gs; - rep_gs = gstr_new(""); - gstr_appendn(rep_gs,orig + match[i].rm_so,match[i].rm_eo - match[i].rm_so); - gstr_append(rep_gs,""); - rep = gstr_detach(rep_gs); - sprintf(key,"\\\\%d",i); - /* now replace \(digit) with whatever in "replace" */ - ns = regsub(replace,key,rep,0); - free(replace); - replace = ns; - i++; - } - return replace; + char key[5]; + int i; + char *replace = strdup(replace_o); + i = 1; + while (match[i].rm_so != -1) { + char *ns; + char *rep; + GSTR *rep_gs; + rep_gs = gstr_new(""); + gstr_appendn(rep_gs, orig + match[i].rm_so, match[i].rm_eo + - match[i].rm_so); + gstr_append(rep_gs, ""); + rep = gstr_detach(rep_gs); + sprintf(key, "\\\\%d", i); + /* now replace \(digit) with whatever in "replace" */ + ns = regsub(replace, key, rep, 0); + free(replace); + replace = ns; + i++; + } + return replace; } /* * globally substitute replace for regex in str */ -char *regsub_c(char *str, regex_t *rx, char *replace) -{ - regmatch_t match[10]; - size_t str_offset = 0, rep_len, tail_len; - GSTR *res; - int initial = 0; - - res = gstr_new(""); - - while ((regexec(rx, str + str_offset, 10, match, - !initial++ ? 0 : REG_NOTBOL) == 0)) { - char *rep = rx_interpolate(str+str_offset,replace,match); - rep_len = strlen(rep); - gstr_appendn(res, str + str_offset, match[0].rm_so); - gstr_appendn(res, rep, rep_len); - str_offset += match[0].rm_eo; - } - - tail_len = strlen(str + str_offset); - gstr_appendn(res, str + str_offset, tail_len); - gstr_append(res,""); - return gstr_detach (res); +char *regsub_c(char *str, regex_t *rx, char *replace) { + regmatch_t match[10]; + size_t str_offset = 0, rep_len, tail_len; + GSTR *res; + int initial = 0; + + res = gstr_new(""); + + while ((regexec(rx, str + str_offset, 10, match, !initial++ ? 0 + : REG_NOTBOL) == 0)) { + char *rep = rx_interpolate(str + str_offset, replace, match); + rep_len = strlen(rep); + gstr_appendn(res, str + str_offset, match[0].rm_so); + gstr_appendn(res, rep, rep_len); + str_offset += match[0].rm_eo; + } + + tail_len = strlen(str + str_offset); + gstr_appendn(res, str + str_offset, tail_len); + gstr_append(res, ""); + return gstr_detach(res); } /* * same as regsub, except it compiles the regex for you */ -char *regsub(char *str, char *rx, char *replace, int flags) -{ - regex_t comp_rx; - char *result; - - regcomp(&comp_rx, rx, flags); - result = regsub_c(str, &comp_rx, replace); - regfree(&comp_rx); - return result; +char *regsub(char *str, char *rx, char *replace, int flags) { + regex_t comp_rx; + char *result; + + regcomp(&comp_rx, rx, flags); + result = regsub_c(str, &comp_rx, replace); + regfree(&comp_rx); + return result; } /* multiple cascading regsubs. rxs should be in the form @@ -78,108 +77,102 @@ char *regsub(char *str, char *rx, char *replace, int flags) * returns the final string after all substitutions have been * done. */ -char *regsubs(char *str, char **rxs, int flags) -{ - char *buf[2]; - int i, alt; +char *regsubs(char *str, char **rxs, int flags) { + char *buf[2]; + int i, alt; - buf[0] = str; + buf[0] = str; - for (i = 0, alt = 0; rxs[i] != NULL; i += 2, alt++) { - register char *old = buf[alt % 2]; + for (i = 0, alt = 0; rxs[i] != NULL; i += 2, alt++) { + register char *old = buf[alt % 2]; - buf[(alt + 1) % 2] = regsub(old, rxs[i], rxs[i + 1], flags); + buf[(alt + 1) % 2] = regsub(old, rxs[i], rxs[i + 1], flags); - if (old != str) - /* don't free the string originally passed in! */ - free(old); - } + if (old != str) + /* don't free the string originally passed in! */ + free(old); + } - return buf[alt % 2]; + return buf[alt % 2]; } /* * return copies of all the matched subexps. NULL if failed */ -char **regmatch_c(char *str, regex_t * rx, size_t nmatch, int eflags) -{ - char **result = NULL; - int i; - regmatch_t *matches; +char **regmatch_c(char *str, regex_t * rx, size_t nmatch, int eflags) { + char **result = NULL; + int i; + regmatch_t *matches; - matches = (regmatch_t *) calloc(nmatch + 1, sizeof(regmatch_t)); + matches = (regmatch_t *) calloc(nmatch + 1, sizeof(regmatch_t)); - i = regexec(rx, str, nmatch + 1, matches, eflags); + i = regexec(rx, str, nmatch + 1, matches, eflags); - if (i) - goto finish; + if (i) + goto finish; - result = (char **) calloc(nmatch, sizeof(char *)); - for (i = 1; i < nmatch + 1; i++) { - int len = matches[i].rm_eo - matches[i].rm_so; + result = (char **) calloc(nmatch, sizeof(char *)); + for (i = 1; i < nmatch + 1; i++) { + int len = matches[i].rm_eo - matches[i].rm_so; - result[i - 1] = (char *)malloc(len + 1); - strncpy(result[i - 1], str + matches[i].rm_so, len); - result[i - 1][len] = '\0'; - } + result[i - 1] = (char *) malloc(len + 1); + strncpy(result[i - 1], str + matches[i].rm_so, len); + result[i - 1][len] = '\0'; + } -finish: - free(matches); - return result; + finish: free(matches); + return result; } /* * same as regmatch_c, except it compiles the regex for you */ -char **regmatch(char *str, char *rx, size_t nmatch, int cflags, int eflags) -{ - regex_t rx_comp; - char **result; +char **regmatch(char *str, char *rx, size_t nmatch, int cflags, int eflags) { + regex_t rx_comp; + char **result; - regcomp(&rx_comp, rx, cflags); + regcomp(&rx_comp, rx, cflags); - result = regmatch_c(str, &rx_comp, nmatch, eflags); + result = regmatch_c(str, &rx_comp, nmatch, eflags); - regfree(&rx_comp); + regfree(&rx_comp); - return result; + return result; } /* * split str into substrings separated by rx */ -LIST *split_c(char *str, regex_t * rx) -{ - regmatch_t match[1]; - size_t str_offset = 0; - LIST *result = list_new(); - - while (regexec(rx, str + str_offset, 1, match, REG_NOTBOL) == 0) { - int len = match[0].rm_so; - char *frag = (char *) malloc(len + 1); - - strncpy(frag, str + str_offset, len); - *(frag + len) = '\0'; - list_add(result, frag); - str_offset += match[0].rm_eo; - } - - list_add(result, strdup(str + str_offset)); - return result; +LIST *split_c(char *str, regex_t * rx) { + regmatch_t match[1]; + size_t str_offset = 0; + LIST *result = list_new(); + + while (regexec(rx, str + str_offset, 1, match, REG_NOTBOL) == 0) { + int len = match[0].rm_so; + char *frag = (char *) malloc(len + 1); + + strncpy(frag, str + str_offset, len); + *(frag + len) = '\0'; + list_add(result, frag); + str_offset += match[0].rm_eo; + } + + list_add(result, strdup(str + str_offset)); + return result; } /* * same as regsub, except it compiles the regex for you */ -LIST *split(char *str, char *rx, int flags) -{ - regex_t comp_rx; - LIST *result; +LIST *split(char *str, char *rx, int flags) { + regex_t comp_rx; + LIST *result; - regcomp(&comp_rx, rx, flags); + regcomp(&comp_rx, rx, flags); - result = split_c(str, &comp_rx); + result = split_c(str, &comp_rx); - regfree(&comp_rx); - return result; + regfree(&comp_rx); + return result; } diff --git a/rxutil.h b/rxutil.h index ebcf6b6..6b516f4 100644 --- a/rxutil.h +++ b/rxutil.h @@ -7,16 +7,16 @@ #include "list.h" /* perform a regex sub. str=source string, rx=regex, replace=replacement string, flags=flags */ -char *regsub (char *str, char *rx, char *replace, int flags); +char *regsub(char *str, char *rx, char *replace, int flags); /* same as regsub, but compiles the regex first */ -char *regsub_c (char *str, regex_t *rx, char *replace); +char *regsub_c(char *str, regex_t *rx, char *replace); /* multiple cascading regsubs */ -char *regsubs (char *str, char **rxs, int flags); +char *regsubs(char *str, char **rxs, int flags); /* match. str=source string, rx=regex, nmatch=matches expected, ... */ -char **regmatch (char *str, char *rx, size_t nmatch, int cflags, int eflags); +char **regmatch(char *str, char *rx, size_t nmatch, int cflags, int eflags); /* same as regmatch but compiles the regex first */ -char **regmatch_c (char *str, regex_t *rx, size_t nmatch, int eflags); +char **regmatch_c(char *str, regex_t *rx, size_t nmatch, int eflags); /* split a string by a regex and return a list */ -LIST *split (char *str, char *rx, int flags); +LIST *split(char *str, char *rx, int flags); #endif /* REGUTIL_H */