Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I match empty lines with Flex?

22 views
Skip to first unread message

Johann 'Myrkraverk' Oskarsson

unread,
Oct 4, 2019, 11:29:52 AM10/4/19
to
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?

Is there a reason .* doesn't match zero characters in this case?

%option noyywrap noinput nounput

%{
#include <stdio.h>

char *yylval;

%}


%%

..* { yylval = yytext; return 1; }
\n { yylineno += 1; }
<<EOF>> { return 0; }

%%

int main( int argc, char *argv[] )
{
if ( argc > 1 ) {
yyin = fopen( argv[ 1 ], "r" );

while ( yylex() ) {
printf( "%d: %s\n", yylineno, yylval );

}
}

return 0;
}

--
Johann | email: invalid -> com | www.myrkraverk.com/blog/
I'm not from the Internet, I just work there. | twitter: @myrkraverk
[Flex never matches an empty string.
I expect a pattern like ^\n would do what you want. -John]

Kaz Kylheku

unread,
Oct 4, 2019, 7:39:06 PM10/4/19
to
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]

Johann 'Myrkraverk' Oskarsson

unread,
Oct 5, 2019, 10:49:03 AM10/5/19
to
On 05/10/2019 12:55 am, Kaz Kylheku wrote:
> 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; }

Thank you, that indeed does the trick. With %option yylineno the count
is off by one; the first line is numbered 2 for some reason. With my
own count, it's correct.

--
Johann | email: invalid -> com | www.myrkraverk.com/blog/
I'm not from the Internet, I just work there. | twitter: @myrkraverk
[Flex starts yylineno at 1, so if you increment it in the pattern
that matches .+\n you'll see the first line as line 2. -John]
0 new messages