Parrot Perl6Grammar compiler and parsers #22

Subrule matches

Grammar:

regex TOP     { <integer> <addop> <integer> {*} }
regex integer { \d+ {*} }
regex addop   { [ '+' | '-' ] {*} }

Action method:

method TOP($/) {
    my $left  := +($<integer>[0]);
    my $right := +($<integer>[1]);
    my $value := $<addop> eq '+' ?? $left + $right !! $left - $right;
    say($value);
}
continued...
Copyright © 2009
http://www.pmichaud.com/2009/pres/