help-bison
[Top][All Lists]
Advanced

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

Re: $2 not a string


From: Luca
Subject: Re: $2 not a string
Date: Sun, 07 Dec 2008 14:08:00 +0100
User-agent: Thunderbird 2.0.0.18 (Windows/20081105)

Jim Michaels ha scritto:
in the .y file,
                                    |
ZERO T_SECTION { SyntaxNode t; t.ystype=0; t.enum_ystype=STRVAL; t.datum.strval=_strdup($2); deque_put_back(t); }
                                  |


                        | T__CELTYPE code6
                         { SyntaxNode t; t.ystype=9; t.enum_ystype=STRVAL; 
t.datum.strval=_strdup($1); deque_put_back(&t); }

                        | T__CELWEIGHT code370
                         { SyntaxNode t; t.ystype=9; t.enum_ystype=STRVAL; 
t.datum.strval=_strdup($1); deque_put_back(&t); }

                        | T__CEPSNID code390
                         { SyntaxNode t; t.ystype=9; t.enum_ystype=STRVAL; 
t.datum.strval=_strdup($1); deque_put_back(&t); }

I have tried this with yylval and with $2.  I get the same results.  I am using 
bison 2.1 (sorry, it's the latest version available for my platform).
If you're are running under windows, you can download 2.3.1 from cygwin setup (otherwise you can compile 2.4 also using cygwin). Anyway I think the problem doesn't concern the version of bison.
apparently what gets put in as a datum is a null the first time (here).
the rest of the times I use $2 I get an empty string or a crash when I try to 
print the string.  it works until the 10th item.

what I want is the string value from flex.
In your lex file, when your lexer matches a string, you can set a semantic value using "yylval" (you should include "y.tab.h").
For example, using integer:
[0-9]+ {yylval.iValue = atoi(yytext); return INTEGER_VALUE; }


defining in bison:
%union { int iValue;
   char *sValue;
   };

%token <iValue> INTEGER_VALUE
%token <sValue> STRING_VALUE


Of course you can do the same for the strings, but remember to copy the string (using strdup) otherwise the parser can destroy them (see "Strings are Destroyed", chapter 11.3 in bison 2.1 manual, page 112) because the semantic value (yylval.sValue) is assigned to yytext but the lexer can modify this pointer many times before the rule is reduced.
What I mean, the lexer should allocate the string and not the parser:

/*String match*/ {yylval.sValue=strdup(yytext); return STRING_VALUE;}


help

Jim Michaels
address@hidden
http://JesusnJim.com


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






reply via email to

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