Take the 2-minute tour ×

I am using the following lex file to convert numbers into tokens. However, the program is not able to parse floating-point numbers correctly. For debugging, I have added the printf statements, and they show that strtof function is unable to parse numbers correctly. Thus, giving the input 4.2 gives me the output 0.

#include <stdlib.h>
void yyerror(char *);
#include "y.tab.h"
char *p = NULL;


[0-9]+\.[0-9]+  {
            printf("%s", yytext);
            yylval = strtof(yytext, &p);
            printf("%f has been read\n", yylval);
            return FLOAT;   
            printf("%d (int) has been read\n", yylval); 
                return INTEGER;

[-+*/\n]    {   printf("%s has been read", yytext);
            return *yytext;

[ \t]       ;   /* skip whitespace */
.               {
            printf("Invalid character %s", yytext);
            yyerror("invalid character");

int yywrap(void) {
return 1;

Any ideas where is the problem in the above code? I have used strtof function (outside lex) with the same input, and it seems to work perfectly.


share|improve this question
I removed the Flex tag; as this question has nothing to do w/ the UI Framework and added the gnu-flex tag which is generally used for the lexical analyzer. –  Reboog711 Dec 9 '12 at 17:35
@www.Flextras.com Thanks. –  Arani Dec 9 '12 at 17:36
There are other problems here. You should define yylval as a union and use it accordingly. You shouldn't include \n in the rule for social characters, and you should include it in the rule for white space. You can eliminate the rule for special characters by having the rule for . return *yytext and letting the parser deal with illegal characters: you get better error recovery that way too. –  EJP Dec 9 '12 at 20:23
add comment

1 Answer

up vote 1 down vote accepted


Sadly, no. The printf statements show that yylval if interpreted as a floating point number prints out as 0 with format %f. It says nothing about the result of strtof.

Independent of lex, the following two statements cannot meaningfully appear in the same scope in a C program:



yylval is either an int or a double, or neither, but it cannot be both. Note that the format in printf amounts to a promise: if you use %f, you're promising printf that you will pass it a double. It has no way to verifying that, so it takes you at your word and interprets whatever argument it gets as a double. If you lie to printf, you will get meaningless output. If you compile your programs with reasonable warning levels (-Wall on gcc or clang), the compiler will warn you about obvious transgressions. I strongly recommend that you do this.

Note that by default yylval is an int. If you haven't done anything to change that, then that's what you have.

share|improve this answer
I have defined yylval as a float (by using #define YYSTYPE float in yacc file). But this does not give me the desired result, even if I use only floating-point inputs. –  Arani Dec 10 '12 at 6:33
You can put #define YYSTYPE float in your yacc file, but I don't believe that will make it available in y.tab.h, so I'm pretty sure flex will still treat yylval as an int. In any event, you cannot use %d and %f with yylval and expect to get meaningful output; I continue to suggest that you compile with adequate warning levels, in which case you will probably see what's going on. –  rici Dec 10 '12 at 14:46
Thank you. I now understand the problem. Adding -Wall to the make file gives me an warning. –  Arani Dec 10 '12 at 15:10
add comment

Your Answer


