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

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

#!/home/rir/rakudo/parrot_install/bin/perl6 grammar OP { token TOP { <op>+ } token op { <cop> | <sop> | <bop> } token cop { # constrained <short> <sep> <long> <div> <char>+ } token sop { # string <short> <sep> <long> <div> } token bop { # bool <short> <sep> <long> } token long { \w+ } token short { \w } token char { # XXX Rename flag. Eliminate '_'. \w } rule sep { ':' } rule div { '/' } } my $s = 'a:aaa b:bbb/ c:ccccc/def'; my $match = OP.parse($s); die "failed" unless $match; say '$match<op>: ', $match<op>; say '$match.from: ', $match.from, ' $match.to: ', $match.to; say '$match.chars: ', $match.chars; say '$match.orig: ', $match.orig; say '$match.Str: ', $match.Str; say '$match.ast: ', $match.ast; say '$match.caps: is not found'; say '$match.pos: is not found'; my @array = $match; say '@array = $match; then @array: ['; for @array -> $i { say $i ; } say "]";
I have a match object that doesn't appear to contain what I'd like. The examples, I've found, are not working for me; I find SO5 a bit thick. Attempting this with a grammar is a learning exercise.

In the above, I'd like to grab ops, know their "type" and insert into a hash keyed on the shorts.

Is this arranged correctly to avoid a partial sop matching as a bop?

The input string will be small; should I walk the string or walk the match object? As an exercise, treating the problem like a larger language appeals?

How do I traverse $match?

$match is supposed to work just like $/, yes?

I don't care to capture seps or divs but I would like to abstract the values. Should this be done in the grammar? --by variables?

Be well,
rir -- swimming a sea of sticky voluminous syntax errors all alike

Replies are listed 'Best First'.
Re: P6: Beginning grammar
by moritz (Cardinal) on Jun 02, 2010 at 17:27 UTC
    The input string will be small; should I walk the string or walk the match object? As an exercise, treating the problem like a larger language appeals?

    The idiomatic approach is not to iterate at all, but to define an action class with methods that generate the desired datastructures, and storing them in the ast attribute of the current match object.

    The grammar chapter of our work-in-progress-book talks about this technique a bit (it's also slightly outdated, and I plan to update it within the next few days). (Update: I've updated it both to match the current spec, and to work with current Rakudo. You can find the examples in the simplified_parsing branch on github.)

    $match is supposed to work just like $/, yes?

    Yes. And recent development versions of Rakudo also set $/ in Grammar.parse.

    Since a few days Rakudo also supports the $/.perl method (Perl 6's built-in version of Data::Dumper), which shows you the structure of the match object.

    Perl 6 - links to (nearly) everything that is Perl 6.
      ( Moritz: Thanks. Thursday, I grabbed a copy of the book and moved to the latest rakudo release. I didn't find the .perl method yet; so I'll just salivate for a while more. I will look again at the book--that was helpful. )

      This is going slow as I have little time (and lots of ignorance) but I will keep scratching away.

      #!/home/rir/rakudo/parrot_install/bin/perl6 { grammar Calc { token TOP { <expression> } rule expression { <lhs> <op> <rhs> } token lhs { <numeric> } token rhs { <numeric> } token numeric { \d+[\.\d*]? } token op { '-' | '+' | '*' | '/' | 'x' } } class Calc::Actions { method TOP($/) { make $<expression>.ast } method expression($/) { make eval "$/<lhs> $/<op>.ast() $/<rhs +>" } method lhs($/) { make $/ } method rhs($/) { make $/ } method numeric($/) { make $/ } method op($/) { if ( $/ eq 'x') { make '*'; } else {make $/; } + } } my $m = Calc.parse( "8.8 x 5.0 - 2", :actions( Calc::Actions)); die "dying no match" unless $m; say "$m<expression> = $m.ast()"; }
      Ok, I have retrenched. I grabbed Rakudo's/Pug's spectest and found some grammars that compile out of the box. The above is built up from t/spec/S05-grammarr/action-stubs.t . (Running spectest was less onerous than I expected.)

      The above is meant to model a cheap calculator.

      Questions:

      • Is the string eval reasonable here?
      • 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> }
      • The above can easily be extended by changing the rhs token line to
        token rhs { <numeric> | <expression> }
        But this destroys the calculator's left associativity and equal precedence. I suppose this could be addressed by changing the rule expression line to:
        rule expression { <lhs> ( <op> <rhs> )+ }
        but I'm getting lost here.

      Be well,
      rir

        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.

        Update:

        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.