Search code examples
bisonflex-lexeryacclex

Flex / Bison: Configure yylineno to recognize '\r' as newline


Is there a way to configure/modify yylineno to recognize a single \r (carriage return) character as newline?

Ultimately, I'd like to use yylineno for 3 different types of file:

  • \n: Unix-like
  • \r\n: Windows-based (will be recognized as a single newline, not two, i.e. \r + \n)
  • \r: e.g. classic Mac OS

I know that I'm able to define my own variable to keep track of the line number in Flex and increment it everytime one of the newline characters (\r|\n|\r\n) is found, but I'd prefer to use the built-in Flex feature if it's available.

Thank you.


Solution

  • The bad news is that flex does not have that feature. Line counting is not configurable at all, and it depends on static analysis of the patterns to see which ones might match a newline. That wouldn't be possible if a newline sequence were more than one byte long, because the newline sequence could be split over two separate tokens. (Make sure to take that fact into account if you are implementing your own line counter. The simplest solution would be to ensure that it is not possible for a token to end after the \r in a \r\n sequence.)

    The good news is that \r-terminated files are pretty rare these days. So the cost of not handling them might not be huge.