/* calc.l simple postfix calculator written in lex Daniel Scharstein 2/18/08 */ %{ # include # include void push(int n); int pop(); int stack[1000]; int sp = 0; %} digit [0-9] num -?{digit}+ %% {num} { push(atoi(yytext)); } "+" { push(pop() + pop()); } "-" { int a = pop(); push(pop() - a); } "*" { push(pop() * pop()); } "/" { int a = pop(); push(pop() / a); } [ \t] {} "\n" { printf("result=%d\n", pop()); } . { printf("syntax error\n"); } %% void push(int n) { stack[++sp] = n; } int pop() { return stack[sp--]; }