Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

P6: parsing whitespace

by rir (Vicar)
on Jun 09, 2010 at 17:25 UTC ( #843877=perlquestion: print w/ replies, xml ) Need Help??
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

Comment on P6: parsing whitespace
Download Code
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://843877]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (13)
As of 2014-08-27 19:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (250 votes), past polls