////////////////////////////////////////////////// // 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; }
void parser(){ // while(operators->top != 0 && pop(operators)) while(operators->tail != operators->head && dequeue(operators)) exp = Exp_Sum_new((struct Exp_t *) exp, //Exp_Int_new(pop(numbers) - '0')); Exp_Int_new(dequeue(numbers) - '0')); }
void parser_init(char * str){ //numbers = stack_char_new(CAPACITY); //operators = stack_char_new(CAPACITY/2); numbers = queue_new(CAPACITY); operators = queue_new(CAPACITY/2); if(!str) exit(-1); char *p = str; while(*p){ if ('0' <= *p && '9' >= *p) //push(numbers,*p); inqueue(numbers,*p); else if ('+' == *p) //push(operators,*p); inqueue(operators,*p); p++; } //exp = Exp_Int_new(pop(numbers)-'0'); exp = Exp_Int_new(dequeue(numbers)-'0'); }