void printProgram(NODE *program){ NODE *node=program; while(node!=NULL){ printf("[%s %d", node->instruction, node->number); if (node->subroutine!=NULL){ printProgram(node->subroutine); } printf("]"); node=node->next; } }
int main(int argc, char ** argv) { FILE *input; FILE *output; Program parse_tree; char *str; char *fileName; if (argc > 1) { input = fopen(argv[1], "r"); if (!input) { fprintf(stderr, "Error opening input file.\n"); exit(1); } } else input = stdin; /* The default entry point is used. For other options see Parser.h */ parse_tree = pProgram(input); if (parse_tree) { printf("\nParse Succesful!\n"); printf("\n[Abstract Syntax]\n"); printf("%s\n\n", showProgram(parse_tree)); printf("Parsing AST for doge now...\n"); visitProgram(parse_tree); printf("Done!\n"); str = printProgram(parse_tree); if (input != stdin) { fileName = strdup(argv[1]); fileName = realloc(fileName, strlen(fileName) + 3); strcat(fileName, ".c"); output = fopen(fileName, "w"); fwrite(str, strlen(str), 1, output); printf("Wrote program to %s\n", fileName); free(fileName); } else { printf("[Linearized Tree]\n"); printf("%s\n\n", str); } if (str) free(str); return 0; } return 1; }
void ProgramGrounder::ground() { //Create the dependency graph statementDependency->createDependencyGraph(predicateTable); // Create the component graph and compute an ordering among components. // Components' rules are classified as exit or recursive. // An rule occurring in a component is recursive if there is a predicate belonging // to the component in its positive body, otherwise it is said to be an exit rule. vector<vector<Rule*>> exitRules; vector<vector<Rule*>> recursiveRules; vector<unordered_set<index_object>> componentPredicateInHead; statementDependency->createComponentGraphAndComputeAnOrdering(exitRules, recursiveRules, componentPredicateInHead); if (Options::globalOptions()->isPrintRewrittenProgram()) printProgram(exitRules, recursiveRules); // Ground each module according to the ordering: // For each component, each rule is either recursive or exit, // Exit rules are grounded just once, while recursive rules are grounded until no more knowledge is derived for (unsigned int component = 0; component < exitRules.size(); component++) { #if DEBUG == 1 cout<<"Component: "<<component; cout<<"\tExit rules: "<<exitRules[component].size(); cout<<"\tRecursive rules: "<<recursiveRules[component].size()<<endl; #endif // Ground exit rules for (Rule* r : exitRules[component]){ inizializeSearchInsertPredicate(r); groundRule(r); #ifdef DEBUG_RULE_TIME Timer::getInstance()->print(); #endif } // Ground recursive rules if (recursiveRules[component].size() > 0) { unsigned int n_rules = recursiveRules[component].size(); bool found_something = false; // First iteration for (unsigned int i = 0; i < n_rules; i++) { Rule *rule=recursiveRules[component][i]; inizializeSearchInsertPredicate(rule,componentPredicateInHead[component]); if(groundRule(rule)) found_something=true; } while (found_something) { found_something = false; // Since in the first iteration search is performed in facts and no facts tables, // while in the next iteration search is performed in the delta table, it is needed // to keep track if the current iteration is the first or not. for (unsigned int i = 0; i < n_rules; i++) { Rule* r = recursiveRules[component][i]; //If no more knowledge is derived the grounding of this component can stop #if DEBUG == 1 r->print(); #endif inizializeRecursiveCombinationPredicate(r,componentPredicateInHead[component]); for(unsigned i=0;i<pow(2,predicate_combination.size())-1;i++){ computeRecursiveCombinationPredicate(); nextSearchInsertPredicate(r,componentPredicateInHead[component]); if (groundRule(r)){ found_something = true; } } } for (unsigned int i = 0; i < n_rules; i++) // Move the content of the delta table in the no fact table, // and fill delta with the content of the next delta table. swapInDelta(recursiveRules[component][i]); } } } // Constraints are grounded at the end for (unsigned int i = 0; i < statementDependency->getConstraintSize(); i++) if (statementDependency->getConstraint(i)->getSizeBody() > 0){ Rule *rule=statementDependency->getConstraint(i); inizializeSearchInsertPredicate(rule); groundRule(rule); } //Print and simplify the rule // evaluator.printAndSimplify(predicateExtTable); }
int main() { int generation = 0, i; FILE *fp; extern float minFitness, maxFitness, avgFitness; extern int curCrossovers, curMutations; extern int curPop; void printProgram( int, int ); srand(time(NULL)); curPop = 0; fp = fopen("stats.txt", "w"); if (fp == NULL) exit(-1); initPopulation(); performFitnessCheck( fp ); while (generation < MAX_GENERATIONS) { curCrossovers = curMutations = 0; performSelection(); /* Switch the populations */ curPop = (curPop == 0) ? 1 : 0; performFitnessCheck( fp ); if ((generation++ % 100) == 0) { printf("Generation %d\n", generation-1); printf("\tmaxFitness = %f (%g)\n", maxFitness, MAX_FIT); printf("\tavgFitness = %f\n", avgFitness); printf("\tminFitness = %f\n", minFitness); printf("\tCrossovers = %d\n", curCrossovers); printf("\tMutation = %d\n", curMutations); printf("\tpercentage = %f\n", avgFitness / maxFitness); } if ( generation > (MAX_GENERATIONS * 0.25) ) { if ((avgFitness / maxFitness) > 0.98) { printf("converged\n"); break; } } if (maxFitness == MAX_FIT) { printf("found solution\n"); break; } } printf("Generation %d\n", generation-1); printf("\tmaxFitness = %f (%g)\n", maxFitness, MAX_FIT); printf("\tavgFitness = %f\n", avgFitness); printf("\tminFitness = %f\n", minFitness); printf("\tCrossovers = %d\n", curCrossovers); printf("\tMutation = %d\n", curMutations); printf("\tpercentage = %f\n", avgFitness / maxFitness); for (i = 0 ; i < MAX_CHROMS ; i++) { if (populations[curPop][i].fitness == maxFitness) { int index; printf("Program %3d : ", i); for (index = 0 ; index < populations[curPop][i].progSize ; index++) { printf("%02d ", populations[curPop][i].program[index]); } printf("\n"); printf("Fitness %f\n", populations[curPop][i].fitness); printf("ProgSize %d\n\n", populations[curPop][i].progSize); printProgram(i, curPop); break; } } return 0; }
/* debuger */ void debug(void){ int numberInstructions,tempInstruction,j; unsigned int sizeArray, i; char inputString[5]; numberInstructions=fileSize/sizeof(programPointer[0]); programCounter=0; printf("Ninja Virtual Machine started\n"); printProgram(programPointer); while(programCounter<numberInstructions){ printf("DEBUG: i (inspect), l (list), s (step), r (run), q (quit)?: "); scanf("%s",inputString); if(strcmp(inputString,"i")==0){ printf("DEBUG, inspect: s (stack), o (object) ?: "); scanf("%s", inputString); if(strcmp(inputString, "s")== 0){ for(j=stackPointer;j>-1;j--){ /* stack von oben nach unten durchgehen */ if(j==stackPointer && j==framePointer){ printf("sp, fp ---> 0x%08x: (xxxxxx) xxxx\n",j); }else if(j==stackPointer){ printf("sp -------> 0x%08x: (xxxxxx) xxxx\n",j); }else{ char * ausgabeString; if(j==framePointer){ ausgabeString = "fp -------> "; }else{ ausgabeString = " "; } printf("%s 0x%08x: (%s): ",ausgabeString, j, getTypeOfVariable(j)); if(stack[j].isNumber == true){ printf("0x%08x\n", stack[j].u.number); }else{ if(getHeapAddress(j) == NULL){ printf("(nil)\n"); }else{ printf("%p\n", getHeapAddress(j)); } } } } printf("--- bottom of stack ---\n"); }else if(strcmp(inputString, "o")== 0){ ObjRef temp; printf("object reference? 0x"); scanf("%p", (void **) &temp); printf("VMT: 0x%08x\n", temp->vmt); /* print obj */ if(OBJ_HAS_BYTES(temp)){ for(i=0;i<4;i++){ printf("byte[%d] = 0x%02x\n", i, temp->data.byte[i]); } } if(OBJ_HAS_OBJREF(temp)){ sizeArray = COUNT_FROM_OBJREF(temp); printf("object holds %d fields\n", sizeArray); for(i=0;i<sizeArray;i++){ if(temp->data.field[i]!=NULL){ printf("field[%d] = %p\n",i,(void *)temp->data.field[i]); }else{ printf("field[%d] = nil\n",i); } } } printProgram(programPointer); } }else if(strcmp(inputString,"l")==0){ tempInstruction=programCounter; /* temporaere speicherung programCounter */ for(programCounter=0;programCounter<numberInstructions;programCounter++){ if(programCounter == tempInstruction){ printf("\033[31m"); printProgram(programPointer); printf("\033[0m"); }else{ printProgram(programPointer); } } printf("--- end of code ---\n"); programCounter=tempInstruction; printProgram(programPointer); }else if(strcmp(inputString,"s")==0){ /* naechster programcode wird ausgegeben und ausgefuehrt */ program(programPointer); programCounter++; printProgram(programPointer); }else if(strcmp(inputString,"r")==0){ /* program laeuft normal ab */ for(;programCounter<numberInstructions;programCounter++){ program(programPointer); } break; }else if(strcmp(inputString,"q")==0){ /* beenden des debugers */ break; }else{ printf("Command unknown, try again\n"); } } }