### Re^2: Operator Precedence Parser

by ikegami (Pope)
 on Jun 09, 2006 at 21:14 UTC ( #554557=note: print w/replies, xml ) Need Help??

in reply to Re: Operator Precedence Parser

• Is 6 / 5 * 4 equal to (6 / 5) * 4 (like Perl) or to 6 / (5 * 4)?
• Is 2 ** 3 ** 4 equal to (2 ** 3) ** 4 or to 2 ** (3 ** 4) (like Perl)?

Deviances from Perl's behaviour should be documented, IMHO.

For fun since it doesn't really matter, something else to look at is the interaction between unary minus, post- and pre- -decrement and -increment. For example,

• a---b
• a----b
• a-----b

Replies are listed 'Best First'.
Re^3: Operator Precedence Parser
by Rhandom (Curate) on Jun 09, 2006 at 22:27 UTC
Thank you for the reply. I tested to see using the following template.

```<h2>6 / 5 * 4</h2>
CET: [% 6 / 5 * 4 %]<br>
Perl: [% PERL %]print 6 / 5 * 4 [% END %]<br>
<hr>

<h2>2 ** 3 ** 4</h2>
CET: [% 2 ** 3 ** 4 %]<br>
Perl: [% PERL %]print 2 ** 3 ** 4[% END %]<br>
<hr>

<h2>a---b</h2>
CET: [% a = 5; b = 2 %][% a---b %]<br>
Perl: [% PERL %]\$a = 5; \$b = 2; print \$a---\$b[% END %]<br>
<hr>

<h2>a--- -b</h2>
CET: [% a = 5; b = 2 %][% a--- -b %]<br>
Perl: [% PERL %]\$a = 5; \$b = 2; print \$a--- -\$b[% END %]<br>
<hr>

<h2>a--- --b</h2>
CET: [% a = 5; b = 2 %][% a--- --b %]<br>
Perl: [% PERL %]\$a = 5; \$b = 2; print \$a--- --\$b[% END %]<br>
<hr>
It printed out:
```6 / 5 * 4
CET: 0.3
Perl: 4.8
2 ** 3 ** 4
CET: 2.41785163922926e+24
Perl: 2.41785163922926e+24
a---b
CET: 3
Perl: 3
a--- -b
CET: 7
Perl: 7
a--- --b
CET: 4
Perl: 4
So - it looks like I need to fix my right vs left vs non-associative. I'll add that to the table and change the parser (it is always doing right right now - it used to always do left - it will be trivial and won't even cause a speed hit to allow it to do both). Thank you - I knew about precedence and precedence makes complete sense - associativity rules seem like they are a little more arbitrary and it seems to cry foul to the user that it isn't consistent (such is legacy). In the perl6 operators table they don't even mention if the operator group is right or left (though it probably does elsewhere in the doc).

So - the infix type will go away in CGI::Ex::Template and will be replaced by left right and non. It needs to match perl.

I'll get that out hopefully soon. Thanks again.

Oh - also to note - the a----b and a-----b examples are both parse errors because the longest term rule makes them try and find a prefix -- immediately after the postfix --. Adding a space after the first three - disabiguates both cases.

my @a=qw(random brilliant braindead); print \$a[rand(@a)];
Well I had posted late-late saturday night that the fix was out in version 2.03 - but it was late enough I forgot to commit the post instead of just preview.

So - version 2.03 has the fixes. And as a bonus it helped to make the code more readable too.

my @a=qw(random brilliant braindead); print \$a[rand(@a)];

Create A New User
Node Status?
node history
Node Type: note [id://554557]
help
Chatterbox?
 [erix]: re: ORMs: sql can easily have many where-clauses so then you'll have to start parsing -- not so easy [erix]: subqueries, filter-clauses, window functions. etc, etc

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2017-09-25 12:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
During the recent solar eclipse, I:

Results (280 votes). Check out past polls.

Notices?