Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
( Moritz: Thanks. Thursday, I grabbed a copy of the book and moved to the latest rakudo release. I didn't find the .perl method yet; so I'll just salivate for a while more. I will look again at the book--that was helpful. )

This is going slow as I have little time (and lots of ignorance) but I will keep scratching away.

#!/home/rir/rakudo/parrot_install/bin/perl6 { grammar Calc { token TOP { <expression> } rule expression { <lhs> <op> <rhs> } token lhs { <numeric> } token rhs { <numeric> } token numeric { \d+[\.\d*]? } token op { '-' | '+' | '*' | '/' | 'x' } } class Calc::Actions { method TOP($/) { make $<expression>.ast } method expression($/) { make eval "$/<lhs> $/<op>.ast() $/<rhs +>" } method lhs($/) { make $/ } method rhs($/) { make $/ } method numeric($/) { make $/ } method op($/) { if ( $/ eq 'x') { make '*'; } else {make $/; } + } } my $m = Calc.parse( "8.8 x 5.0 - 2", :actions( Calc::Actions)); die "dying no match" unless $m; say "$m<expression> = $m.ast()"; }
Ok, I have retrenched. I grabbed Rakudo's/Pug's spectest and found some grammars that compile out of the box. The above is built up from t/spec/S05-grammarr/action-stubs.t . (Running spectest was less onerous than I expected.)

The above is meant to model a cheap calculator.

Questions:

  • Is the string eval reasonable here?
  • Can the expression rule be handled without the separate naming of the lhs and rhs? Is there a way to access each numeric in something like:
    rule expression { <numeric> <op> <numeric> }
  • The above can easily be extended by changing the rhs token line to
    token rhs { <numeric> | <expression> }
    But this destroys the calculator's left associativity and equal precedence. I suppose this could be addressed by changing the rule expression line to:
    rule expression { <lhs> ( <op> <rhs> )+ }
    but I'm getting lost here.

Be well,
rir


In reply to Re^2: P6: Beginning grammar by rir
in thread P6: Beginning grammar by rir

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (4)
    As of 2014-12-29 05:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (184 votes), past polls