Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I forgot to mention, some of the features of Match objects need the latest Rakudo development version, the release lags behind.

Is the string eval reasonable here?

Yes, though you can easily avoid it with a hash:

my %actions = '*' => { $^a * $^b }, '/' => { $^a / $^b }, '+' => { $^a + $^b }, '-' => { $^a - $^b }; ... make %actions{$<op>.ast}.(|$/<lhs rhs>)

Again, hash slices on Match objects likely require a new version of Rakudo built from source.

Can the expression rule be handled without the separate naming of the lhs and rhs? Is there a way to access each numeric in something like: rule expression { <numeric> <op> <numeric> }

First of all you can do the renaming inline:

rule expression { <lhs=.numeric> <op> <rhs=.numeric> }

I'd prefer this solution for clarity. However you can also use <numeric> twice in the same regex, in which case $<numeric> becomes and array. Then you can access the left and right side as $<numeric>[0] and $<numeric>[1].

rule expression { <lhs> ( <op> <rhs> )+ }

The corresponding action method might look like this:

method numeric($/) { make +$/ } # ^ convert to a number # propagate the number as the AST: method lhs($/) { make $<numeric>.ast } method rhs($/) { make $<numeric>.ast } method expression($/) { my $value = $<lhs>.ast; # iterate over all matches of the first (...) group for $0.list -> $m { $value = %actions{$m<op>.ast}.($value, $m<rhs>.ast); } make $value; }

I hope this helps.


Here's a complete, working example that also passes the action down as an AST:

use v6; grammar Calc { token TOP { <expression> } rule expression { <lhs=.numeric> ( <op> <rhs=.numeric> )* } token numeric { \d+[\.\d*]? } token op { '-' | '+' | '*' | '/' | 'x' } } my %actions = '*' => { $^a * $^b }, 'x' => { $^a * $^b }, '/' => { $^a / $^b }, '+' => { $^a + $^b }, '-' => { $^a - $^b }; class Calc::Actions { method TOP($/) { make $<expression>.ast } method expression($/) { my $value = $<lhs>.ast; for $0.list -> $m { $value = $m<op>.ast.($value, $m<rhs>.ast); } make $value; } method numeric($/) { make +$/ } method op($/) { make %actions{$/} } } my $m = Calc.parse( "8.8 x 5.0 - 2", :actions( Calc::Actions)); die "dying no match" unless $m; say "$m<expression> = $m.ast()"; # vim: ft=perl6
Perl 6 - links to (nearly) everything that is Perl 6.

In reply to Re^3: P6: Beginning grammar by moritz
in thread P6: Beginning grammar by rir

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (4)
    As of 2017-11-22 06:33 GMT
    Find Nodes?
      Voting Booth?
      In order to be able to say "I know Perl", you must have:

      Results (316 votes). Check out past polls.