File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing Math Experesions

 
cyrus khazaei
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
steve souza
Ranch Hand
Posts: 862
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
cyrus khazaei
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 862
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also go to sourceforge and search for java math or something similar.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic