0

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.

splash
  • 13,037
  • 1
  • 44
  • 67
Sam Tatasurya
  • 223
  • 2
  • 15

1 Answers1

2

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.

rici
  • 234,347
  • 28
  • 237
  • 341
  • Thanks for the explanation and the heads-up! Yeah, after I looked deeper into the generated lex.yy.c, it seems that yylineno incrementing is hard-coded to \n, so I have a feeling that it cannot be modified using some kind of parameter. – Sam Tatasurya Jun 01 '17 at 07:57
  • @sam: not only is it hard-coded to \n, the check only occurs in certain actions (which are the ones whose patterns might match a \n). So even if you changed the scan, it might miss some sequences. – rici Jun 01 '17 at 13:21