[net.unix-wizards] HELP needed from an experience lex-er

peters@cubsvax.UUCP (07/06/84)

'Fraid I jumped in over my head on this one... anyone with experience
(or even just plain advice!!) is welcome to reply.

I hacked up lex.yy.c by changing the definitions of "input()" and "output(c)",
as well as the function definiton of yylex(), so that yylex now has the
form yylex(string), where string gets copied into extern char *instr.
Output is placed in extern char *outstr, which is returned.  The net 
effect is that yylex() can now be called as a subroutine from another
function, and returns outstr to that function.  Fine and dandy.

This works wonderfully on the first call...  but on subsequent calls
the input string never gets parsed;  the right part
of yylook() is never entered.  Apparently, something
is done to lex.yy.c's external variables during parsing, and something
needs to be re-initialized before the second call will work properly.  If
you know what it is, please let me know!  (In the meantime, I'll be
hacking away... I never really wanted to know how lex does its thing, and
if you tell me how to solve this problem, I won't have to....)

If you've read this far, you might like to know that my input() looks
like lex's, except getc is replaced with a #defined "sgetc" ("string-
getc," dig it?), which returns the next character in instr, increments
an index to instr, and returns EOF if '\0' is encountered.  (Then on each
call, instr is re-initialized to the string passed to yylex.)

{philabs,cmcl2!rocky2}!cubsvax!peters            Dr. Peter S. Shenkin 
Dept of Biol. Sci.;  Columbia Univ.;  New York, N. Y.  10027;  212-280-5517

andrew@orca.UUCP (Andrew Klossner) (07/09/84)

[]

	"I hacked up lex.yy.c by changing the definitions of "input()"
	and "output(c)", as well as the function definiton of yylex(),
	so that yylex now has the form yylex(string), where string gets
	copied into extern char *instr.  Output is placed in extern
	char *outstr, which is returned.  The net effect is that
	yylex() can now be called as a subroutine from another
	function, and returns outstr to that function.  Fine and dandy.

	"This works wonderfully on the first call...  but on subsequent
	calls the input string never gets parsed;  the right part of
	yylook() is never entered.  Apparently, something is done to
	lex.yy.c's external variables during parsing, and something
	needs to be re-initialized before the second call will work
	properly.  If you know what it is, please let me know!"

You must execute the statement "NLSTATE;" upon each entry to yylex.
Put the following immediately in front of your first pattern, after the
first "%%":

%{
	NLSTATE;
%}

  -- Andrew Klossner   (decvax!tektronix!orca!andrew)      [UUCP]
                       (orca!andrew.tektronix@rand-relay)  [ARPA]