Re: Parsing a language with optional spaces

Akim Demaille <[email protected]>
Newsgroups gmane.comp.parsers.bison.general
Message-ID <[email protected]>
Hi Maury,

> Le 6 juil. 2020 à 18:25, Maury Markowitz <[email protected]> a écrit :
> 
> Moving to a new thread - I was surprised I could even post, previous efforts were bounced from the list server for no obvious reason.

That's really weird.

>> On Jul 6, 2020, at 9:04 AM, Christian Schoenebeck <[email protected]> wrote:
>> 
>> You would simply add a RegEx pattern & rule like this:
> 
> Consider two snippets in home-computer-era BASIC:
> 
> FOREX=10
> 
> and:
> 
> FOREX=10TO20
> 
> Is the first one a broken FOR statement or a perfectly valid variable assignment? (Why not both?!) MS says the former, BBC the later.
> 
> In the lex/flex model of longest-match-wins, assuming any reasonable definition for your variable pattern, both statements are variable assignments and the second fails to parse.

Let me say it again.  That's a job for Flex's / operator.

$ cat foo.l
%option noyywrap

id  [A-Z]+
sp  [ \t\n]*
num [0-9]+
%%
FOR/{sp}{id}{sp}={sp}{num}{sp}TO{sp}{num}  { printf("for: %s\n", yytext); }
{id}                    { printf("id: %s\n", yytext); }
{num}                   { printf("num: %s\n", yytext); }
{sp}                    { continue; }
=                       { printf("=: %s\n", yytext); }
%%
int main(void)
{
 while (yylex())
   continue;
}

$ flex -o foo.c foo.l && gcc-mp-9 foo.c

$ echo "FOREX=10TO20" | ./a.out
for: FOR
id: EX
=: =
num: 10
id: TO
num: 20

$ echo "FOR EX = 10 TO 20" | ./a.out
for: FOR
id: EX
=: =
num: 10
id: TO
num: 20

$ echo "FOR EX = 10" | ./a.out
id: FOR
id: EX
=: =
num: 10

$ echo "FOREX=10" | ./a.out
id: FOREX
=: =
num: 10

$
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.