[Top][All Lists]

[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: Tue, 7 Jul 2020 05:35:16 +0200

> Le 6 juil. 2020 à 22:01, Maury Markowitz <> a écrit :
>> On Jul 6, 2020, at 3:23 PM, Akim Demaille <> wrote:
>> FOR/{sp}{id}{sp}={sp}{num}{sp}TO{sp}{num}  { printf("for: %s\n", yytext); }
> This is a very different style than what I have seen in the past. In the 
> past, most examples of flex tend to match against the tokens as individual 
> items and return immediately so that bison can do the grammar check and 
> generation. So in my existing (semi-working) .l, I followed this style and 
> produced:
>    FOR        { return FOR; }
>    ...
>    TO { return TO; }
> Then over in my .y I have:
>    FOR variable '=' expression TO expression
> Using your method, what would the associated bison look like? Just a single 
> item matching the entire line?

I believe you need to read again the documentation of /

    an 'r' but only if it is followed by an 's'.  The text matched by
    's' is included when determining whether this rule is the longest
    match, but is then returned to the input before the action is
    executed.  So the action only sees the text matched by 'r'.  This
    type of pattern is called "trailing context".  (There are some
    combinations of 'r/s' that flex cannot match correctly.  *Note
    Limitations::, regarding dangerous trailing context.)

and to read carefully the examples I sent.

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

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

reply via email to

[Prev in Thread] Current Thread [Next in Thread]