Re: How do I match empty lines with Flex?

Kaz Kylheku <847-115-0292@kylheku.com>
Fri, 4 Oct 2019 16:55:25 +0000 (UTC)

          From comp.compilers

Related articles
How do I match empty lines with Flex? johann@myrkraverk.invalid (Johann 'Myrkraverk' Oskarsson) (2019-10-04)
Re: How do I match empty lines with Flex? 847-115-0292@kylheku.com (Kaz Kylheku) (2019-10-04)
Re: How do I match empty lines with Flex? johann@myrkraverk.invalid (Johann 'Myrkraverk' Oskarsson) (2019-10-05)
| List of all articles for this month |
From: Kaz Kylheku <847-115-0292@kylheku.com>
Newsgroups: comp.compilers
Date: Fri, 4 Oct 2019 16:55:25 +0000 (UTC)
Organization: Aioe.org NNTP Server
References: 19-10-003
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="42084"; mail-complaints-to="abuse@iecc.com"
Keywords: lex, comment
Posted-Date: 04 Oct 2019 19:39:04 EDT

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]


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.