%{
#include <stdio.h>
#include <stdlib.h>
extern int yylex();
extern int yylval;
extern int yyparse();
%}
%token NUMBER
%%
expr:
expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| '+' expr
{ $$
= $
2; } // Unary plus does nothing
, just returns the
number | '-' expr
{ $$
= -$
2; } // Unary minus negates the
number | '*' expr { $$ = -$2; } // Treat * as unary minus
| '/' expr { $$ = -$2; } // Treat / as unary minus
;
%%
int main() {
printf("Enter an expression: ");
yyparse();
return 0;
}
int yyerror(const char *s) {
fprintf(stderr, "%s\n", s);
return 0;
}
JXsKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KZXh0ZXJuIGludCB5eWxleCgpOwpleHRlcm4gaW50IHl5bHZhbDsKZXh0ZXJuIGludCB5eXBhcnNlKCk7CiV9CgoldG9rZW4gTlVNQkVSCgolJQoKZXhwcjoKICAgIGV4cHIgJysnIGV4cHIgeyAkJCA9ICQxICsgJDM7IH0KICB8IGV4cHIgJy0nIGV4cHIgeyAkJCA9ICQxIC0gJDM7IH0KICB8ICcrJyBleHByICAgICAgeyAkJCA9ICQyOyB9ICAvLyBVbmFyeSBwbHVzIGRvZXMgbm90aGluZywganVzdCByZXR1cm5zIHRoZSBudW1iZXIKICB8ICctJyBleHByICAgICAgeyAkJCA9IC0kMjsgfSAvLyBVbmFyeSBtaW51cyBuZWdhdGVzIHRoZSBudW1iZXIKICB8ICcqJyBleHByICAgICAgeyAkJCA9IC0kMjsgfSAvLyBUcmVhdCAqIGFzIHVuYXJ5IG1pbnVzCiAgfCAnLycgZXhwciAgICAgIHsgJCQgPSAtJDI7IH0gLy8gVHJlYXQgLyBhcyB1bmFyeSBtaW51cwogIHwgTlVNQkVSICAgICAgICB7ICQkID0gJDE7IH0KICA7CgolJQoKaW50IG1haW4oKSB7CiAgICBwcmludGYoIkVudGVyIGFuIGV4cHJlc3Npb246ICIpOwogICAgeXlwYXJzZSgpOwogICAgcmV0dXJuIDA7Cn0KCmludCB5eWVycm9yKGNvbnN0IGNoYXIgKnMpIHsKICAgIGZwcmludGYoc3RkZXJyLCAiJXNcbiIsIHMpOwogICAgcmV0dXJuIDA7Cn0K