0

I have a file that generates cc code using flex. When I use the version 2.5.4a-10 the codes works as expected.

If I use bit more recent version 2.5.37 or even newer like 2.6 the generated code seems not to allocate anything. It uses some pointers defined with nullptr and crashes.

I think the syntax has changed in between these versions. I find it also strange that Debian/Ubuntu have a package called flex-old saying:

flex is a tool for generating scanners: programs which recognize lexical patterns in text. This is the old 2.5.4a version, which is no longer being developed. You should normally choose flex, unless you have legacy lexer files that do not work with a modern flex.

This product includes software developed by the University of California, Berkeley and its contributors. The upstream source code can be found at http://flex.sourceforge.net/

(Editor's note: Flex has moved to Github but v2.5.4a is not there.)

That version seems to be a big deal for others I suspect. Getting to my question:

Is there any manual or guide of what I have to do in order to port that code to generate some c++ code that works in more recent versions of flex?

EDIT: Here is my simple example taken from something larger:

    int num_lines = 0, num_chars = 0;

%%
\n      ++num_lines; ++num_chars;
.       ++num_chars;

%%
int main()
{
    yy_init=1;

    yylex();
    printf( "# of lines = %d, # of chars = %d\n",
            num_lines, num_chars );
    return 0;
}

flex it with flex file.l and build it with gcc lex.yy.c -lfl. Now, if you used version 2.5.4 it will work. With later versions it translates and compiles just fine, but when you run the program you will get segmentation fault.

rici
  • 234,347
  • 28
  • 237
  • 341
Gerhard Stein
  • 1,543
  • 13
  • 25
  • 1
    It would be helpful if you could provide some actual details about the problems you are experiencing. Obviously, a [mcve] would be ideal. – rici Apr 12 '17 at 15:52
  • Yes, I know, let try to upload one. The example is not that small. – Gerhard Stein Apr 13 '17 at 06:05

1 Answers1

3

I found the problem myself. The variable yy_init can be explicitly set in that old version. In newer versions it is not allowed. I'm not sure if that is intended, maybe someone can explain why this behavior is observed. I find it a bit strange.

If someone has a similar problem, you might want to take a look at the yy_init variable. Other than that I had no issues.

Gerhard Stein
  • 1,543
  • 13
  • 25
  • 1
    That variable is undocumented, so changing it is a bad idea. It looks like setting it to 1 will prevent flex from initializing itself, so why would you want to do that? – interjay Apr 13 '17 at 08:00
  • Hi jay, I never want to do such things. The code I got is about 20 years old. It had that variable defined as 1. Need to ask the person who passed me that, why he decided to set it. – Gerhard Stein Apr 13 '17 at 08:17
  • Anyways, I have decided to change that to a reentrant scanner. We need that because we want to be able to scan multiple streams. I think for such situation and improved controls over the scanner state it is a much better approach. – Gerhard Stein Apr 21 '17 at 11:09