Is there a way make the lex program match multiple line?

Started by Wen Yialmost 3 years ago3 messagesgeneral
Jump to latest
#1Wen Yi
896634148@qq.com

Hi community,
I am making a config program, use the lex program to analyse input.

/*
    config.l
        The lex rule file for toysql
    Wen Yi
*/
%option noyywrap
%{
#include <string.h&gt;
#include <stdlib.h&gt;&nbsp;&nbsp;&nbsp;
%}
%%
-[0-9]+.[0-9]+|[0-9]+.[0-9]+|-[0-9]+|[0-9]+ { printf("Number = %lf\n", atof(yytext)); }
[a-zA-Z_0-9]+&nbsp; { printf("Token = %s\n", yytext); }
['].+['] {
&nbsp;&nbsp; &nbsp;yytext[yyleng - 1] = '\0';
&nbsp;&nbsp; &nbsp;memmove(yytext, yytext + 1, yyleng - 1);
&nbsp;&nbsp; &nbsp;printf("String = %s\n", yytext);
}

; {}
. { printf("Anything: '%s'\n", yytext);&nbsp; }
%%
int main()
{
&nbsp;&nbsp; &nbsp;yylex();
&nbsp;&nbsp; &nbsp;return 0;
}

But when I try to run it:

[beginnerc@bogon config]$ ./a.out
'This is a single line string'
String = This is a single line string

'This is a multiple line string
Anything: '''
Token = This
Anything: ' '
Token = is
Anything: ' '
Token = a
Anything: ' '
Token = multiple
Anything: ' '
Token = line
Anything: ' '
Token = string

Can someone give me some advice to make the ['].+['] match multiple string?
Thanks in advance!

Yours,
Wen Yi

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Wen Yi (#1)
Re: Is there a way make the lex program match multiple line?

"=?ISO-8859-1?B?V2VuIFlp?=" <896634148@qq.com> writes:

Can someone give me some advice to make the ['].+['] match multiple string?

You should check the flex manual, but it's likely that "." doesn't
match newline. Another problem with this pattern is that "."
*does* match "'", so it's ambiguous what will happen with quotes.
You probably need something closer to [']([^']|\n)[']

regards, tom lane

#3Wen Yi
896634148@qq.com
In reply to: Tom Lane (#2)
Re: Is there a way make the lex program match multiple line?

I rewrite, and I think the [']([^']|\n)['] should be [']([^']|\n)+[']
Thanks very much!

Yours,
Wen Yi.

&nbsp;

------------------&nbsp;Original&nbsp;------------------
From: "Tom Lane" <tgl@sss.pgh.pa.us&gt;;
Date:&nbsp;Sun, Jun 18, 2023 09:50 AM
To:&nbsp;"Wen Yi"<896634148@qq.com&gt;;
Cc:&nbsp;"pgsql-general"<pgsql-general@lists.postgresql.org&gt;;
Subject:&nbsp;Re: Is there a way make the lex program match multiple line?

"=?ISO-8859-1?B?V2VuIFlp?=" <896634148@qq.com&gt; writes:
&gt; Can someone give me some advice to make the ['].+['] match multiple string?

You should check the flex manual, but it's likely that "." doesn't
match newline.&nbsp; Another problem with this pattern is that "."
*does* match "'", so it's ambiguous what will happen with quotes.
You probably need something closer to [']([^']|\n)[']

regards, tom lane