Path: csiph.com!3.us.feeder.erje.net!feeder.erje.net!news.snarked.org!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: Kaz Kylheku <847-115-0292@kylheku.com> Newsgroups: comp.compilers Subject: Re: How do I match empty lines with Flex? Date: Fri, 4 Oct 2019 16:55:25 +0000 (UTC) Organization: Aioe.org NNTP Server Lines: 31 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <19-10-004@comp.compilers> References: <19-10-003@comp.compilers> 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 X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:2374 On 2019-10-04, Johann 'Myrkraverk' Oskarsson 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. ><> { return 0; } <> 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]