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


Groups > comp.compilers > #2374

Re: How do I match empty lines with Flex?

From Kaz Kylheku <847-115-0292@kylheku.com>
Newsgroups comp.compilers
Subject Re: How do I match empty lines with Flex?
Date 2019-10-04 16:55 +0000
Organization Aioe.org NNTP Server
Message-ID <19-10-004@comp.compilers> (permalink)
References <19-10-003@comp.compilers>

Show all headers | View raw


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]

Back to comp.compilers | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

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

csiph-web