#include "structures.c" struct atom *get_clause_head(); struct list *read_clause_body(); void write_clause(struct clause *clause); void write_atom(struct atom *a); struct atom *read_atom(); struct list *cons(void *object, struct list *tail); struct list *read_term_list(); void write_list(struct list *l, char list_type); char next; FILE *input_file; extern struct list *prop_table[]; void skip_space() { while ((next==' ' || next=='\n')&& next!=EOF) { next=getc(input_file); } } term_pointer read_term() { int i=0; term_pointer t; term_pointer *temp; term_pointer buffer[20]; int arity=0; t=(struct term *)malloc(sizeof(struct term)); if (next=='(') { next=getc(input_file); skip_space(); t->functor=(void *)next; t->term_type=NONVAR; next=getc(input_file); skip_space(); while ((next!=')')&&(i<100)) { buffer[arity]=read_term(); arity++; i++; } t->arity=arity; if (arity==0) t->argument_array=NULL; else { t->argument_array=(term_pointer *) calloc(arity,sizeof(term_pointer)); for (i=0;iargument_array[i]=buffer[i]; } } next=getc(input_file); skip_space(); } else if (next=='[') { t->functor=read_term_list(); t->term_type=LIST; } else { t->functor=(void *)next; if ('0'<=((int)t->functor) && ((int)t->functor)<='9') { t->term_type=VARIABLE; t->functor=(void *)((int)t->functor-'0'); } else t->term_type=NONVAR; t->arity=0; t->argument_array=NULL; next=getc(input_file); skip_space(); } return t; } void write_term(term_pointer t) { int i; if (t->arity>0) printf("("); if (t->term_type==VARIABLE) putchar((int)t->functor+'0'); else if (t->term_type==LIST) write_list(t->functor,'t'); else putchar((int)t->functor); putchar(' '); for (i=0;i<(t->arity);i++) { write_term(t->argument_array[i]); } if (t->arity>0) printf(") "); } struct clause *read_clause() { int i; struct clause *cl=NULL; if (next!='[') {printf("no clause\n"); exit(0);} else { next=getc(input_file); skip_space(); cl=(struct clause *)malloc(sizeof(struct clause)); cl->head=read_atom(); cl->body=read_clause_body(); } update_clause_tables(cl); return cl; } struct atom *read_atom() { struct atom *a; if (next !='<') {printf("no atom\n"); exit(0);} else { a=(struct atom *)malloc(sizeof(struct atom)); next=getc(input_file); skip_space(); switch (next) { case 'A': a->type=AB;break; case 'N': a->type=NON_AB;break; case 'R': a->type=RECURSIVE;break; case 'I': a->type=INCOMPLETE;break; case 'D': a->type=DIRECT;break; } next=getc(input_file); skip_space(); switch (next) { case 'A': a->action_type=ACTION;break; case 'N': a->action_type=NON_ACTION;break; } next=getc(input_file); skip_space(); a->term=read_term(); if (next!='>') {printf("no '>'\n");exit(0);} next=getc(input_file); skip_space(); } return a; } struct list *read_clause_body() { struct list *ptr=NULL; while (next!=']') ptr=cons(read_atom(),ptr); next=getc(input_file); skip_space(); return ptr; } void write_clause(struct clause *clause) { struct list *temp; struct atom *body_atom; temp=clause->body; printf("["); write_atom(clause->head); while (temp) { body_atom=temp->object; write_atom(body_atom); temp=temp->next; } printf("]"); } void write_atom(struct atom *a) { printf("<"); printf("%d %d",a->type,a->action_type); write_term(a->term); printf(">"); } extern struct list *clause_head_table[]; extern struct list *clause_body_table[]; struct list *read_term_list_body() { if (next==']') { next=getc(input_file); skip_space(); return NULL; } else return cons(read_term(),read_term_list_body()); } struct list *read_term_list() { if (next!='[') {printf("no '['\n");exit(0);}; next=getc(input_file);skip_space(); return read_term_list_body(); } void write_list(struct list *l, char list_type) { printf("["); while (l) { switch (list_type) { case 'a': write_atom(l->object);break; case 't': write_term(l->object);break; case 'i': printf("%d ",l->object);break; } l=l->next; } printf("]"); } void main() { struct clause *cl; struct clause *cl1; struct term *t; struct term *t1; struct atom *a1; struct atom *a2; struct list *l; struct clause *rec; struct clause *exp; struct list *atom_list; struct list *var_list; struct proposition *prop; struct indexed_clause *i_c; input_file=fopen("test","r"); next=getc(input_file);skip_space(); l=read_term_list(); rec=read_clause(); exp=cl_expand2(l,rec); write_clause(exp); }