aspose file tools*
The moose likes Java in General and the fly likes Incorect operations order when implementing math parser with JavaCC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Incorect operations order when implementing math parser with JavaCC" Watch "Incorect operations order when implementing math parser with JavaCC" New topic
Author

Incorect operations order when implementing math parser with JavaCC

Arsen Liapin
Greenhorn

Joined: Aug 04, 2009
Posts: 11

hello, gentlemen.

Have problem (incorrect operator order) with JavaCC when implementing Math parser

Here is my grammar code



When testing on expression "1-1+1" I am getting "-1", while should get "1". As I understood - this is because of LL-nature of JavaCC.

Debug output is:



Any help appreciated. Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Why have you re-assigned "a" in your add method?
Arsen Liapin
Greenhorn

Joined: Aug 04, 2009
Posts: 11
I founded example in the web and applied to my case, code from that example showed below




When googling, i founded that this guy have same issue, but solution absent

http://www.experts-exchange.com/Programming/Theory/Software-Design/Q_21550807.html

Btw, parsing works good except operators order. For example "1+2+3" is "6" "1+1-1" is "1"
I need workaround for cases like "1-1+1"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
You don't want a workaround at all. You need to understand your grammar first. The solution you were given is totally different from what you wrote.

There is actually a simpler way to write an add method. What language is the example with expr(): ? It doesn't look like Java.
Arsen Liapin
Greenhorn

Joined: Aug 04, 2009
Posts: 11
Actually, this is grammar for Java Compiler Compiler (JavaCC, https://javacc.dev.java.net/)
And, of course this is not pure java

I need parser for math expressions with possibility to override operators (third-party tools which I founded are either do not have this feature or heavy and costs a lot).
And it's stupid idea to write my own parser while there are such tools already exists: JavaCC, ANTLR, yacc, SableCC, etc.
So, I decided to use JavaCC, wrote simple grammar, compiled it (received a bunch of pure Java classes which represents Lexer and Parser).
By passing some string expression to Parser (i.e. like "1+2+3/(5+3)") to it - I am receiving results of this expression.



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
So what does your grammar think "1 + 2 - 3" is?
Arsen Liapin
Greenhorn

Joined: Aug 04, 2009
Posts: 11
Hi, Ritchie once again

Ok, here is debug for "1 + 2 - 3"



so, result is 0

lets try for "1 - 3 + 2"



Result is -4

This is because how Operators tree built. For 1 - 3 + 2 it working as "1 minus (3 plus 2), while it should be "(1 minus 3) plus 2"
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
And what have you written in your grammar? You have obviously managed to give it the misconception that "-" is a right-associative operator.
Arsen Liapin
Greenhorn

Joined: Aug 04, 2009
Posts: 11
Ok, so, could you please show me how this grammar should look like ?
Thanks.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Tell us what grammar you have got for your parser. I am not familiar with javaCC, only yacc and CUP. But they all take grammars in similar forms. You will only have to write about two lines which will look something like this (assuming you use BNF)
Arsen Liapin
Greenhorn

Joined: Aug 04, 2009
Posts: 11
Full grammar is listed in my first message.
I simplified it (used only operators "+" and "-").

The code which correspondent to



is

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
That isn't a grammar, that's code. I can't work back from that code to a grammar.
Arsen Liapin
Greenhorn

Joined: Aug 04, 2009
Posts: 11
Nope, that's the grammar file.
It uses same syntax as these ones(Grammar contributions from the community : https://javacc.dev.java.net/servlets/ProjectDocumentList?folderID=110

I have .jj - file with listed grammar in it, passing it as argument for compilation i.e. "javacc.bat myfile.jj"
And receiving java-sources after this compilation finishes.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Those files (I have only read the C file) have a mixture of Java-like syntax (which I am not familiar with) and BNF (=Backus-Naur Format), which I am familiar with. You will have to write out the grammar you are using in BNF (or bunch notation, which you probably aren't familiar with), otherwise I can't understand it.

I can't see anything which looks like BNF in what you quoted, so it looks as if you had written code without a grammar. Get your grammar correct, then insert it into the JavaCC code, and it will work
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Incorect operations order when implementing math parser with JavaCC