//Input: Two expressions x and y, and a relational operator op //Output: One conditional expression x op y NC* createConditionalExpression( NC *expr1, char *relOp, NC *expr2 ) { NC *andExpr, *orExpr, *relExpr; NC *newExpr1, *newExpr2; NC *newExpr; int operator; operator = getRelOperatorIndex(relOp); newExpr1 = copylist(expr1); newExpr2 = copylist(expr2); newExpr2 = negateExpression(newExpr2); newExpr = (NC*) malloc (sizeof(NC)); Add_Sums(newExpr1, newExpr2, newExpr); relExpr = (NC*) malloc (sizeof(NC)); relExpr->list = NULL; relExpr->type = 'R'; relExpr->inc = operator; relExpr->link = newExpr; orExpr = (NC*) malloc (sizeof(NC)); orExpr->list = NULL; orExpr->type = 'O'; orExpr->inc = 0; orExpr->link = relExpr; andExpr = (NC*) malloc (sizeof(NC)); andExpr->list = NULL; andExpr->type = 'A'; andExpr->inc = 0; andExpr->link = orExpr; return andExpr; }
//Input: Two normalized expressions x and y, and a flag (type: character) //Output: A normalized expression x/y or x%y depending upon the flag NC* createExpression_Mod_Div( NC *neumerator, NC *denominator, char flag ) { NC *finalExpression, *newNeumerator, *newDenominator; NC *term, *sum; newNeumerator = copylist(neumerator); newDenominator = copylist(denominator); finalExpression = (NC*) malloc (sizeof(NC)); finalExpression->list = NULL; finalExpression->inc = 0; finalExpression->link = newNeumerator; newNeumerator->list = newDenominator; if(flag == '/') finalExpression->type = 'D'; else finalExpression->type = 'M'; term = (NC*) malloc (sizeof(NC)); term->list = NULL; term->type = 'T'; term->inc = 1; term->link = finalExpression; sum = (NC*) malloc (sizeof(NC)); sum->list = NULL; sum->type = 'S'; sum->inc = 0; sum->link = term; return sum; }
LVAL copylist(LVAL from) { if (from == NULL) return NULL; return cons(car(from), copylist(cdr(from))); }
//Input: A string x and a normalized expression y //Output: A normalized expression with x as as an array // and y as its first index expression NC* createArray( char *arrayName, NC *expression ) { NC *sum, *term, *array, *newExpression; int symbolIndex; symbolIndex = indexof_symtab(arrayName); newExpression = copylist(expression); array = (NC*) malloc (sizeof(NC)); array->list = NULL; array->type = 'a'; array->inc = symbolIndex; array->link = newExpression; term = (NC*) malloc (sizeof(NC)); term->list = NULL; term->type = 'T'; term->inc = 1; term->link = array; sum = (NC*) malloc (sizeof(NC)); sum->list = NULL; sum->type = 'S'; sum->inc = 0; sum->link = term; //The next statement adds the array to corresponding var_list if(!(flagVar_List)) indexof_varlist(arrayName, &V0); else indexof_varlist(arrayName, &V1); return sum; }
LOCAL LVAL copylist(LVAL from) { if (from == NULL) { return NULL; } return cons(car(from), copylist(cdr(from))); }
int main(int argc, char**argv) { FreqNode *root; char *alltext; FreqNode *buffer[256] = { NULL }; NodeLList *txtfreqs = newlist(); NodeLList *auxlist; Stack *auxstack; char auxchar; char auxchar2; int i, j; int numvalidsymbols; int highestfreq; int isfirst; if (argc == 1) { printf("Error: Missing filename as argument.\n"); exit(-1); } alltext = readfile(argv[1], buffer); numvalidsymbols = 0; for (i = 0; i < 256; i++) { if (buffer[i] && strcmp(buffer[i]->sym, " ") >= 0 && strcmp(buffer[i]->sym, "~") <= 0 ) { numvalidsymbols++; } } isfirst = 1; for (i = 0; i < numvalidsymbols; i++) { for (j = 32; j < 127; j++) { if (buffer[j] && isfirst) { highestfreq = j; isfirst = 0; } else if (buffer[j] && buffer[j]->freq > buffer[highestfreq]->freq) { highestfreq = j; } } addend(&txtfreqs, buffer[highestfreq]); buffer[highestfreq] = NULL; isfirst = 1; } auxlist = copylist(txtfreqs); root = huffman(&txtfreqs, numvalidsymbols-1); auxstack = newstack(); auxchar = 0 << 7 | 0 << 6 | 0 << 5 | 0 << 4 | 0 << 3 | 0 << 2 | 0 << 1 | 0 << 0; push(&auxstack, auxchar); generatehuffcodes(&root, &auxstack, 0); writefile(argv[1], alltext, root, auxlist, numvalidsymbols); return 0; }
//Input: Two conditions x and y //Output: One condition x AND y (i.e., x && y) NC* createAndExpression( NC *cond1, NC *cond2 ) { NC *traverseOr; NC *t1, *t2, *node; boolean flag; if(cond1 == NULL && cond2 == NULL) return NULL; if(cond1 == NULL) return cond2; if(cond2 == NULL) return cond1; traverseOr = cond1->link; while(traverseOr->list != NULL) traverseOr = traverseOr->list; //Appending (ANDing) cond1 and cond2 //traverseOr->list = cond2->link; //The above-mentioned simplest strategy is neglected //to avoid duplicating the same conjuncts t2 = cond2->link; while( t2 != (NC*)NULL ) { flag = TRUE; t1 = cond1->link; while( t1 != (NC*)NULL ) { if(compare_trees(t1->link, t2->link) == 1) { flag = FALSE; break; } t1 = t1->list; } if(flag) { node = (NC*)malloc(sizeof(NC)); node->list = (NC*)NULL; node->type = 'O'; node->inc = 0; node->link = copylist( t2->link ); traverseOr->list = node; traverseOr = node; } t2 = t2->list; } return cond1; }
//Input: Two relations x = (a or b or c), and y = (b or d) //Output: One relation z = (a or b or c or d) -- devoid of identical clauses NC* compareRelations( NC *rel1, NC *rel2 ) { NC *copyRel1, *copyRel2; NC *r1, *r2, *tempR; boolean flag; if(rel1 == NULL && rel2 == NULL) return NULL; if(rel1 == NULL) return rel2; if(rel2 == NULL) return rel1; copyRel1 = copylist(rel1); copyRel2 = copylist(rel2); tempR = copyRel1; while(tempR->list != NULL) tempR = tempR->list; for(r2 = copyRel2; r2 != NULL; r2 = r2->list) { flag = TRUE; for(r1 = copyRel1; r1 != NULL; r1 = r1->list) { if(compare_trees(r1, r2) == 1) { flag = FALSE; break; } } if(flag) { tempR->list = copylist( r2->link ); tempR = tempR->list; } } return copyRel1; }
/* Make a copy of the obarray so that we can erase any changes the user makes to global variables */ LOCAL void nyx_copy_obarray() { LVAL newarray; int i; // Create and set the new vector. newarray = newvector(HSIZE); setvalue(obarray, newarray); for (i = 0; i < HSIZE; i++) { LVAL from = getelement(nyx_obarray, i); if (from) { setelement(newarray, i, copylist(from)); } } }
//Input: A list of actions (type: DT_LIST) and a transition (type: TRANSITION_ST) //Output: The transition associated with the list of actions void generateAssignments( DT_LIST *actions_list, TRANSITION_ST *trans ) { int i; for(i = 0; i < actions_list->numOperations; i++) { //Copy DATA_TRANS trans->action[i].lhs = actions_list->action[i].lhs; trans->action[i].rhs = copylist(actions_list->action[i].rhs); } //The following assignments are needed to denote //"end of action" has been reached trans->action[i].lhs = -1; trans->action[i].rhs = NULL; }
//Input: A normalized expression x //Output: Negate of x (Sum's and Term's of x are multiplied by -1) NC* negateExpression( NC* expression ) { NC *temp, *newExpression; newExpression = copylist(expression); newExpression->inc = (newExpression->inc) * -1; temp = newExpression->link; while(temp != NULL) { temp->inc = (temp->inc) * -1; temp = temp->list; } return newExpression; }
//Input: An array x (of some dimension) and a normalized expression y //Output: The array x with an increased dimension (with index value y) NC* addArrayDimension( NC *array, NC *expression ) { NC *temp, *newExpression; newExpression = copylist(expression); //currently array points to Sum (S), so traverse downward //to reach array's first dimension temp = array->link->link->link; //traverse the list until the last dimension is reached while(temp->list != NULL) temp = temp->list; temp->list = newExpression; return array; }
/* Make a copy of the obarray so that we can erase any changes the user makes to global variables */ void nyx_save_obarray() { LVAL array, obarrayvec; int i; xlsave1(array); array = newvector(HSIZE); obarrayvec = getvalue(obarray); for(i=0; i<HSIZE; i++) { LVAL from = getelement(obarrayvec, i); if (from) setelement(array, i, copylist(from)); } nyx_old_obarray = obarray; obarray = xlmakesym("*OBARRAY*"); setvalue(obarray, array); xlpop(); }
static LVAL elementlist P1C(LVAL, x) { LVAL next, last, result; if (!compoundp(x)) result = consa(x); else { xlprot1(x); x = compounddataseq(x); x = (listp(x)) ? copylist(x) : coerce_to_list(x); if (all_simple(x)) result = x; else { for (next = x; consp(next); next = cdr(next)) rplaca(next, elementlist(car(next))); result = car(x); last = lastcdr(car(x)); for (next = cdr(x); consp(next); next = cdr(next)) { rplacd(last, car(next)); last = lastcdr(car(next)); } } xlpop(); } return(result); }
//Input: A conditional expression x (at OR level) //Output: The conditional expression !x NC* computeNegation( NC *cond ) { NC *retOr, *oldOr, *newOr; NC *iRel, *oneRel; NC *retNegCond, *jRet; //base case if(cond->list == NULL) { for(iRel = cond->link; iRel != NULL; iRel = iRel->list) { oneRel = (NC*) malloc (sizeof(NC)); oneRel->list = NULL; oneRel->type = 'R'; oneRel->inc = negateoperator(iRel->inc); oneRel->link = copylist(iRel->link); newOr = (NC*) malloc (sizeof(NC)); newOr->list = NULL; newOr->type = 'O'; newOr->inc = 0; newOr->link = oneRel; if(iRel == cond->link) retOr = newOr; else oldOr->list = newOr; oldOr = newOr; } return retOr; } //inductive case retNegCond = computeNegation( cond->list ); for(iRel = cond->link; iRel != NULL; iRel = iRel->list) { for(jRet = retNegCond; jRet != NULL; jRet = jRet->list) { oneRel = (NC*) malloc (sizeof(NC)); oneRel->list = copylist(jRet->link); oneRel->type = 'R'; oneRel->inc = negateoperator(iRel->inc); oneRel->link = copylist(iRel->link); newOr = (NC*) malloc (sizeof(NC)); newOr->list = NULL; newOr->type = 'O'; newOr->inc = 0; newOr->link = oneRel; if(iRel == cond->link) retOr = newOr; else oldOr->list = newOr; oldOr = newOr; } } return retOr; }