Search code examples
cbisonflex-lexer

Trying to make calculator with bison and flex, but it only prints out 0


I have been following a tutorial from the book flex and bison by John Levine where I am trying to make a calculator in C with the help of flex and bison. However, when i try to use my implementation it only prints out 0 no matter the calculation i enter. I have been trying to find the problem for hours know but i can't see whats wrong. Here is my flex, bison and makefile in respective order:

calc.l

%{
 #include "calc.tab.h"
%}
%%
"+"     { return ADD; }
"-"     { return SUB; }
"*"     { return MUL; }
"/"     { return DIV; }
"|"     { return ABS; }
[0-9]+  { yylval = atoi(yytext); return NUMBER; }
\n      { return EOL; }
[ \t]   { }
"//".   { }
"("     { return OP; }
")"     { return CP; }
.       { printf("Unkown character: %s\n", yytext); }
%%

calc.y

%{
#include <stdio.h>
%}

%token NUMBER
%token ADD SUB MUL DIV ABS
%token EOL
%token OP CP

%%

calclist:
  | calclist exp EOL { printf("= %d\n", $1); }
  ;

exp: factor        { $$ = $1; }
  | exp ADD factor { $$ = $1 + $3; }
  | exp SUB factor { $$ = $1 - $3; }
  ;

factor: term        { $$ = $1; }
  | factor MUL term { $$ = $1 * $3; }
  | factor DIV term { $$ = $1 / $3; }
  ;

term: NUMBER  { $$ = $1; }
  | ABS term  { $$ = $2 >= 0? $2 : - $2; }
  | OP exp CP { $$ = $2; }
  ;
%%
main(int argc, char **argv)
{
  yyparse();
}

yyerror(char *s)
{
  fprintf(stderr, "error: %s\n", s);
}

makefile

calc: calc.l calc.y
      bison -d calc.y
      flex calc.l
      cc -o $@ calc.tab.c lex.yy.c -lfl

Solution

  • In this production:

    calclist exp EOL { printf("= %d\n", $1); }
    

    $1 refers to calclist, but nothing has given that non-terminal a value.

    What has a value, and what you actually want to print, is exp, which is the second symbol in the right-hand side, and thus corresponds to $2.