http://qs321.pair.com?node_id=843877

rir has asked for the wisdom of the Perl Monks concerning the following question:

In the below, I don't see why the x matches differently from the * ?
#!/home/rir/rakudo/parrot_install/bin/perl6 use v6; grammar Calc { rule TOP { <term> <op> <term> } # rule TOP { <term>\s?<op>\s?<term> } # a fix token term { \d+(\.\d*)? } token op { '*' | 'x' } } my @t = ( [ 44, " 8.8 * 5.0 " ], [ 44, " 8.8 x 5.0 " ], [ 44, " 8.8*5.0 " ], [ 44, " 8.8x5.0 " ], # no match ); for @t -> $i { my $m = Calc.parse( $i[1]); print $m ?? " " !! "no"; say " match: $i[1]" }
Be well,
rir

Replies are listed 'Best First'.
Re: P6: parsing whitespace
by moritz (Cardinal) on Jun 09, 2010 at 18:01 UTC
    If you have whitespace in a rule, it is internally replaced by a call to the ws token.

    The default ws matches at least one space (\s+) if the left and right are word characters (ie matching \w), and matching zero or more (\s*) otherwise.

    In code:

    class Grammar { token ws { <!ww> \s* } }

    Where ww is within word, and could be defined as

    token ww { <after \w> <before \w> }

    If you want 8.8x5.0 to match, you need to override the ws rule in your grammar:

    grammar Calc { token ws { \s* } # rest of your code here without any modifications

    (Update: fixed ww rule after comment from TimToady++)

    Perl 6 - links to (nearly) everything that is Perl 6.