r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Parse] Discussion of PARSE dialect

Ladislav
15-Mar-2011
[5490]
regarding the supercalculator - it has wrong associativity as well
Geocaching
15-Mar-2011
[5491x2]
like parse-equation.r did yesterday btw :)
Ladislav... sorry, what should be the code returned?
Ladislav
15-Mar-2011
[5493]
in which case?
Geocaching
15-Mar-2011
[5494]
3-3+3
Ladislav
15-Mar-2011
[5495]
add subtract 3 3 3
Geocaching
15-Mar-2011
[5496]
Do you have an example where this would make a difference?
Ladislav
15-Mar-2011
[5497x2]
>> subtract 3.0 add 3.0 3.0
== -3.0

>> add subtract 3.0 3.0 3.0
== 3.0
I think, that counts as a difference
Geocaching
15-Mar-2011
[5499x2]
oups :)
Do you think that parsing reversal would fix the problem
Ladislav
15-Mar-2011
[5501x2]
not completely
you need to realize, that the ** operator is right-associative
Geocaching
15-Mar-2011
[5503x2]
I should review some basic math stuff :))
Ladislav... according to wiki, addition, subtraction, multiplication 
and division are left-assiciative... http://en.wikipedia.org/wiki/Operator_associativity
Ladislav
15-Mar-2011
[5505]
yes
Geocaching
15-Mar-2011
[5506x2]
Now that I take a paper and a pen... left-associativity is indeed 
natural :)
So natural that i forgot about the associativity when thinking on 
how to split an expression i successive blocks! So stupid!
Ladislav
15-Mar-2011
[5508]
:-D
Geocaching
15-Mar-2011
[5509]
Thanks for pointing this. Hopefully, I have been clever not to mark 
this version as 1.0.0 :)
Ladislav
15-Mar-2011
[5510]
I put my %evaluate.r script processing operators with different priorities 
and associativities to the

http://www.rebol.org/view-script.r?script=evaluate.r


as well, since I think there it will be more accessible to the public
Geocaching
15-Mar-2011
[5511x2]
Your script is so much more complicated :)... maybe why it works... 
Good idea to put on rebol.org!
A quick doc to show how to use it?
Ladislav
15-Mar-2011
[5513x2]
Much more complicated

 - do not forget it processes three different rule sets, each of which 
 uses different priorities and associativities.
How to use it: check the examples at the bottom
Geocaching
15-Mar-2011
[5515x2]
yes... the concept of "rule sets" is not straightforward :) what 
do you mean by this?
OK... very nice indeed! But can I use "unknown variable"
Ladislav
15-Mar-2011
[5517x4]
the rule sets are described in the comments to the SRA, STD and RLE 
functions
>> mold sra [3 - 3 + 3] translate
== "[subtract 3 add 3 3]"

>> mold std [3 - 3 + 3] translate
== "[add subtract 3 3 3]"
unknown variable
? you need to use the %use-rule.r script:

http://www.rebol.org/view-script.r?script=use-rule.r
I guess, that you actually want to use just the STD function
Geocaching
15-Mar-2011
[5521]
I will adopt your script! Impressive. But i am still wondering if 
with the strategy in my script it would be possible to implement 
associativity properly?
Ladislav
15-Mar-2011
[5522x3]
well, if you modify it appropriately, why not?
except, that it requires some care, since all the operators you use 
are left-associative, except for the exponentiation operator, which 
is right-associative
>> std [3 ** 3 ** 3] translate
== [power 3 power 3 3]

>> std [3 * 3 * 3] translate
== [multiply multiply 3 3 3]
Geocaching
15-Mar-2011
[5525]
THat's why parsing reversal and taking care of ** specifically might 
be a good start...
Ladislav
15-Mar-2011
[5526x2]
Parsing reversal does not help IMO, because you have to process both 
left- and right-associativity at the same time
Notice, that my script parses left to right
Geocaching
15-Mar-2011
[5528x4]
I think I found a way... see you later today (or tomorraow). Again, 
many thanks for your feedback and congrat for your impressive script
It was actually so easy to fix: 5 'append to change into 'insert... 
I still have to check this does not break anything before posting 
a new version on rebol.org.

Ladislav... thank you again for pointing this bug!
Voilą... I renamed my script as parse-expression.r and it is on rebol.org 
... http://www.rebol.org/script-information.r?script-name=parse-expression.r

I just had to repace 5 'append into 'insert, so it fixing this big 
did not brak the "philosophy" of the implementation. Ouf :)

Example:
>> do parse-expression "-(-3+2)+3-2+3-sqrt((1+2)**2)-2**3+34"
== 28.0
@Endo: I tried exponent with ^, but as it is the escape character 
for rebol, i consistently end up with an error. If enyone can help 
on this! 

For the english comment, I promise I will take the time to translate 
the french comments soon.
Gregg
15-Mar-2011
[5532]
Nice work Francois. Good conversation here too, with important details 
brought out by Ladislav.
Geocaching
15-Mar-2011
[5533x2]
Yes... Ladislav reminds me some basic math! God, I felt so stupid 
about this associativity bug! 

The reason why I developped parse-expression.r is because I need 
it to build an companion app for one of the best math book: Calculus 
3d edition from Smith & Minton! For now, I have developped a rebol 
library to transform any vid face into a function plotter, and parse-expression.r 
allows me to use human readable expression in the gui instead of 
guru rebol code :)
Ladislav:

How would you interpret x**-1/2 :
(1) [divide power x -1 2]
or 
(2) [power x divide -1 2]


A previous version of parse-expression.r returned (2)... but i considered 
this as a bug and changed it for (1) (see history 0.9.2 in the header 
of the script). But now, with our discussion on associativity, i 
am not sure anymore... 

Thanks for your help!
Steeve
15-Mar-2011
[5535]
Precedence of ** is higher than /
The right form should be (1)
Geocaching
15-Mar-2011
[5536]
Thanks, this confirms my feeling
Steeve
15-Mar-2011
[5537x3]
the associativity concern operators with same precedence.
** is right associative, the other ones are left associative
So, 2 ** 3 ** 4
is evaluated from rigth to left
like
2 ** (3 ** 4)