lex/yacc input redirect

I’d like to use lex and yacc to build a command line
interface to my qnx app, but instead of reading from a file
or stdin, I’d like to invoke the lexical machinery on a
character buffer. Sort of like this, but not really:

char buf[BUFSIZ];
while (1) {
strcpy(buf, “the line I want to parse”);
yyparse();
}

I tried adding a routine called input(), which fetches the
next character out of my buffer, but that conflicted with
the input() already define in the skeleton.

Can anyone sketch the way to make yyparse (and yylex) act on
my character array?

Jeffrey W Percival, Senior Scientist and Associate Director
Space Astronomy Laboratory, University of Wisconsin - Madison
1150 University Ave, Madison, WI 53706 USA
608-262-8686 (fax 608-263-0361) jwp@sal.wisc.edu http://www.sal.wisc.edu/~jwp

define the macro YY_INPUT(buf,result,max_size)

eg

%{
#define YY_INPUT(buf,result,max_size)
{
if ( global_input_buf[0] == EOF )
result = YY_NULL;
result = min( global_input_chars, max_size );
strncpy( buf, global_input_buf, result );
}
%}

Jeffrey W Percival <jwp@sal.wisc.edu> wrote:

I’d like to use lex and yacc to build a command line
interface to my qnx app, but instead of reading from a file
or stdin, I’d like to invoke the lexical machinery on a
character buffer. Sort of like this, but not really:

char buf[BUFSIZ];
while (1) {
strcpy(buf, “the line I want to parse”);
yyparse();
}

I tried adding a routine called input(), which fetches the
next character out of my buffer, but that conflicted with
the input() already define in the skeleton.

Can anyone sketch the way to make yyparse (and yylex) act on
my character array?

Jeffrey W Percival, Senior Scientist and Associate Director
Space Astronomy Laboratory, University of Wisconsin - Madison
1150 University Ave, Madison, WI 53706 USA
608-262-8686 (fax 608-263-0361) > jwp@sal.wisc.edu > > http://www.sal.wisc.edu/~jwp


cburgess@qnx.com

very useful; you should add this to the knowledge base…

cburgess@qnx.com wrote:

define the macro YY_INPUT(buf,result,max_size)

eg

%{
#define YY_INPUT(buf,result,max_size)
{
if ( global_input_buf[0] == EOF )
result = YY_NULL;
result = min( global_input_chars, max_size );
strncpy( buf, global_input_buf, result );
}
%}

Jeffrey W Percival <> jwp@sal.wisc.edu> > wrote:
I’d like to use lex and yacc to build a command line
interface to my qnx app, but instead of reading from a file
or stdin, I’d like to invoke the lexical machinery on a
character buffer. Sort of like this, but not really:

char buf[BUFSIZ];
while (1) {
strcpy(buf, “the line I want to parse”);
yyparse();
}

I tried adding a routine called input(), which fetches the
next character out of my buffer, but that conflicted with
the input() already define in the skeleton.

Can anyone sketch the way to make yyparse (and yylex) act on
my character array?

Jeffrey W Percival, Senior Scientist and Associate Director
Space Astronomy Laboratory, University of Wisconsin - Madison
1150 University Ave, Madison, WI 53706 USA
608-262-8686 (fax 608-263-0361) > jwp@sal.wisc.edu > > http://www.sal.wisc.edu/~jwp


cburgess@qnx.com

There are several flavors of lex, each requiring slightly different means to
redirect input. I suggest the book “Lex and Yacc” by John R. Levine, et al.
(O’Reilly & Associates, 1991 ISBN 1-56592-000-7.

In article <3A26E56F.E7E23DAA@sal.wisc.edu>, jwp@sal.wisc.edu says…

I’d like to use lex and yacc to build a command line
interface to my qnx app, but instead of reading from a file
or stdin, I’d like to invoke the lexical machinery on a
character buffer. Sort of like this, but not really:

char buf[BUFSIZ];
while (1) {
strcpy(buf, “the line I want to parse”);
yyparse();
}

I tried adding a routine called input(), which fetches the
next character out of my buffer, but that conflicted with
the input() already define in the skeleton.

Can anyone sketch the way to make yyparse (and yylex) act on
my character array?

Jeffrey W Percival, Senior Scientist and Associate Director
Space Astronomy Laboratory, University of Wisconsin - Madison
1150 University Ave, Madison, WI 53706 USA
608-262-8686 (fax 608-263-0361) > jwp@sal.wisc.edu > > http://www.sal.wisc.edu/~jwp