0

I have been using flex and bison for making a small calculator. My files are the following:

bisonFile.y

%{
#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 { $$ = $2 >= 0? $2 : - $2; }
;
%%
main(int argc, char **argv)
{
yyparse();
}
yyerror(char *s)
{
fprintf(stderr, "error: %s\n", s);
}

flexFile.l

%{
# include "f5.tab.h"
int yylval;
%}
/* reconocimiento de tokens e impresion */
%{
int yylval;
%}
%option noyywrap
%%
"+" { return ADD; }
"-" { return SUB; }
"*" { return MUL; }
"/" { return DIV; }
"|" { return ABS; }
[0-9]+("."[0-9]+)? { yylval = atoi(yytext); return NUMBER; }   //part added
\n { return EOL; }
[ \t] { /* ignore whitespace */ }
. { printf("Mystery character %c\n", *yytext); }
%%

My program works fine with integer numbers, and it also recognizes real numbers, but the problem is that when I print the results of an operation it always return the answer as an integer number. Why is that?

Thanks

splash
  • 13,037
  • 1
  • 44
  • 67
Little
  • 3,363
  • 10
  • 45
  • 74

2 Answers2

0

Your use of atoi in the production converts the string to an integer. Using atof will convert it to a floating point number. If you want to separate the two, you'll need to change the matching rule for integers, and add one for floating point.

MIS
  • 76
  • 3
0

Change "%d""%f" in the file “bisonFile.y”. This uses a floating point format for printing the result. The fixed line should read:

| calclist exp EOL { printf("= %f\n", $2); }

In the file “flexFile.l” remove both definitions int yylval. bison outputs

YYSTYPE yylval;

automatically. YYSTYPE is the type of the semantic values. Because you want a floating point calculator, this shall be double. Note that YYSTYPE defaults to int. To change that, YYSTYPE must be defined when compiling the C-codes (from bison and flex) (see below).

Finally, as already stated by MIS, replace atoi()atof(). The edited line in flexFile.l should read:

[0-9]+("."[0-9]+)? { yylval = atof(yytext); return NUMBER; }

For a novice the dependencies between flex and bison sources might be confusing. A minimal Makefile documents how the example can be compiled. Line 2 sets the semantic type for the scanner and the parser consistently:

calc:               calc.o l.o
calc.o l.o:         CFLAGS+=-DYYSTYPE=double
l.o:                l.c f5.tab.h
calc.c f5.tab.h:    bisonFile.y
                    bison -o $@ --defines=f5.tab.h $^
l.c:                flexFile.l f5.tab.h
                    flex -o $@ $^
clean::
                    $(RM) calc calc.o calc.c f5.tab.h l.o l.c

That’ll do the trick.

hermannk
  • 745
  • 8
  • 12
  • You should remove (both) declarations of `yylval`. It is defined in the bison generated C file and declared `extern` in the generated bison header, so an explicit declaration is redundant and two of them doubly so. – rici May 12 '17 at 16:06
  • @rici: Thanks. That’s cleaner. I’ll rework my answer. – hermannk May 29 '17 at 12:39