lex question: how to set search specification at run-time

Daniel Wu dwu at castor.usc.edu
Sat Feb 3 23:39:22 AEST 1990


I'm having trouble getting lex to perform the tasks that I has in mind.

   1. I want lex to search for a user-specified token

I would like to write a lex program that scans through an input file and
searches for a token.  Here's the rub though: I'd like the user to set 
this token at run time by putting it into an environment variable.

	setenv LEXTOKEN "howdy"

Then somewhere in my lex program, I would like lex to read the environment
variable and search for the string (which may contain regular expressions).

	user_token = getenv("LEXTOEKN");

and in the lex file

	%%
	{user_token}  	{perform some action}

I.e., I would like lex to operate as though I had defined a macro named
user_token in the declarations section, and then act on it.  Effectively,
something like

	user_token "howdy"
	%%
	{user_token}	{perform some action}

Is this possible under lex?  Or is there some alternative way of setting this
up in lex?  The basic idea is that I want to let users define their own search
rule tokens at run-time.


   2. Assuming I'm able to overcome the hurdle above, I would like to get lex 
      to rescan the input file with a different set of lex specifications.

Here's what I mean:  Suppose I want lex to delete everything between 2 markers
in an input file, and then re-insert some new information in between those
2 markers.

I was thinking of doing it as follows.  For the first scan, I'd have something 
like this:

	BEGINMARKER	"howdy"
	ENDMARKER	"doody"
	%start InBetweenMarkers
	%%
	
	{BEGINMARKER}		{BEGIN InBetweenMarkers}
	{ENDMARKER}		{BEGIN 0} 	/* reset back to normal */

	<InBetweenMarkers>.|\n	;		/* do nothing; don't copy it */
	.|\n			{ECHO}

That should strip out all the lines I have between these 2 tokens. I.e.

	howdy			becomes		howdy
	How are you today			doody
	my friend 
	doody

Now after some processing, I want to call call yylex() again, but his time 
insert some new information back in between 'howdy' and 'doody'.

	BEGINMARKER	"howdy"
	ENDMARKER	"doody"
	%%
	
	{BEGINMARKER}		{ ECHO; printf( new stuff ); }
	.|\n			ECHO;

So I can now have

	howdy		changed to		howdy
	doody					This just in...SysV R4
						released into public domain.
						News at 11:00.
						doody

How do I do this all in 1 program?  When I lex a '.l' file, I get yy.lex.c
which I then compile and link with main.c to get one program.  In what I'm
proposing above, I would need 2 lex files: 1 to delete, the other to insert.
However I want just 1 main program which calls yylex() the first time to do 
the deletion, then processes new information, and then calls yylex() again 
but this time do the insertion.

This seems like it should be do-able under lex.  Can anybody tell me how?


Putting questions 1) and 2) together, I think you can figure out what I'm
after.  I want the user to set 2 shell variables which then act as the
markers for my deletion and insertion.  I.e.  the environment variables

	setenv LEX_BEGN_TOKEN "howdy"
	setenv LEX_END_TOKEN  "doody"

are read in at run-time by the program, so that I have 2 macros

	BEGINMARKER	defined by 	LEX_BEGN_TOKEN	(="how")
	ENDMARKER	defined by 	LEX_END_TOKEN	(="dood)"

The program will use those 2 markers as its rules to perform the deletion 
and insertion.

Is this possible, and am I approaching this correctly?


Thanks for amny help you can provide.

Daniel
dwu at castor.usc.edu



More information about the Comp.unix.questions mailing list