convert a logical expression to reverse Polish notation in Java
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Does anybody know how to convert a logical expression to reverse Polish notation in Java?
Could you please add some detail here? Are you simply trying to convert the string "((A AND B) OR C)" to something like "push A, push B, AND, push C, OR"?
sorry if I was not clear but you understood correctly.
This sounds suspiciously like a homework assignment. You will get no further help from me until you post some code showing how you are trying to solve this problem. Someone else might help you, but that's their mistake.
The best way to go about it is to process the statement recursively. The first thing you need to be able to do is parse the data and queue sub-statements into a tree. Think of it as factoring the statement.
First you need to assume the following:
Everything is either a statement or an operator
An operator can turn one or more statements into a larger statement
All statements can be broken down into a combination of "atomic" statements and operators
You really have two ways to approach it. The first is in the manner a C-based compiler does, by build awareness of the overall statement one character at a time using a linear stream of source input. The second would be to load the statement as a whole and bisect it into statement pairs conjoined by operators until you can't split any of the remaining statements. In either case, you'd want to create a hierarchically-represented statement tree, or essentially a binary tree of statements.
Once you have a tree outlining the basic components of the statement, you just need to traverse the tree with a compilation function that converts each relationship into the appropriate form.
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.