Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Operator Associativity and Eliminating Left-Recursion in Parse::RecDescent

by ikegami (Patriarch)
on Jun 06, 2006 at 18:52 UTC ( [id://553889]=perltutorial: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    If executed from left-to-right,
    4 - 5 + 6 = (4 - 5) + 6 = 5
    
    If executed from right-to-left,
    4 - 5 + 6 = 4 - (5 + 6) = -7
    
  2. or download this
    If executed from left-to-right,
    4 ** 3 ** 2 = (4 ** 3) ** 2 = 4096
    
    If executed from right-to-left,
    4 ** 3 ** 2 = 4 ** (3 ** 2) = 262144
    
  3. or download this
    sum : sum /[+-]/ NUM
        | NUM
    
  4. or download this
    pow : NUM '**' pow
        | NUM
    
  5. or download this
    sum : sum '+' NUM { $item[1] + $item[3] }
        | sum '-' NUM { $item[1] - $item[3] }
        | NUM         { $item[1]            }
    
  6. or download this
    pow : NUM '**' sum { $item[1] ** $item[3] }
        | NUM          { $item[1]             }
    
  7. or download this
    sum : sum /[+-]/ NUM { [ @item[2,1,3] ] }
        | NUM            { [ $item[1]     ] }
    
  8. or download this
    pow : NUM '**' pow { [ @item[2,1,3] ] }
        | NUM          { [ $item[1]     ] }
    
  9. or download this
    sum  : NUM sum_        { [ $item[1], @{$item[2]} ] }
    sum_ : /[+-]/ NUM sum_ { [ $item[1], $item[2], @{$item[3]} ] }
         |                 { [] }
    
  10. or download this
    {
       sub eval_sum {
    ...
    sum  : NUM sum_         { eval_sum($item[1], @{$item[2]}) }
    sum_ : /[+-]/ NUM sum_  { [ $item[1], $item[2], @{$item[3]} ] }
         |                  { [] }
    
  11. or download this
    {
       sub treeify {
    ...
    sum  : NUM sum_         { treeify($item[1], @{$item[2]}) }
    sum_ : /[+-]/ NUM sum_  { [ $item[1], $item[2], @{$item[3]} ] }
         |                  { [] }
    
  12. or download this
    {
       sub eval_sum {
    ...
    }
    
    sum : <leftop: NUM /[+-]/ NUM> { eval_sum(@{$item[1]}) }
    
  13. or download this
    {
       sub treeify {
    ...
    }
    
    sum : <leftop: NUM /[+-]/ NUM> { treeify(@{$item[1]}) }
    
  14. or download this
    rule1: token rule2
    rule2: token rule3
    rule3: token
    
  15. or download this
    sum  : NUM sum_[ $item[1] ]
    sum_ : '+' NUM sum_[ $arg[0] + $item[2] ]
         | '-' NUM sum_[ $arg[0] - $item[2] ]
         | { $arg[0] }
    
  16. or download this
    sum  : NUM sum_[ $item[1] ]
    sum_ : '+' NUM sum_[ [ $item[1], $arg[0], $item[2] ] ]
         | '-' NUM sum_[ [ $item[1], $arg[0], $item[2] ] ]
         | { $arg[0] }
    
  17. or download this
    pow : NUM '**' pow
        | NUM
    
  18. or download this
    pow   : NUM pow_
    pow_  : '**' pow
          |
    
  19. or download this
    {
       sub eval_pow {
    ...
    pow  : NUM pow_      { eval_pow($item[1], @{$item[2]}) }
    pow_ : '**' NUM pow_ { [ $item[1], $item[2], @{$item[3]} ] }
         |               { [] }
    
  20. or download this
    {
       sub treeify_r {
    ...
    pow  : NUM pow_      { treeify_r($item[1], @{$item[2]}) }
    pow_ : '**' NUM pow_ { [ $item[1], $item[2], @{$item[3]} ] }
         |               { [] }
    
  21. or download this
    {
       sub eval_pow {
    ...
    }
    
    pow : <rightop: NUM /(\*\*)/ NUM> { eval_pow(@{$item[1]}) }
    
  22. or download this
    {
       sub treeify_r {
    ...
    }
    
    pow : <rightop: NUM /(\*\*)/ NUM> { treeify_r(@{$item[1]}) }
    
  23. or download this
    pow : NUM '**' pow { $item[1] ** $item[3] }
        | NUM          { $item[1] }
    
  24. or download this
    pow   : NUM pow_
    pow_  : '**' pow { <<pow's $item[1]>> ** $item[2] }
          |          { <<pow's $item[1]>> }
    
  25. or download this
    pow  : NUM pow_[ $item[1] ]
    pow_ : '**' pow { $arg[0] ** $item[2] }
         |          { $arg[0]             }
    
  26. or download this
    pow  : NUM pow_[ $item[1] ]
    pow_ : '**' pow { [ $item[1], $arg[0], $item[2] ] }
         |          { $arg[0]                         }
    
  27. or download this
    Demonstrates left-associativity
    4-5+6   =  5  got  5
    ...
    4**3**2   = 262144  got 262144
    (4**3)**2 =   4096  got   4096
    4**(3**2) = 262144  got 262144
    
  28. or download this
    use strict;
    use warnings;
    ...
       my $got = $parser->parse($expr);
       print("$expr = $expected  got $got\n");
    }
    
  29. or download this
    use strict;
    use warnings;
    ...
       my $got = eval_node($tree);
       print("$expr = $expected  got $got\n");
    }
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perltutorial [id://553889]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-19 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found