From: | "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> |
Newsgroups: | comp.compilers |
Date: | 13 Nov 2006 16:29:25 -0500 |
Organization: | cbb software GmbH |
References: | 06-11-052 |
Keywords: | interpreter, performance |
Posted-Date: | 13 Nov 2006 16:29:25 EST |
On 11 Nov 2006 15:50:04 -0500, PAolo wrote:
> Given the RPN expression
> 1 2 + 3 *
> in the two stack forms is (the left of the string is the top of the
> stack):
>|1 2 3| |+ *|
Of course you can split the stack into operations and operands ones. I am
doing this all the time.
> which can also mean RPN
> 1 2 3 + *
It cannot. That were
|2 3 1| |+ *|
> Question 2: Is always possible to rewrite the expression in the form I
> said? How could I do it automatically?
You need commutated complement operations for that. Consider:
1 2 3 + /
You cannot rewrite it just as
|2 3 1| |+ /|
You have to as
|2 3 1| |+ \|
where a\b = b/a
Each time you swap operands in the stack, you have to change the operation
as well.
Alternatively you can change the operation to inverse complement and mark
an operand using commutative inverse:
|2 3 1| |+ 1/x *|
here / --> 1/x *
I am using this technique to deal with expressions a+b-c+d, which is parsed
as +(a,b,-c,d). "+" takes four operands. See
http://www.dmitry-kazakov.de/ada/components.htm#Parsers_etc
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
Return to the
comp.compilers page.
Search the
comp.compilers archives again.