Camelia
Prev: Grammar with error handling

Grammar that is easier to extend

examples/regex2/Add3.pm
grammar Add3 {
    rule TOP { ^ <math> $ }
    rule math { 
        <operand>
        [ <operator> || [ \D { die "Invalid operator" } ] || {die "missing operator"} ] 
        [ <operand> || { die "Missing second operand" } ]
        
        [ \S { die "Invalid value after the second operand" } ]?
    }
    token operand { \d+ }
    proto token operator { <...> };
    token operator:sym<\+>    { '+' };
    token operator:sym<\*>    { '*' };
}

examples/regex2/Add3.pm.out
2 + 3 OK
2 + 4  OK
2 + 3 x exception received: Invalid value after the second operand
2 + exception received: Missing second operand
2 3 exception received: missing operator
2 - 3 exception received: Invalid operator

Prev: Grammar with error handling

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: