help-bison
[Top][All Lists]
Advanced

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

Re: Some problems in Bison


From: Akim Demaille
Subject: Re: Some problems in Bison
Date: Mon, 31 Mar 2003 11:47:16 +0200
User-agent: Gnus/5.090016 (Oort Gnus v0.16) Emacs/21.2 (gnu/linux)

| Hy,
| I am trying to make flex and bison communicate, and I have defined
| something like:
|  
| ...
| %union{
|   char *lex;
|   double dval;
| }
| %token <lex> TYPE ID
|  
| %%
|  
| decl : TYPE ID ; {printf("%s %s",$1,$2);}
|  
| %%
| ....
|  
| In flex I do the following:
|  
| ....
| int {yylval.lex = yytext; return TYPE;}
| [_a-zA-Z0-9][_a-zA-Z0-9]* {yylval.lex = yytext; return ID}
| ...
|  
| And when running on something like
|  
| int c;
|  
| I get the result :
|  
| int c; c;
|  
| i.e. $1 is 'int c;' and $2 is 'c;'
|  
| Can anyone tell me how to make bison get the real values (i.e. 'int' and
| 'c')?
|  
| Thakns!

Strings are Destroyed
=====================

     My parser seems to destroy old strings, or maybe it loses track of
     them.  Instead of reporting `"foo", "bar"', it reports
     `"bar", "bar"', or even `"foo\nbar", "bar"'.

   This error is probably the single most frequent "bug report" sent to
Bison lists, but is only concerned with a misunderstanding of the role
of scanner.  Consider the following Lex code:


%{
#include <stdio.h>
char *yylval = NULL;
%}
%%
.*    yylval = yytext; return 1;
\n    /* IGNORE */
%%
int
main ()
{
  /* Similar to using $1, $2 in a Bison action. */
  char *fst = (yylex (), yylval);
  char *snd = (yylex (), yylval);
  printf ("\"%s\", \"%s\"\n", fst, snd);
  return 0;
}

   If you compile and run this code, you get:

     $ flex -osplit-lines.c split-lines.l
     $ gcc  -osplit-lines   split-lines.c -ll
     $ printf 'one\ntwo\n' | ./split-lines
     "one
     two", "two"

this is because `yytext' is a buffer provided for _reading_ in the
action, but if you want to keep it, you have to duplicate it (e.g.,
using `strdup').  Note that the output may depend on how your
implementation of Lex handles `yytext'.  For instance, when given the
Lex compatibility option `-l' (which triggers the option `%array') Flex
generates a different behavior:

     $ flex -l -osplit-lines.c split-lines.l
     $ gcc     -osplit-lines   split-lines.c -ll
     $ printf 'one\ntwo\n' | ./split-lines
     "two", "two"




reply via email to

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