#include #include #include "lex.h" #include "calc.h" number val; int lookahead() /* returns next character without actually reading it */ { if( feof(stdin)) return EOF; int c = getchar(); ungetc(c, stdin); return c; } void whitespaces() /* skips blanks */ { while( isblank(lookahead()) ) getchar(); return; } int lex() { int c; int cp; whitespaces(); c = lookahead(); if( c == EOF) return c; if( isdigit(c) || c == '.'){ scanf(NUMFMT,&val); return NUMBER; } getchar(); /* advance anyway */ if( c == '+' || c == '-'){ /* ** could be a unary + or - in which case a digit should follow */ if( (cp = lookahead()) == '.' || isdigit(cp) ){ scanf(NUMFMT,&val); if( c == '-') val = - val; return NUMBER; } return c; } switch(c){ case '\n': case '/' : case '*' : return c; case 'p' : return POP; case 'P' : return POPALL; default : return UNKNOWN; } /* switch */ }