LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2008, 11:55 AM   #16
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76

Hi, Sergei.
Quote:
Originally Posted by Sergei Steshenko View Post
Sure, but what for - if Java parser already does this ?

The point is not to write code if it's already available.
I agree with you in many contexts. The OP wrote:
Quote:
Originally Posted by rabbit2345
... but i am curious, is there a way to split it multiple ways and solve?
I think we are all curious about how to solve problems at more than one level -- we may not always be writing in Java, for example, and so may not have something already written for us. I have found that knowing more than one way to solve a problem often comes in handy.

We can be constructively lazy on one hand, and, on the other hand, we can investigate deeper -- we can look at algorithms upon which are based extant facilities. Those facilities may be available to us through libraries. Sometimes we don't care how something performs a task. Sometimes we might.

Let us take a step back: if we are only interested in evaluating an arithmetic expression, there are many higher-level solutions that do not involve writing any code ... cheers, makyo
 
Old 11-02-2008, 12:12 PM   #17
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by makyo View Post
Hi, Sergei.

I agree with you in many contexts. The OP wrote:

I think we are all curious about how to solve problems at more than one level -- we may not always be writing in Java, for example, and so may not have something already written for us. I have found that knowing more than one way to solve a problem often comes in handy.

We can be constructively lazy on one hand, and, on the other hand, we can investigate deeper -- we can look at algorithms upon which are based extant facilities. Those facilities may be available to us through libraries. Sometimes we don't care how something performs a task. Sometimes we might.

Let us take a step back: if we are only interested in evaluating an arithmetic expression, there are many higher-level solutions that do not involve writing any code ... cheers, makyo
Here is the first web search match: http://sourceforge.net/projects/jeplite/ .
 
Old 11-02-2008, 04:03 PM   #18
rabbit2345
Member
 
Registered: Apr 2007
Location: SC
Distribution: Kubuntu 20.04 LTS
Posts: 378

Original Poster
Rep: Reputation: 41
I do have this idea that maybe, since java evaluates equations when they are set equal to a variable, maybe I could set the equaiton to a variable?

like this:

if I type 1/2+2, then it sets that particular string equal to something else.
as if you were doing this:
x = 1/2+2;

is this something possible?
 
Old 11-02-2008, 05:30 PM   #19
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
No, that is not possible. Anything you enter on the command line is read as a String - and the problem is that you cannot perform math on a String without first breaking it up and converting the numerical parts into integer of floating-point values. This is very easy to find out: if you make x and you assign "1/2+2", you will run into an exception.
 
Old 11-03-2008, 08:13 AM   #20
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
> like jonaskoelker said, what if someone put in 1+2*3? that would split to 1+(2*3)

If you only allow binary operators and no parenthesis, I'd go for the bottom-up solution:

1. Split the string into a linked list ["1", "+", "2", "*", "3"].
2. For each precedence level of operators (i.e. first for "*" and "/", next for "+" and "-"):
Run left-to-right, whenever you see [number, one of your operators, number] in your array, evaluate that subexpression and replace the sublist with its result.
3. Your list should have one number left now. That's your result.

You probably want to specify negative numbers with something other than "-", otherwise you have to parse "-3*-2--4".

Otherwise, find a good, long introduction to parsing. Find the grammar for a few languages you know (java, c, python) and study how they parse things [or if you rather want to find things out on your own, skip this step]. Find a parser tool, use it to parse expressions.

It'll serve you well later to know this. If you don't know regular expressions, you may want to learn about those if you want to write a good lexer [which efficient parsing really requires].
 
Old 11-03-2008, 07:07 PM   #21
rabbit2345
Member
 
Registered: Apr 2007
Location: SC
Distribution: Kubuntu 20.04 LTS
Posts: 378

Original Poster
Rep: Reputation: 41
ok, i have a idea of how to do this

since java evaluates expressions like when they are set like:
x = 1/2+3*2;

is there a way to set an equation as the value of a variable?
so if I enter 1+2+3, it will set a variable to 1+2+3, and solve?

is this possible?
 
Old 11-03-2008, 07:20 PM   #22
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
No, that is not possible. See my previous post for details. Whatever you enter on the command line is simply a constant value - but operators cannot be simply constant values, they need to have a special meaning. This will not happen automatically so it is up to you to split and sort to output into numbers and operators and turn those into a real expression.
 
Old 11-03-2008, 09:28 PM   #23
rabbit2345
Member
 
Registered: Apr 2007
Location: SC
Distribution: Kubuntu 20.04 LTS
Posts: 378

Original Poster
Rep: Reputation: 41
oops sry, i didn't see the 2nd page

but anyway, is there a more efficient way to do this than to split the string into 6 different parts and do the math that way?
I'm pretty sure I'm not the first person to want to do this kind of thing
 
Old 11-03-2008, 09:56 PM   #24
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
You could use a stream, which would allow you to read in one character at a time but then you'd still have to figure out which are numbers and which are operators. If you prefer being lazy, then you could pass the expression to another application such as bc - but that would be cheating and it would most definitely only work on something like Linux (because you can't expect Mac or Windows users to have that specific application installed). And - obviously - if bc does some things differently, you're back to where you started as you'll need to make certain modifications before passing the expression on. In short, no, there isn't any magic.
 
Old 11-04-2008, 01:38 AM   #25
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
Quote:
so if I enter 1+2+3, it will set a variable to 1+2+3, and solve?
It's possible to invoke javac from a java program: it's available as a class. I think you have to create code in a string like this:

Code:
"class Evaluator {
  public static int eval() {
     return (" + expr + ");
  }
}"
Then get the resulting class, and invoke the eval method using introspection (I'm not sure). Remember to remove all references to the class unless you want to leak memory. I think maybe the class loader stores a copy, but I wouldn't know.
 
Old 11-04-2008, 03:57 AM   #26
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jonaskoelker View Post
It's possible to invoke javac from a java program: it's available as a class. I think you have to create code in a string like this:

Code:
"class Evaluator {
  public static int eval() {
     return (" + expr + ");
  }
}"
Then get the resulting class, and invoke the eval method using introspection (I'm not sure). Remember to remove all references to the class unless you want to leak memory. I think maybe the class loader stores a copy, but I wouldn't know.
It's already been suggested without going into details:

http://www.linuxquestions.org/questi...95#post3328495
.
 
Old 11-04-2008, 09:09 AM   #27
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I seems to me that the OP is trying to gain an understanding of parsers, grammars, and such related concepts (even though he may not know this). One traditional way of generating such codes is through the use of tools such as lex/flex and yacc/bison, which are good at generating lexical analyzers and parsers in C. For java, Sun has produced the equivalent tool set 'Jack' (Yacc for Java). Using tools such as this can be quite instructive, as well as productive.
While I've not used Jack, it is allegedly equivalent to flex & bison. The two have separate but related jobs, that divide the process into separate steps. The first step is to tokenize the input into generalized types of data, such as specific operators (+, -, *, /, etc.), numeric types (integers, reals, etc), and other elements such as parentheses, text (for function names like trigonometry, logs, etc.). The second step is to assemble the component parts into an expression and then evaluate it. Flex & bison (for Java, Jack) generate code that correctly performs these functions, as well as some glue code that makes them work together, if desired. The tools take a special input format using regular expressions and other notation to describe the way the input stream is to broken into tokens (the lexical analyzer part), and then evaluated as an expression. Understanding these tools will give significant insight into expression evaluation.

--- rod.
 
Old 11-04-2008, 09:19 AM   #28
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by theNbomr View Post
I seems to me that the OP is trying to gain an understanding of parsers, grammars, and such related concepts (even though he may not know this). One traditional way of generating such codes is through the use of tools such as lex/flex and yacc/bison, which are good at generating lexical analyzers and parsers in C. For java, Sun has produced the equivalent tool set 'Jack' (Yacc for Java). Using tools such as this can be quite instructive, as well as productive.
While I've not used Jack, it is allegedly equivalent to flex & bison. The two have separate but related jobs, that divide the process into separate steps. The first step is to tokenize the input into generalized types of data, such as specific operators (+, -, *, /, etc.), numeric types (integers, reals, etc), and other elements such as parentheses, text (for function names like trigonometry, logs, etc.). The second step is to assemble the component parts into an expression and then evaluate it. Flex & bison (for Java, Jack) generate code that correctly performs these functions, as well as some glue code that makes them work together, if desired. The tools take a special input format using regular expressions and other notation to describe the way the input stream is to broken into tokens (the lexical analyzer part), and then evaluated as an expression. Understanding these tools will give significant insight into expression evaluation.

--- rod.
http://java-source.net/open-source/parser-generators
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
plz help with JAVA String thtr2k Programming 3 11-03-2007 01:04 AM
How to encrypt a string in Java me4linux Programming 1 09-03-2007 03:30 PM
JAVA: Cleaning up an ugly equation Garda Programming 7 08-07-2007 05:59 PM
how to solve Character and String problems on linux?(screenshot) tradingbr Linux - General 0 04-29-2004 02:51 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration