#include #include #include #define MAX_LINE_LENGTH 1000 #define MAX_ARITY 10 typedef struct te { char name; struct te * sub_terms[MAX_ARITY]; int arity; int variable; int constant; struct te * parent; } term; typedef struct v_as { int variable; term * parent; } variable_assignment; typedef struct as { int variable; variable_assignment assigment;; struct as * rest; } assignment; void make_term( term * base, char * line, char ** stop) { term* current_term = base; char * pos = line; int arg = 0; // a counter for determing which argument we are on. char temp[MAX_LINE_LENGTH]; char * place; char ** stop_point; place = pos; stop_point = &place; if (isalpha(*pos) && isupper(*pos)) { current_term->name = *pos; current_term->variable = 1; //fprintf(stderr, "Found a variable %c\n", *pos); stop[0] = pos+1; return; } if (isalpha(*pos) && islower(*pos)) { current_term->name = *pos; } if (*(pos+1) != '(') { current_term->constant = 1; //fprintf(stderr, "Found a constant %c\n", *pos); stop[0] = pos+1; return; } else { //fprintf(stderr, "Found a function %c\n", *pos); // there are arguments. First we call make_term to get the first argument current_term->arity=1; current_term->sub_terms[arg] = (term *)calloc(1,sizeof(term)); if ( current_term->sub_terms[arg] == NULL) { fprintf(stderr, "Malloc Failure\n"); exit(1); } current_term->sub_terms[arg]->parent = current_term; pos = pos+2;// to skip the function name and the ( while (1) { //fprintf(stderr, "looking for arg %d from %s\n",arg,pos); make_term( current_term->sub_terms[arg],pos,stop_point); place = stop_point[0]; //fprintf(stderr, "After make_term place is %s\n",place); if (*place == ',') { //fprintf(stderr, "Found a comma, so looking for another argument from %s\n",place); current_term->arity++; arg++; current_term->sub_terms[arg] = (term *)calloc(1,sizeof(term)); if ( current_term->sub_terms[arg] == NULL) { fprintf(stderr, "Malloc Failure\n"); exit(1); } current_term->sub_terms[arg]->parent = current_term; } else { if (*place == ')') { //fprintf(stderr, "Found a ), so arity is %d continuing on from %s\n",current_term->arity,place+1); stop[0] = place+1; break; } else { fprintf(stderr, "Parse error\n"); exit(1); }} pos = place+1; // to skip the , } } } void print_term(term * cl) { int i; if (isalpha(cl->name) && isupper(cl->name)) { fprintf(stderr,"%c",cl->name); return; } if (isalpha(cl->name) && islower(cl->name)) { if (cl->constant == 1) fprintf(stderr,"%c",cl->name); else { fprintf(stderr,"%c(",cl->name); for(i = 0; i < cl->arity; i++) { print_term(cl->sub_terms[i]); if (i +1 < cl->arity) fprintf(stderr, ","); } fprintf(stderr, ")"); return; }} } int unify(term * term_one,term * term_two,assignment * ass) { return(0); } int main(int argc, char **argv) { char * read_val; char line_one[MAX_LINE_LENGTH]; char line_two[MAX_LINE_LENGTH]; // read in from stdin if (NULL == fgets(line_one,MAX_LINE_LENGTH,stdin)) { fprintf(stderr, "No input line\n"); exit(1); } if (NULL == fgets(line_two,MAX_LINE_LENGTH,stdin)) { fprintf(stderr, "No 2nd input line\n"); exit(1); } // work out the lenght of the two terms int length_one =1; int length_two = 1; char * pos; char * * place; place = &pos; term * term_one; term_one = calloc(1,sizeof(term)); term * term_two; term_two = calloc(1,sizeof(term)); if (term_one == NULL || term_two == NULL) { fprintf(stderr, "Malloc failure\n"); exit(1); } pos = line_one; int negative = 0; make_term(term_one,line_one,place); make_term(term_two,line_two,place); print_term(term_one); fprintf(stderr, "\n"); fprintf(stderr, "\n"); print_term(term_two); fprintf(stderr, "\n"); fprintf(stderr, "\n"); // }