help-bison
[Top][All Lists]
Advanced

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

Re: Parsing input from a string...


From: Dave Trombley
Subject: Re: Parsing input from a string...
Date: Mon, 29 Jan 2001 00:39:40 -0500
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.2) Gecko/20010628

Dave Trombley wrote:

I'm trying to do some stuff with bison and flex for the first time, and can't seem to figure this out: What I'd like to do is to parse input from a char*, rather than a FILE*. Of course, I could write my string to a temporary file, but this is very poor for obvious reasons. Is there any way at all for me to accomplish this without hand editing the yylex() function? (I'll need to also not use global variables, as I have a multithreaded program, and many threads will be parsing potentially many different grammars.)


I've actually made some progress with this, but I've run into a problem I don't know how to work around; however it's a bit more concrete than what I posted above!

  I'm using

#define YYPARSE_PARAM parm
#define YYLEX_PARAM parm

to create those parm variables, which I intend to be of a type that looks something like this:

typedef struct {
   char *target;
   int pos;
   int length;
   } parsebuf;

The idea was to pass a structure of that defined type to yyparse(), and in term yyparse() would pass it on to yylex(). I had hoped that this would suffice it for use in my YY_INPUT macro, which looks like this:

#define YY_INPUT(buf,res,msize)                 \
   {                             \
     parsebuf *pb = (parsebuf*)parm;            \
     int chars = pb->len - pb->pos;            \
     if (msize < chars) chars = msize;            \
     memcpy(buf,&pb->buf[pb->pos],chars);            \
     pb->pos += chars;                    \
     if (pb->pos >= pb->length) res = YY_NULL;        \
     res = chars;                        \
   }

   It seemed I was all set!  But alas:

mungbean:~/schema$ make
gcc -g  -c lex.yy.c schema.tab.c
lex.yy.c: In function `yy_get_next_buffer':
lex.yy.c:929: `parm' undeclared (first use in this function)
lex.yy.c:929: (Each undeclared identifier is reported only once
lex.yy.c:929: for each function it appears in.)
lex.yy.c:929: parse error before `('
lex.yy.c:929: structure has no member named `length'
make: *** [yyparse.a] Error 1

YY_INPUT is called from yy_get_next_buffer(), and there doesn't seem to be a way to tell yylex() to pass on parm to it. So, my questions:

1) Is there any cleaner way to make the YYLEX_PARAM variable available for use in YY_INPUT than running a script on lex.yy.c to make yylex() pass the parameter onto yy_get_next_buffer()? 2) Is it inadvisable to want to do this for some reason? (say, supposing there is an easier way to do this sort of thing then I'm attempting...)

   -dj trombley
     <address@hidden>




reply via email to

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