Page 1 of 1
Reverse Polish Notation (RPN)
Posted: Sun Jun 26, 2005 9:24 pm
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
Posted: Sun Jun 26, 2005 9:37 pm
by utopiomania
You should use a recursive descent parser to solve expressions, not RPN.

Re: Reverse Polish Notation (RPN)
Posted: Mon Jun 27, 2005 7:07 am
by traumatic
Re: Reverse Polish Notation (RPN)
Posted: Mon Jun 27, 2005 9:10 am
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 ?!
Posted: Mon Jun 27, 2005 10:08 am
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.
Posted: Mon Jun 27, 2005 5:48 pm
by Killswitch
Thanks for the replys, I'll google a good translator and bung that german post in.
RPN can go further than just equations
Posted: Mon Jun 27, 2005 6:34 pm
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.
Posted: Mon Jun 27, 2005 8:36 pm
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.
Posted: Wed Jun 29, 2005 1:16 pm
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.
Posted: Wed Jun 29, 2005 5:43 pm
by utopiomania
Check out 'variables, types and operators' in PureBasic Help/Reference Manual.

Posted: Wed Jun 29, 2005 5:56 pm
by Killswitch
Don't I feel stupid!

Thanks alot!