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 08-22-2008, 11:03 AM   #1
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Rep: Reputation: 37
convert a logical expression to reverse Polish notation in Java


Hi there

Does anybody know how to convert a logical expression such as
Quote:
((A AND B) OR C)
to reverse Polish notation in Java?

Thanks
 
Old 08-22-2008, 12:24 PM   #2
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by manolakis View Post
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"?
 
Old 08-22-2008, 03:06 PM   #3
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
sorry if I was not clear but you understood correctly.
 
Old 08-22-2008, 03:21 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
In what form are the statements? As in, are they text strings or are they already loaded into the program as object trees?
ta0kira
 
Old 08-22-2008, 03:50 PM   #5
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by manolakis View Post
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.
 
Old 08-22-2008, 05:54 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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:
  1. Everything is either a statement or an operator
  2. An operator can turn one or more statements into a larger statement
  3. 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

Last edited by ta0kira; 08-22-2008 at 05:56 PM.
 
Old 08-23-2008, 10:44 AM   #7
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by ta0kira View Post
In what form are the statements? As in, are they text strings or are they already loaded into the program as object trees?
ta0kira
Yes, they are actually passed as an input string like the one mentioned before:

Quote:
((A AND B) OR C)
 
Old 08-23-2008, 04:33 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Got it. My most recent previous post overrides that question, so let me know what you think.
ta0kira
 
Old 08-24-2008, 10:09 AM   #9
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
ta0kira,

thanks for your help. I found also a really good
page which contains the whole algorithm for rpn conversion.
 
Old 08-25-2008, 07:45 AM   #10
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
This link is busted.
Quote:
Originally Posted by manolakis View Post
You wanted
Quote:
Originally Posted by manolakis View Post
 
  


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
Dang! I used Partition Magic to convert hda5 from Primary to Logical, nothing boots.. brjoon1021 Linux - General 3 11-06-2007 09:03 PM
cant convert logical partition winfinit Slackware 11 08-19-2007 09:06 AM
How to convert primary partition to logical leftPeg Linux - Hardware 1 04-27-2006 09:48 PM
regular expression in lex's notation browneyes Programming 1 10-04-2004 04:43 PM
Polish characters missing in Java apps zgolus Linux - Software 0 12-03-2003 04:23 AM

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

All times are GMT -5. The time now is 03:55 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