Link to home
Start Free TrialLog in
Avatar of greenhaze
greenhaze

asked on

lex: how to restart a lexer with string input

(note: I'm using AT&T lex)
I'm writing a shared library that lexs string input sent by external programs. Problem is, when the function is called consecutively by any instance of an external program, the lexer -- yylex() --  does not get reset. Thus, subsequent return values are null (the first call works great though!). Below is part of this program:


Part of the header file:
    ...
    #undef input
    #undef unput
    #define input() (*instring++)
    #define unput(c) ( *--instring = c)
    ...
and later I define the main function:

    char * lexfunction(char *yourstring)
    {
      ...
       instring=cpy_of_yourstring;
       yylex();
      ...
       return parsed_string;
    }

THEN an external script which loads in this library
can call "lexfunction(somestring)".

QUESTION: how do I reset the lexer. I know it's something with either yywrap() or yylex() but I don't know how to do it!

Any suggestions would be GREATLY appreciated.
Avatar of greenhaze
greenhaze

ASKER

Edited text of question
Edited text of question
Try adding the following function to your source:

int yywrap() {
  return 0;
}

Hope this helps
  Tobias
I had tried that. It causes the program to hang because yylex() needs a return 1 to shutdown. Modifying yywrap() seems to be best suited for applications that spin through a series of inputs with each execution.

As a shared library, it's not clear to me when execution really ends. Maybe that's the central issue here: how do I ensure a new instance of the function when new data comes in? This is very frustrating!!

Thanks for any help!
I had tried that. It causes the program to hang because yylex() needs a return 1 to shutdown. Modifying yywrap() seems to be best suited for applications that spin through a series of inputs with each execution.

As a shared library, it's not clear to me when execution really ends. Maybe that's the central issue here: how do I ensure a new instance of the function when new data comes in? This is very frustrating!!

Thanks for any help!
I'm sorry that it didn't fix your problem - I'm at a loss here :-(
  Tobias

Have you done something with YY_INPUT to lex a buffer rather
than a file ??

If so have a look at yy_create_buffer() and yy_switch_to_buffer()

-- I use flex but imagine the interface is much the same as lex
ASKER CERTIFIED SOLUTION
Avatar of login0000
login0000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is correct if using GNU flex; however, I'm using AT&T lex - its buggier cousin.

Likewise, the YY_INPUT macro is flex-specific. I had to use the #define input() statements for AT&T lex.

Alas, I got it running. Thanks for all the suggestions!