• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Incorect operations order when implementing math parser with JavaCC

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why have you re-assigned "a" in your add method?
 
Arsen Liapin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what does your grammar think "1 + 2 - 3" is?
 
Arsen Liapin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so, could you please show me how this grammar should look like ?
Thanks.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Full grammar is listed in my first message.
I simplified it (used only operators "+" and "-").

The code which correspondent to



is

 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That isn't a grammar, that's code. I can't work back from that code to a grammar.
 
Arsen Liapin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic