[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Parsing a language with optional spaces
From: |
Akim Demaille |
Subject: |
Re: Parsing a language with optional spaces |
Date: |
Mon, 6 Jul 2020 21:23:23 +0200 |
Hi Maury,
> Le 6 juil. 2020 à 18:25, Maury Markowitz <maury.markowitz@gmail.com> 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
>> <schoenebeck@crudebyte.com> 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
$
- Re: Which lexer do people use?, (continued)
- Re: Which lexer do people use?, Akim Demaille, 2020/07/04
- Re: Which lexer do people use?, Christian Schoenebeck, 2020/07/04
- Re: Which lexer do people use?, Akim Demaille, 2020/07/06
- Re: Which lexer do people use?, Maury Markowitz, 2020/07/06
- Re: Which lexer do people use?, Christian Schoenebeck, 2020/07/06
- Parsing a language with optional spaces, Maury Markowitz, 2020/07/06
- Re: Parsing a language with optional spaces, Christian Schoenebeck, 2020/07/06
- Re: Parsing a language with optional spaces, Maury Markowitz, 2020/07/06
- Re: Parsing a language with optional spaces, Christian Schoenebeck, 2020/07/06
- Re: Parsing a language with optional spaces, Akim Demaille, 2020/07/08
- Re: Parsing a language with optional spaces,
Akim Demaille <=
- Re: Parsing a language with optional spaces, Maury Markowitz, 2020/07/06
- Re: Parsing a language with optional spaces, Akim Demaille, 2020/07/06
- Re: Parsing a language with optional spaces, John P. Hartmann, 2020/07/07
- Re: Parsing a language with optional spaces, uxio prego, 2020/07/07
- Re: Parsing a language with optional spaces, Akim Demaille, 2020/07/08
- Re: Parsing a language with optional spaces, Christian Schoenebeck, 2020/07/08
- Re: Parsing a language with optional spaces, Akim Demaille, 2020/07/08
- Re: Parsing a language with optional spaces, Christian Schoenebeck, 2020/07/08
- Re: Parsing a language with optional spaces, Daniele Nicolodi, 2020/07/08
- Re: Parsing a language with optional spaces, Akim Demaille, 2020/07/09