help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Different behaviour with string and number tokens ?


From: Hans Aberg
Subject: Re: Different behaviour with string and number tokens ?
Date: Tue, 18 Apr 2006 09:41:22 +0200

[Please do not forget cc'ing Help-Bison in replies.]

This is one of the most FAQ here: you do not copy the string, so you just pass on a pointer to the buffer in the Flex generated lexer, which of course changes in subsequent calls to the lexer.


On 18 Apr 2006, at 09:34, Arno Wilhelm wrote:

Hello,

just have started to write my first parser with flex and bison
and have run in a problem I cannot understand or solve myself.
So I would be really glad if somebody on this list would help me a bit
since I think the problem is caused by my limited understanding of the internals of bison.

The problem is that whenever I pass a number token from flex to bison in order to process it the values of the variables $1 and $3 are different from the ones passed by a similar string token.

For example:

# The number token is: "3 == 3"
--------------------------------

The rule in flex is:

NUMBER        -?(([0-9]+)|([0-9]*\.[0-9]+))
EQUAL         ==

;

{NUMBER} { yylval.num = atof(yytext); return NUMBER; }
{EQUAL}         { return EQUAL; }

In bison:

num_expr: NUMBER EQUAL NUMBER { printf("EQUAL: 1: %f | 3: %f\n", $1, $3); $$ = $1 == $3; }


# The string token is: "aa == bb"
----------------------------------

The rule in flex is:

WORD          ([a-zA-Z]+)([0-9]*)|([0-9]+)([a-zA-Z]+)
EQUAL         ==

;

{EQUAL}         { return EQUAL; }
{WORD}         { yylval.str = yytext; return WORD; }


In bison:

str_expr: WORD EQUAL WORD { printf( "EQUAL: 1: \"%s\" | 3: \"%s \"\n", $1,$3 ); sprintf( $$, "%i", strcmp( $1, $3 ) ); }




Whenever I pass a number the variable $1 is filled with the first NUMBER (in this case 3) and the variable $3 is filled with the second NUMBER (also 3) in bison as I expected But when I pass the string token "aa == bb" the variable $1 is filled with the *whole token* itself (in this case "aa == bb") and not with the first WORD which would be "aa". The variable $3 is filled with the second WORD (which is "bb") as expected.
Has anybody got a clue what happens here ?


Thanks in advance,

Arno




P.S.: I use bison 1.875c and flex 2.5.4 on Fedora Core release 3


_______________________________________________
address@hidden http://lists.gnu.org/mailman/listinfo/help-bison





reply via email to

[Prev in Thread] Current Thread [Next in Thread]