On 2019-10-04, Johann 'Myrkraverk' Oskarsson <joh...@myrkraverk.invalid> wrote:
> Dear comp.compilers,
>
> The following program matches lines in a text file, but ignores empty
> lines. Is there any way I can alter it so it returns something on empty
> lines?
How about:
.+\n { num_lines++; return NONEMPTY_LINE; }
\n { num_lines++; return EMPTY_LINE; }
.+ { num_lines++; return MISSING_LAST_NEWLINE; }
> ..* { yylval = yytext; return 1; }
> \n { yylineno += 1; }
I'd recommend not to rely on yylineno being defined, and certainly
don't increment it yourself. It's not described by POSIX. I think some
implementations of Lex have it.
GNU Flex will generate this varaible and update its value if you use
%option yylineno, or --yylineno on the command line.
If you do your own line counting, invent some variable that doesn't
intrude into the reserved yacc yy* namespace.
><<EOF>> { return 0; }
<<EOF>> might be a GNU Flex extension; if you rely on that, you might
as well use %option yylineno.
[Either that or something with start states. -John]