help-bison
[Top][All Lists]
Advanced

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

Re: yy_scan_string


From: Philip Herron
Subject: Re: yy_scan_string
Date: Wed, 14 Oct 2009 12:34:00 +0100
User-agent: Thunderbird 2.0.0.23 (X11/20090817)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

never2di wrote:
> I'm a newbie to Bison + Flex. and I'm having trouble in getting
> yy_scan_string to work. I have tried the following
>
> YY_BUFFER_STATE my_string_buffer = yy_scan_string(my_string);
> yyparse(); yy_delete_buffer(my_string_buffer );
>
> the parser errors out with a syntax error at the first token. I
> have verified that the grammar and the content of 'my_string' works
> using yyrestart(yyin) and also with yy_create_buffer() through a
> file.
>
> Is there something special state that I have to set flex in before
> I call yyparse()? if not what is that I'm doing wrong and what
> needs to be done?
>
> bison (GNU Bison) 2.4
>
> Thanks :handshake:
>
>
>

Hey

You might find this useful i do this in my lexer:

int parse( FILE* fin )
{
  int err= 0;
  if( fin == stdin )
    {
#ifdef HAVE_LIBREADLINE
      char *line, *prompt= ">>> ";
#else
      char line[256];
#endif
      YY_BUFFER_STATE bp;

      while( 1 )
    {
#ifdef HAVE_LIBREADLINE
      line= readline(prompt);
#else
      fgets(line, sizeof(line), stdin);
#endif
      if( line == 0 )
        break;
      else if( strlen(line) > 0)
        {
#ifdef HAVE_READLINE_HISTORY
          add_history(line);
#endif
          bp= yy_scan_string( line );
          yy_switch_to_buffer(bp);
          err= yyparse();
          yy_delete_buffer(bp);
        }
    }
    }
  else
    {
      yyin= fin;
      err= yyparse();
    }
  return err;
}

The bit your interested is:

bp= yy_scan_string( line );
yy_switch_to_buffer(bp);
err= yyparse();
yy_delete_buffer(bp);

You need to switch to the buffer you created from the scan string.

- --Phil
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkrVtyQACgkQAhcOgIaQQ2FudACfUfM1LKEf/RmzmterEOd41Gbz
dukAn3x9VI01C4RUc/7M3KKEIedOKahW
=GXnJ
-----END PGP SIGNATURE-----





reply via email to

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