hello gentlemen,

I'm preparing a talk about Perl6 for the Italian Perl Workshop, and I would like to have a slide comparing a BNF (yacc/bison) grammar to a Perl6 one, to show how powerful in parsing/lexing Perl6 regexen are.

so I ask your assistance in helping me putting up a simple, yet impressive enough, sample.

I've taken this bison example (an RPN calculator, stripped down version from http://www.gnu.org/software/bison/manual/html_node/Rpcalc-Rules.html):

 input:    /* empty */
         | input line
 ;

 line:     '\n'
         | exp '\n'
 ;

 exp:      NUM
         | exp exp '+'
         | exp exp '-'
         | exp exp '*'
         | exp exp '/'
 ;
 %%

and this is my attempt at a "port" to Perl6:

 grammar RPN {
     rule input { <line>* }
     rule line { <exp>? \n }
     rule exp {
         [ NUM
         | <exp> <exp> <'+'>
         | <exp> <exp> <'-'>
         | <exp> <exp> <'*'>
         | <exp> <exp> <'/'>
         ]
     }

     rule NUM { \d+ } # could be more complex, I know :-)
 }

 if $data ~~ RPN.input { say "valid RPN data" }

am I missing something obvious here? will the above code work in Perl6?
do you have perhaps a better example, or just an idea about what to show?

cheers,
Aldo




Reply via email to