Reverse Polish Notation (RPN)

Everything else that doesn't fall into one of the other PB categories.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Reverse Polish Notation (RPN)

Post by Killswitch »

Hey,

As some of you may know a friend and I are working on our own BASIC variant, which we are developing in PureBasic - we should have something to show pretty soon!

I've been discussing this elsewhere and I've been pointed to RPN to solve expressions, unfortunatly I really can't seem to code an example which works well.

Could someone please help?

Edit:

To be more specific I am having trouble converting an infix expression to a postfix (RPN expression). While this is easy with simple expressions, such as:

1+2 becomming 12+

Its quite a lot harder with 1+2*3-6
Last edited by Killswitch on Sun Jun 26, 2005 10:50 pm, edited 1 time in total.
~I see one problem with your reasoning: the fact is thats not a chicken~
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

You should use a recursive descent parser to solve expressions, not RPN. :wink:
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Re: Reverse Polish Notation (RPN)

Post by traumatic »

Good programmers don't comment their code. It was hard to write, should be hard to read.
dontmailme
Enthusiast
Enthusiast
Posts: 537
Joined: Wed Oct 29, 2003 10:35 am

Re: Reverse Polish Notation (RPN)

Post by dontmailme »

Looks good..... but hard to understand the german :(

I have always though I'm missing some great posts on the other language forums......

I wish there was only the one forum, maybe the community would benefit ?!
Paid up PB User !
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

Killswitch, I have a recursive descent parser lying around which I translated from C
and added support for floats, functions and variables to it. I plan to translate it to
PureBasic as a newbie exercise when I find the time, and post it in Tips&Tricks.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

Thanks for the replys, I'll google a good translator and bung that german post in.
~I see one problem with your reasoning: the fact is thats not a chicken~
ivory
User
User
Posts: 36
Joined: Fri Jun 25, 2004 2:30 am

RPN can go further than just equations

Post by ivory »

There have been several languages built on variants of RPN (more or less) including Forth and a little language I saw once called Mouse, and perhaps even APL.

The trick is to use a stack, and to know how many operators are required by an operations.

If a parameter is a number, push it on the stack.
If a parameter is an operator, use it against the appropriate number of values popped from the stack.

1+2= becomes 1 push 2 push +(pop pop sum push) pop

RPN forces you to provide the precedence so:

1+2*3-6 becomes 1 push 2 push +(pop pop sum push) 3 push 6 push -(pop pop diff push) *(pop pop mult push) pop
or 1 2 + 3 6 - *

Languages can be built on this principle, where you push values on the stack and then call the procedure. Fundamentally, however, modern languages such as Purebasic handle all the hardwork for us allowing us to keep source code that is much more readable.
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

Thats the plan, I've managed (thanks to a lot of help) to write an infix to postfix converter, it doesn't support brackets yet but I'm working on it, with this users can have regular expressions and my interpreter can work out what they mean.
~I see one problem with your reasoning: the fact is thats not a chicken~
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

Me again :) , I've encorporated brackets into the postfix evaluation - and everything works pretty well.

I'd like to introduce operators such as =, <, >, <> and 'OR' etc, except I cannot find their priority. Could someone please tell me what their priorities is in comparison with regular operators (+, - etc ).

Thanks.
~I see one problem with your reasoning: the fact is thats not a chicken~
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

Check out 'variables, types and operators' in PureBasic Help/Reference Manual. :)
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

Don't I feel stupid! :oops: Thanks alot!
~I see one problem with your reasoning: the fact is thats not a chicken~
Post Reply