wood burning stoves*
The moose likes Java in General and the fly likes Parsing Math Experesions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing Math Experesions" Watch "Parsing Math Experesions" New topic
Author

Parsing Math Experesions

cyrus khazaei
Greenhorn

Joined: Aug 28, 2007
Posts: 16
Dear All:
I want to write a math parser as a part of my finite element nmodeling code (a method to analyse continious media such as building,dam,tunnel...)
But what must be parsed is more complicated than simple math expressions.Is there any book or web page you can introduce me for full parsing learning?

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41852
    
  63
In which way is it more complicated than simple math expressions? There are a number of libraries that do a good job at parsing expressions, and which can be extended with your own functions.


Ping & DNS - my free Android networking tools app
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
I found this books very informative when writing a (simple) mathematical parser:
http://books.google.com/books?id=uB-eYkcxImEC&pg=PP1&dq=Building+Parsers+with+Java&ei=Er_rRuigNJ306wLYrLAo&sig=QvkFUV_HODy8Dsvn85OO0qLhsAQ
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Writing your own parser can be a lot of work. I don't know if you already have an exact idea of how the expressions should look like that you want to parse. Do they look like Java or JavaScript (or even some other existing scripting language)?

Java 6 includes a scripting engine which can evaluate JavaScript expressions (and also other scripting languages if you plug-in an interpreter for the language). Here's an example:

(Note: Remove the space in "ev al" above; JavaRanch doesn't let me post the word without the space, the forum software thinks I'm trying to insert an evil JavaScript into the page... :roll: )

There's also BeanShell, which can evaluate expressions in Java.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
Recently I used antlr to create a simple math parser. Once you get the hang of it, then it is pretty easy and powerful.

http://www.antlr.org/

This book is very helpful and was written by the creator of antlr. http://www.pragmaticprogrammer.com/title/tpantlr/

This is my parser so far. It allows for math input with variables such as ((4*5*7+55)*(myvar1*myvar2))/count

http://fdsapi.cvs.sourceforge.net/fdsapi/fdsapi/Code/com/fdsapi/parser/FDSMath.g?revision=1.3&view=markup


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
cyrus khazaei
Greenhorn

Joined: Aug 28, 2007
Posts: 16
Originally posted by Ulf Dittmer:
In which way is it more complicated than simple math expressions? There are a number of libraries that do a good job at parsing expressions, and which can be extended with your own functions.


It's more complicated because I must pars expressions within each other and more important, because I must pars derivatives(for example: dx/dy=(2z+d2x/dw2))
Of course maybe you are right.If I omit derivatives, even expressions inside each other( for example:y=(2*x+ln(sin(sqrt(power(x,4)))) ) can be assumed as collection of simple math expressions.
By the way you said: "There are a number of libraries" What's the name of these libraries?What should I search for?
Thanks however
[ September 16, 2007: Message edited by: Jim Yingst ]
cyrus khazaei
Greenhorn

Joined: Aug 28, 2007
Posts: 16
Originally posted by Piet Verdriet:
I found this books very informative when writing a (simple) mathematical parser:
http://books.google.com/books?id=uB-eYkcxImEC&pg=PP1&dq=Building+Parsers+with+Java&ei=Er_rRuigNJ306wLYrLAo&sig=QvkFUV_HODy8Dsvn85OO0qLhsAQ


Thanks Piet.I will read it.It seems a good book.
cyrus khazaei
Greenhorn

Joined: Aug 28, 2007
Posts: 16
Originally posted by Jesper Young:
Writing your own parser can be a lot of work. I don't know if you already have an exact idea of how the expressions should look like that you want to parse. Do they look like Java or JavaScript (or even some other existing scripting language)?

Java 6 includes a scripting engine which can evaluate JavaScript expressions (and also other scripting languages if you plug-in an interpreter for the language). Here's an example:

(Note: Remove the space in "ev al" above; JavaRanch doesn't let me post the word without the space, the forum software thinks I'm trying to insert an evil JavaScript into the page... :roll: )

There's also BeanShell, which can evaluate expressions in Java.


Thanks for your very helpful guide Jasper.Does these engine indlude derivitivies(like dx/dy=...) and integrals (Some times user might want to enter an integral that must be solved numerically)?

I know it seems very difficult
cyrus khazaei
Greenhorn

Joined: Aug 28, 2007
Posts: 16
Originally posted by steve souza:
Recently I used antlr to create a simple math parser. Once you get the hang of it, then it is pretty easy and powerful.

http://www.antlr.org/

This book is very helpful and was written by the creator of antlr. http://www.pragmaticprogrammer.com/title/tpantlr/

This is my parser so far. It allows for math input with variables such as ((4*5*7+55)*(myvar1*myvar2))/count

http://fdsapi.cvs.sourceforge.net/fdsapi/fdsapi/Code/com/fdsapi/parser/FDSMath.g?revision=1.3&view=markup


Thank you steve.Very helpful comment
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41852
    
  63
Originally posted by cyrus khazaei:
By the way you said: "There are a number of libraries" What's the name of these libraries?What should I search for?


The search term would be something like "java expression evaluation". Libraries I've used include JEP, JEP and Javassist; they're all linked in the JavaIntermediateFaq. They do handle nested expressions, but not derivations.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
You could also go to sourceforge and search for java math or something similar.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Visit: Kenneth Louden's Home Page , there to examine the book - Compiler Construction -Compiler Construction
Principles and Practice


Also, Javanotes_5 has a parser/math parser it being written in Java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parsing Math Experesions