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
[5482x3]
BTW, there is an error in associativity:

>> parse-equation "3-3-3"
== [subtract 3.0 subtract 3.0 3.0]
(you need to take into account, that some operations are left-associative, 
while other operations may be right-associative)
The same error here:

>> parse-equation "3-3+3"
== [subtract 3.0 add 3.0 3.0]
MaxV
15-Mar-2011
[5485]
Did you see http://www.rebol.org/view-script.r?script=supercalculator.r&sid=f4jz
(script start in the just last 20 lines)
instead of parse you can use "replace/all" and work evene better
Geocaching
15-Mar-2011
[5486x4]
Ladislav, thanks for your comments... I will have a closer look  
on this associativity issue.
MaxV. Yes, I have seen supercalculator but it crashes on simple expression 
like sqrt(1-3)/(1+4)...
MaxV sorry, my mistake... normal it crashes :)

But when i tried it, it fails on an expression... I do not remember 
which one...
MaxV: supercaclulator fails on 1/-(3+2)
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.