#include #include #include "lex.h" #include "stack.h" void skipLine(); void binop(int op); int calc(); int main() { return calc(); } int calc() { int token; while( (token = lex()) != EOF){ switch(token){ case '\n': printStack(); break; case NUMBER: push(val); break; case POP: if( !isStackEmpty() ) pop(); break; case POPALL: emptyStack(); break; case UNKNOWN: printf("lex error skipping till end of the line\n"); break; default: binop(token); } /* switch */ }/* while */ return 0; } void binop(int op) { long double a,b; if( isStackEmpty() ){ printf("stack underfull\n"); skipLine(); return; } b = pop(); if( isStackEmpty() ){ printf("stack underfull\n"); push(b); return; } a = pop(); switch(op){ case '+' : push(a+b); break; case '-' : push(a-b); break; case '*' : push(a*b); break; case '/' : push(a/b); break; } return; } void skipLine() { int token; while( (token = lex()) != EOF && token != '\n'); }