World: r3wp
[Parse] Discussion of PARSE dialect
older newer | first last |
Geocaching 11-Mar-2011 [5464x2] | Hello |
Hello, I am struggling to build a parser of equation. The idea is a function who would take sth like "SQRT(1-x^2)/(1-x)^3" and return a block of rebol code: [divide square-root subtract 1 power x 2 power subtract 1 x 3]. Anyone has done it already? | |
Gregg 11-Mar-2011 [5466] | I know Gabriele has done something along those lines, and I'm sure others have as well. I don't know of any that return REBOL code. Can you be more specific about what problems you're having? |
Ladislav 11-Mar-2011 [5467x2] | Geocachi, see http://www.fm.tul.cz/~ladislav/rebol/evaluate.r |
(can return Rebol code) | |
Geocaching 13-Mar-2011 [5469] | Thanks ladislav... but I end up with my own implementation... http://www.rebol.org/view-script.r?script=parse-equation.r But I will check the your implementation to see how it differs :) |
Geocaching 14-Mar-2011 [5470] | Hello, I put a new version of my parse-equation.r script on rebol.org: the parser engine is now completed and finalized. I improved the recursive implementation to make the code much more readable, understandable and elegant. Future improvements should be now limited to adding new math functions and a syntax error handling to return usefull error messages to the user when she/he makes a syntax error in the equation. http://www.rebol.org/view-script.r?script=parse-equation.r |
jocko 14-Mar-2011 [5471] | very nice work ! |
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 [5513] | Much more complicated - do not forget it processes three different rule sets, each of which uses different priorities and associativities. |
older newer | first last |