#include #include #include #include #define NDEBUG #define BUFSIZE 1024 #define bail(arg...) do { fprintf(stderr, ##arg); exit(EXIT_FAILURE); } while (0) char *arg; int istack[BUFSIZE]; char opstack[BUFSIZE]; int istackp,opstackp; #ifdef NDEBUG #define dump(x) #else static void dump(char *pref) { int i; fprintf(stderr,"%s: i[",pref); for (i=0;i=mypri) evalit(); if (!op) return; if (op==')') { if (!opstackp || opstack[opstackp-1]!='(') bail("Not enough content for brackets-inside space!\n"); opstackp--; return; } if (opstackp>=BUFSIZE) bail("Too many operators (%d is max)!\n",BUFSIZE); opstack[opstackp++]=op; } static void process(void) { char lastop,op; int curi; istackp=opstackp=0; for (lastop=0;;lastop=op) switch (op=lex(&curi)) { case 'n': if (lastop=='n') bail("Two successive numbers not permitted!\n"); if (istackp>=BUFSIZE) bail("Too many operators (%d is max)!\n",BUFSIZE); istack[istackp++]=curi; break; case '(': if (lastop=='n') bail("Open-bracket not permitted after number!\n"); pushop(op); break; case '+': case '-': case '*': case '/': case ')': pushop(op); break; case 0: pushop(op); if (istackp!=1||opstackp) bail("Machine not in sane state on the end of expression, ints=%d,ops=%d!\n",istackp,opstackp); printf("%d\n",istack[0]); return; default: assert(0); } } int main(int argc,char **argv) { int argn; for (argn=1;argn