develooper Front page | perl.perl6.users | Postings from March 2021

"rule" declarator: Different results for 'unadorned' match vsunnamed/named captures? (in re Grammars...).

Thread Next
From:
William Michels via perl6-users
Date:
March 11, 2021 20:53
Subject:
"rule" declarator: Different results for 'unadorned' match vsunnamed/named captures? (in re Grammars...).
Message ID:
CAA99HCwVXdiscibMZnx5r6Zb6ewvr6x20rw=cF1646TGyKSn=A@mail.gmail.com
Hello,

I've been chatting with raiph on SO regarding Grammar "tokens" vs
"rules". The OP is here https://stackoverflow.com/q/62051742 and our
discussion is here https://stackoverflow.com/a/62053666 .

I think there's something going on with the examples below, as I'm
seeing different results when comparing a basic "rule" match vs either
unnamed or named "rule" captures. The focus of the tests below is to
look at changes with/without whitespace, and comparing "tokens" vs
"rules".

TL;DR version:

> say $/ if 'ab'  ~~ rule  {.? .?}
「a」
> say $/ if 'ab'  ~~ rule  {(.?) (.?)}
「b」
 0 => 「b」
 1 => 「」
> say $/ if 'ab'  ~~ rule  {$<first>=.? $<second>=.?}
「b」
 first => 「b」
 second => 「」

So for the first example (an unadorned "rule" match), Raku returns 「a」.
But for the second and third examples ("rule" captures), Raku returns 「b」.
(Also confirmed the three lines above with Raku one-liners).

Full list of examples:

Last login: Thu Mar 11 11:50:11 on ttys042
user@mbook:~$ raku
Welcome to 𝐑𝐚𝐤𝐮𝐝𝐨™ v2020.10.
Implementing the 𝐑𝐚𝐤𝐮™ programming language v6.d.
Built on MoarVM version 2020.10.

To exit type 'exit' or '^D'
> say $/ if 'a'  ~~ token  {.?.?}
「a」
> say $/ if 'ab'  ~~ token  {.?.?}
「ab」
> say $/ if 'ab'  ~~ token  {.? .?}
「ab」
> say $/ if 'a b'  ~~ token  {.? .?}
「a 」
> say $/ if 'a'  ~~ rule  {.?.?}
「a」
> say $/ if 'ab'  ~~ rule  {.?.?}
「ab」
> say $/ if 'ab'  ~~ rule  {.? .?}
「a」
> say $/ if 'a b'  ~~ rule  {.? .?}
「a b」
> ####
Nil
> say $/ if 'a'  ~~ token  {(.?)(.?)}
「a」
 0 => 「a」
 1 => 「」
> say $/ if 'ab'  ~~ token  {(.?)(.?)}
「ab」
 0 => 「a」
 1 => 「b」
> say $/ if 'ab'  ~~ token  {(.?) (.?)}
「ab」
 0 => 「a」
 1 => 「b」
> say $/ if 'a b'  ~~ token  {(.?) (.?)}
「a 」
 0 => 「a」
 1 => 「 」
> say $/ if 'a'  ~~ rule  {(.?)(.?)}
「a」
 0 => 「a」
 1 => 「」
> say $/ if 'ab'  ~~ rule  {(.?)(.?)}
「ab」
 0 => 「a」
 1 => 「b」
> say $/ if 'ab'  ~~ rule  {(.?) (.?)}
「b」
 0 => 「b」
 1 => 「」
> say $/ if 'a b'  ~~ rule  {(.?) (.?)}
「a b」
 0 => 「a」
 1 => 「b」
> ####
Nil
> say $/ if 'a'  ~~ token  {$<first>=.?$<second>=.?}
「a」
 first => 「a」
 second => 「」
> say $/ if 'ab'  ~~ token  {$<first>=.?$<second>=.?}
「ab」
 first => 「a」
 second => 「b」
> say $/ if 'ab'  ~~ token  {$<first>=.? $<second>=.?}
「ab」
 first => 「a」
 second => 「b」
> say $/ if 'a b'  ~~ token  {$<first>=.? $<second>=.?}
「a 」
 first => 「a」
 second => 「 」
> say $/ if 'a'  ~~ rule  {$<first>=.?$<second>=.?}
「a」
 first => 「a」
 second => 「」
> say $/ if 'ab'  ~~ rule  {$<first>=.?$<second>=.?}
「ab」
 first => 「a」
 second => 「b」
> say $/ if 'ab'  ~~ rule  {$<first>=.? $<second>=.?}
「b」
 first => 「b」
 second => 「」
> say $/ if 'a b'  ~~ rule  {$<first>=.? $<second>=.?}
「a b」
 first => 「a」
 second => 「b」
> ####
Nil

Any advice appreciated, Thx, Bill.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About