News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Algebraic expression

Started by ToutEnMasm, October 11, 2007, 03:06:51 PM

Previous topic - Next topic

ToutEnMasm

Hello,
I am searching something that can translate Algebraic expression into source code.
Does this thing exist ?

realcr

Hey ToutEnMasm.

You are infact looking for information about compiler construction.
I recommend you to read this article :
http://compilers.iecc.com/crenshaw/
It might not be masm32 ,however I think it was one of the best article of this kind.

good luck,
realcr.

ToutEnMasm

Hello,
Thanks for answer.
I see that my question isn't very good.I will try a sample in pseudo code.
Quote
.data
X DWORD 5
Y REAL 4.0
z dword 0
Formula db "z=X² + Y²",0
That's not very difficult to do it with the FPU (just do it ...).What I am searching is something that can translate this in masm code.This thing seems possible,no?


Rockoon

Convert your infix expression to RPN .. after that it damn near codes itself.

When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

ToutEnMasm

Hello,
Thanks for answer.
I see somethings easy to do using re-entrance ( a prog that call itself) and tempory data in stack.
Perhaps if I have sometimes i do it myself.

jack

I looked for the same thing, does anybody know of an algorithm to minimize stack usage in RPN ?
let's say you translate an algebraic expression to postfix (RPN), how would you rewrite it so as to use the minimum of stack registers ?

tenkey

For minimizing the parser stack usage, if you don't have function calls (for example, sin(x) or exp(x)), then you can use "operator precedence".
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

Rockoon

Quote from: tenkey on October 21, 2007, 04:21:52 PM
For minimizing the parser stack usage, if you don't have function calls (for example, sin(x) or exp(x)), then you can use "operator precedence".

RPN doesnt have "operator precedence" .. its one of the main points of using RPN

RPN expressions are ALWAYS evaluated left to right

The "worst case" for an RPN expression as far as stack usage is when all the terms precede all the operators ..

Examples:

a b c d e + + + +

which has a stack usage depth of 5

a b + c + d + e +

which has a stack usage depth of 2



Both expressions perform the exact same calculation .. as far as automatically performing such minimization .. not sure what a good technique would be

Something to consider however is that something like:

a b + c d + e + +

which while it has a stack usage depth of 3, has greater parallelism than the others so should lend itself well to generating code with better instruction level parallelism

edited to add:

It should be noted that "stack" is just an abstraction which can easily be implimented as register management (for instance ST0 = eax, ST1 = ebx, ST2 = ecx, ...)  hence the desire to minimize "stack" usage could mean minimizing register usage which means minimizing auxilary storage outside of registers (such as on the real processor stack)
When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.

tenkey

RPN doesn't use operator precedence, but a parser that converts from infix to RPN can. It wasn't clear at first read what was to be rewritten - the parser or the RPN evaluator.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

Rockoon

For register/stack minimization, there is the Sethi-Ullman algorithm
When C++ compilers can be coerced to emit rcl and rcr, I *might* consider using one.