clojure-numerics / expresso

Clojure library for symbolic computation
314 stars 20 forks source link

Double parentheses #13

Closed GHExplorer closed 9 years ago

GHExplorer commented 9 years ago

Hi,

Do I have the syntax wrong, or is there an issue with double parens?

If you try to simplify the following in infix form, at http://www.mathportal.org/calculators/polynomials-solvers/polynomials-expanding-calculator.php

( ( 1 + a )/2 )*( ( 1 + b )/2 )

, it understand the form, and generates the proper result.

But if you try to just parse it with the expresso library,

( def s_expr "( ( 1 + a )/2 )*( ( 1 + b )/2 )" )

( def lst_expr ( parse-expression s_expr ))

, it complains about the second parens:

espressodemo.core=> lst_expr Parse error at line 1, column 3: ( ( 1 + a )/2 )*( ( 1 + b )/2 ) ^ Expected one of: "["

"[a-zA-Z]"

"-" "+"

"[1-9]"

"[0-9]"

" "

espressodemo.core=>

Or do I have the division operator / wrong?

lucasloisp commented 9 years ago

I haven't used expresso much yet, but it seems that it doesn't handle parens-space very well I tried a number of expressions against parse-expression

(parse-expression "(1+a)") ;;infix addition works well
=> (+ 1 a)
(parse-expression "( 1 + a )") ;;spaced addition as well
=> (+ 1 a)
(parse-expression "((1)+a)") ;;double parens also does
=> (+ 1 a)
(parse-expression "( (1) + a)") ;;same error as reported
=> Parse error at line 1 ...
(parse-expression "((1) + a)") ;;once again
=>Parse error at line 1, column 5:
((1) +a)
    ^
Expected one of:
"**"
"*"
"/"
"+"
"-"
"="
")"

So, it seems to me, that parens which are followed by (or preceded by) a space are the cause. I'll see if I can get to it

GHExplorer commented 9 years ago

Yep, you're right, Lucas. It parsed correctly, without the spaces.

Ok, I can work with that, even before there's a fix. My program is generating the form from a table, so the workaround is to simply stop outputting the spaces.

Very good, thanks.

lucasloisp commented 9 years ago

Sure you're welcome. I'm sorry, school's got me busy, so I couldn't get to fix it. I thought of stripping spaces from the string before its parsed, though it doesn't seem a good enough fix. I'll have to read the instaparse definitions. I may get to it on the weekend

mschuene commented 9 years ago

sorry for the late reply, I had much to do in the last week. You were right that spaces handling is (hopefully was) very brittle with the current parser.

I pushed a fix to master and the clojars latest snapshot. Please check if it works for you and when you find other examples, that are broken

GHExplorer commented 9 years ago

The version is still 0.2.1-SNAPSHOT, correct?

mschuene commented 9 years ago

yes