I want to implement the abs operator in Flex and Bison; so far the program that I made is the following:
Bison file
%{
#include <stdio.h>
%}
/* declare tokens */
%token NUMBER
%token ADD SUB MUL DIV ABS
%token EOL
%%
calclist: /* nothing */
| calclist exp EOL {
printf("= %d\n", $2);
}
;
exp: factor
| exp ADD factor { $$ = $1 + $3; }
| exp SUB factor { $$ = $1 - $3; }
;
factor: term
| factor MUL term { $$ = $1 * $3; }
| factor DIV term { $$ = $1 / $3; }
;
term: NUMBER
| ABS term {if ($2>=0) $$=$2;
else $$= - $2;}
;
%%
main(int argc, char **argv)
{
yyparse();
}
yyerror(char *s)
{
fprintf(stderr, "error: %s\n", s);
}
Flex file
%{
# include "f5.tab.h"
%}
/* recognize tokens for the calculator and print them out */
%%
"+" { return ADD; }
"-" { return SUB; }
"*" { return MUL; }
"/" { return DIV; }
"|" { return ABS; }
[0-9]+ { yylval = atoi(yytext);
return NUMBER;
}
\n { return EOL; }
[ \t] { /* ignore whitespace */ }
. {
printf("Mystery character %c\n", *yytext);
}
%%
yywrap()
{
}
I compile it with:
bison -d bisonfile.y
flex flexfile.l
gcc bisonfile.tab.c lex.yy.c -o test.exe
Then when I enter one expression like the following:
34+|3
it outputs 37
but when I put something like:
34+|-3
it prints "syntax error"
Why is that? Any help would be valuable.
Thanks
NUMBER, ADD, ABS, SUB, NUMBER
which doesn't get handled by your parsing rules. You probably want to consider+
and-
as possible unary operations as well – Rorschachunary_expr: SUB ... | ...
– Rorschach