• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

Class to help validate an arithmetic expression

I'm looking for classes that can assist the validation of an arithmetic expression. For example,  it can capture that
(3+9) *  4 - 2)  is not valid.  And would be good to create a tree that represent this expression.

Do I have to manage  the task by myself?
0
JeabSMJ
Asked:
JeabSMJ
  • 2
  • 2
1 Solution
 
mbormannCommented:
yes ,but do hunt around for free code somewhere .....
0
 
JodCommented:
You need some sort of well formed expression parser.

There are plenty of java parsers out there. JavaCC comes to mind for example.

With JavaCC you write a grammar that describes the format of the data you want to parse and it then generates the program to do it for you.

You could also try putting the data into an XML tree. There is free stuff to do this for you to but this may require a bit more work.
0
 
mbormannCommented:
hey man , i suddenly remembered I had done a simple Reverse Polish notation (postfix) using a simple 'C' code but I have forgotten totally abt it.
I only remember that it used a stack and u could implement the operations like add, substract etc... enforcing precedence without parentheses.
this is what compiler do internally ,if i remember correctly from college days.

Please search for it as I feel that it will suit u to a 't' .

Jod, wot say matey?
0
 
JodCommented:
Yep, postfix notation (or for that matter prefix) will work with only a stack - the problem is to get normal expressions such as the one above into postfix form.

For that you would still need a parser I'm afraid so JavaCC is still your best option unless someone has some free code that converts between infix notation (such as that above) to postfix notation.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now