Camelia
Prev: Replace

Grammar

examples/regex2/add.p6
use v6;

BEGIN {
    @*INC.push('examples/regex2/');
}
use Add1;

my @experssions = (
    "2 + 3",
    "2 + 4 ",
    "2 + 3 x",
    "2 +",
    "2 3",
    "2 - 3",
);

for @experssions -> $exp {
    print $exp, " ";
    my $result = Add1.parse($exp);
    say $result ?? 'OK' !! 'NOT OK'; 
    CATCH {
        say "exception received: $!";
    }
}


examples/regex2/Add1.pm
grammar Add1 {
    rule TOP { ^ <math> $ }
    rule math { <operand> <operator> <operand> }
    token operand { \d+ }
    token operator { <[\+\*]> }
}

examples/regex2/Add1.pm.out
2 + 3 OK
2 + 4  OK
2 + 3 x NOT OK
2 + NOT OK
2 3 NOT OK
2 - 3 NOT OK

Prev: Replace

Table of Contents

Comments

In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.
Email:
Name: