Reverse Polish Notation (RPN)
-
- Enthusiast
- Posts: 731
- Joined: Wed Apr 21, 2004 7:12 pm
Reverse Polish Notation (RPN)
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
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~
- utopiomania
- Addict
- Posts: 1655
- Joined: Tue May 10, 2005 10:00 pm
- Location: Norway
Re: Reverse Polish Notation (RPN)
Maybe this helps:
http://forums.purebasic.com/german/viewtopic.php?t=3266
http://forums.purebasic.com/german/viewtopic.php?t=3266
Good programmers don't comment their code. It was hard to write, should be hard to read.
-
- Enthusiast
- Posts: 537
- Joined: Wed Oct 29, 2003 10:35 am
Re: Reverse Polish Notation (RPN)
Looks good..... but hard to understand the germantraumatic wrote:Maybe this helps:
http://forums.purebasic.com/german/viewtopic.php?t=3266

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 !
- utopiomania
- Addict
- Posts: 1655
- Joined: Tue May 10, 2005 10:00 pm
- Location: Norway
-
- Enthusiast
- Posts: 731
- Joined: Wed Apr 21, 2004 7:12 pm
RPN can go further than just equations
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.
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.
-
- Enthusiast
- Posts: 731
- Joined: Wed Apr 21, 2004 7:12 pm
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~
-
- Enthusiast
- Posts: 731
- Joined: Wed Apr 21, 2004 7:12 pm
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'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~
- utopiomania
- Addict
- Posts: 1655
- Joined: Tue May 10, 2005 10:00 pm
- Location: Norway
-
- Enthusiast
- Posts: 731
- Joined: Wed Apr 21, 2004 7:12 pm