Solved

lex: how to restart a lexer with string input

Posted on 1999-01-20
9
365 Views
Last Modified: 2006-11-17
(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.
0
Comment
Question by:greenhaze
9 Comments
 
LVL 1

Author Comment

by:greenhaze
ID: 1258232
Edited text of question
0
 
LVL 1

Author Comment

by:greenhaze
ID: 1258233
Edited text of question
0
 
LVL 5

Expert Comment

by:thoellri
ID: 1258234
Try adding the following function to your source:

int yywrap() {
  return 0;
}

Hope this helps
  Tobias
0
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
LVL 1

Author Comment

by:greenhaze
ID: 1258235
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!
0
 
LVL 1

Author Comment

by:greenhaze
ID: 1258236
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!
0
 
LVL 5

Expert Comment

by:thoellri
ID: 1258237
I'm sorry that it didn't fix your problem - I'm at a loss here :-(
  Tobias

0
 
LVL 3

Expert Comment

by:_Scotch_
ID: 1258238
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
0
 

Accepted Solution

by:
login0000 earned 200 total points
ID: 1258239
to force lex to go back to the initial state after each call ,you can use yyterminate.
you can add for example this whitch work eaven if lex work on a string an not a file.

<<EOF>>                        {
                              //      printf("end du fichier");
                                    BEGIN(0);
                                    yyterminate();
                        
                              }
just try it. good luck
0
 
LVL 1

Author Comment

by:greenhaze
ID: 1258240
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!


0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question