Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.compilers > #2373 > unrolled thread

How do I match empty lines with Flex?

Started byJohann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid>
First post2019-10-04 22:01 +0800
Last post2019-10-05 22:29 +0800
Articles 3 — 2 participants

Back to article view | Back to comp.compilers


Contents

  How do I match empty lines with Flex? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2019-10-04 22:01 +0800
    Re: How do I match empty lines with Flex? Kaz Kylheku <847-115-0292@kylheku.com> - 2019-10-04 16:55 +0000
      Re: How do I match empty lines with Flex? Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2019-10-05 22:29 +0800

#2373 — How do I match empty lines with Flex?

FromJohann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid>
Date2019-10-04 22:01 +0800
SubjectHow do I match empty lines with Flex?
Message-ID<19-10-003@comp.compilers>
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]

[toc] | [next] | [standalone]


#2374

FromKaz Kylheku <847-115-0292@kylheku.com>
Date2019-10-04 16:55 +0000
Message-ID<19-10-004@comp.compilers>
In reply to#2373
On 2019-10-04, Johann 'Myrkraverk' Oskarsson <johann@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]

[toc] | [prev] | [next] | [standalone]


#2375

FromJohann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid>
Date2019-10-05 22:29 +0800
Message-ID<19-10-005@comp.compilers>
In reply to#2374
On 05/10/2019 12:55 am, Kaz Kylheku wrote:
> On 2019-10-04, Johann 'Myrkraverk' Oskarsson <johann@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]

[toc] | [prev] | [standalone]


Back to top | Article view | comp.compilers


csiph-web