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

World: r3wp

[Parse] Discussion of PARSE dialect

Geocaching
14-Mar-2011
[5472]
Thanks... just uploaded a new file on rebol.org... I mistakenly put 
a old one :)
sqlab
14-Mar-2011
[5473]
this does not work with signed numbers as in "1 + -2"
Geocaching
14-Mar-2011
[5474x4]
indeed, not yet implemented... in the meantime use "1 - 2" instead
And i am not sure how to implement signed numbers... Usinfg the minus 
sign introduces a level of complexiy I want to avaoid in the code. 
I might decide to force the use of a function like neg()... I am 
still thinking about it...
actually, i found a quite straightforward way to handle signed numbers 
:) i will post later today (for now, I am going to ski... shusss!!!!)
Hello sqlab... Signed numbers and signed sub-expression are now properly 
handled :)
>> parse-equation "+(1+-x)**-sqrt(-1--x)"

== [power add 1.0 negate x negate square-root subtract -1.0 negate 
x]

Thanks for pointing my attention to this issue.

http://www.rebol.org/view-script.r?script=parse-equation.r
Endo
15-Mar-2011
[5478x2]
Very nice script. ^ character can also be added as power operator 
just like **. As in some languages ^ is power operator.
Oh and English comments in the source code please, if possible :)
Ladislav
15-Mar-2011
[5480]
Just a terminological note: the strings you convert to REBOL expressions 
are not "mathematical equations". They are just mathematical expressions.
GrahamC
15-Mar-2011
[5481]
an equation has an = sign in the expression
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?