Rafael Garcia-Suarez writes:
> Luke Palmer wrote:
> > Also, if this is going to be an explanation rather than just a picture,
> > I suggest you go with Perl's usual versatile power, and store the
> > operators in a declarative data source.
> > 
> >     grammar RPN {
> >         my @operator = << + - * / >>;
> > 
> >         rule input { <line>* }
> >         rule line  { <exp>? \n }
> >         rule exp :w{ <NUM> | <@operator> }
> >         
> >         rule NUM   { \d+ }
> 
> This becomes too weak.
> 
>     rule exp :w{ <NUM> | <exp> <exp> <@binary_operator> }
> 
> is stronger.
> But it involves left-recursion, so it's typically better adapted to
> yacc-like parser generators. Probably not to P6 rules...

Whoops, yeah, that's what I meant to write.  That is, after I realized
we were working with an RPN caluclator and not an infix one.  And even
then my brain was broken.

That left recursion won't do.  I can't remember my transformation rules
well enough to know how to put that in a form suitable for a recursive
descent parser.  To be honest, I've never seen an RPN calculator modeled
with a grammar.

To be sure, a prefix calculator should be the easiest:

        rule exp :w { <@operator> <exp> <exp> | <NUM> }

Luke

Reply via email to