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

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

Post by Daniel » Sun, 04 Feb 1990 21:39:22

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

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.

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.



1. Search run-time profile for unix(HP_UX, ru64, SolarisĀ°

I'm looking for a C++ run-time profiler for Unix (HP_UX/Tru64/Solaris
5.8) and Linux more convenient than gprof which could if possible print
the results
average time spent by function calls .... under graphic schema if
possible not mandatory.
. Have you some URL pointer ?

Thanks for your help.

Fran?ois REYGAGNE. Software Architect.
opt[e]way S.A., 2881 route des Crtes, BP308
06906 Sophia Antipolis Cedex, FRANCE
tl: +33 (0)4 92 95 27 01    http://www.opteway.com

Fran?ois REYGAGNE. Software Architect.
opt[e]way S.A., 2881 route des Crtes, BP308
06906 Sophia Antipolis Cedex, FRANCE
tl: +33 (0)4 92 95 27 01    http://www.opteway.com

2. Adaptec AIC6360 Boot Disk Needed

3. SQL Lex(1) and Yacc(1) specification

4. cgi-bin input

5. lex/yacc specification of Occam-2 wanted

6. stop calculating with button

7. Lex specification

8. Netscape Newgroups

9. LEX Specification using a variable

10. Lex String Specification

11. Compile-time versus run-time

12. In wich order does the run time linker search the lib*so ?

13. Need help with 'time' C Run-Time Library functions under HP-UX