////////////////////////////////////////////////// // program entry int main() { printf("Compile starting\n"); // build an expression tree: // + // / \ // + 4 // / \ // 2 3 struct Exp_t *exp = Exp_Sum_new (Exp_Sum_new(Exp_Int_new (2) , Exp_Int_new (3)) , Exp_Int_new (4)); // print out this tree: printf ("the expression is:\n"); Exp_print (exp); // compile this tree to Stack machine instructions compile (exp); // print out the generated Stack instructions: printf("\n\n"); List_reverse_print (all); printf("\nCompile finished\n"); return 0; }
int main() { FILE *Read; int x = 1, y = 1; char str[32]; char data_ch; char state; Read = fopen("text2.txt", "r"); while (0 == feof(Read)){ data_ch = fgetc(Read); state = 1; switch (data_ch){ case 'i': data_ch = fgetc(Read); state++; if (data_ch == 'f'){ data_ch = fgetc(Read); state++; if (data_ch == ' '){ emit(Ch_Flag, x++, y, "IF"); } else if (data_ch != '\n'){ memset(str, '\0', sizeof(str)); Read_Str(state, Read, str); emit(Ch_NoFlag, x++, y, str); } } else if ((data_ch >= 'a') && (data_ch <= 'z') || (data_ch >= 'A') && (data_ch <= 'Z') || (data_ch >= '0') && (data_ch <= '9')){ memset(str, '\0', sizeof(str)); Read_Str(state, Read, str); emit(Ch_NoFlag, x++, y, str); } break; case '\n': y++; x = 1; break; case ' ': break; default: if ((data_ch >= 'a') && (data_ch <= 'z') ){ memset(str, '\0', sizeof(str)); Read_Str(state, Read, str); emit(Ch_NoFlag, x++, y, str); } break; } } List_reverse_print(all); while (1); return 0; }
// "printer" void List_reverse_print (struct List_t *list) { if(list->next != NULL) List_reverse_print(list->next); if(list->instr->kind == STACK_ADD) printf("\nADD"); else printf("\nPUSH %d",((struct Stack_Push *)(list->instr))->i); }
void List_reverse_print(struct List_t *list) { switch (list->kind){ case Ch_Flag:{ if ((list->next) != NULL){ List_reverse_print(list->next); } printf("IF (%d,%d)\n", list->y, list->x); break; } case Ch_NoFlag:{ if ((list->next) != NULL){ List_reverse_print(list->next); } printf("ID(%s) (%d,%d)\n", list->str, list->y, list->x); break; } default: break; } }
int main (int argc, char **argv) { yyparse(); printf ("\n"); Exp_print (tree); // compile this tree to Stack machine instructions printf ("\n"); compile (tree); // print out the generated Stack instructions: printf("\n"); List_reverse_print (all); return 0; }
// "printer" void List_reverse_print (struct List_t *list) { //TODO(); if(list==NULL){ return; } else{ List_reverse_print(list->next); } switch(list->instr->kind){ case STACK_ADD: printf("add\n");break; case STACK_PUSH: printf("push %d\n",((struct Stack_Push *)(list->instr))->i); break; } }
// "printer" void List_reverse_print (struct List_t *list) { if (list->next != 0) List_reverse_print(list->next); switch (list->instr->kind) { case STACK_ADD: { printf("ADD\n"); break; } case STACK_PUSH: { struct Stack_Push *sp = (struct Stack_Push *)list->instr; printf("PUSH %d\n", sp->i); break; } default: break; } }
// "printer" void List_reverse_print (struct List_t *list) { //TODO(); if (list->next != 0){ List_reverse_print(list->next); } switch(list->instr->kind){ case STACK_ADD:{ printf ("\nADD"); break; } case STACK_PUSH:{ struct Stack_Push *p = (struct Stack_Push *)(list->instr); printf ("\nPUSH %d", p->i); break; } default: break; } }