i am trying to parse string expressions like "1234567h+var1" and "4h*(var1+(2345h*var2))" to a hexadecimal value. in these samples, considering var1=1 and var=2, the routine should have returned eax=1234568h, and eax=11A2Ch

i have coded a couple of ascii2value() and value2ascii() functions, and also routines to return the value of variables.

my problem is parsing it in a mathematical correct way, taking in acount the ( and ), and the precedence of * and / over + and -

somebody have a idea of how parse these math strings in a efficient way?

Posted on 2002-06-03 10:44:38 by ancev
I was writing a calcaltor once and I wanted similar functionality so I wrote some routine, see attached file.

There are a couple of things however, first the equations aren't represented as text but rather by a form of byte code I suppose you'd call it. Secondly seeing as I was writing this for a calculator which would want percision over speed I used FPU TBYTEs for all numbers. Thirdly there's no support for variable in the current implementation, but I image they'd pretty simple to include.

Fell free to use this code if you want, but the method isn't too bad if you want to code your own implementation.

Basically you have to start with the most nested layer and move back towards the least nested solving as you go.

The step for solving a layer involve solving each function first, then solve the operators in the following order: {*,/},{+,-}

Finding the first equation to solve simply requires starting at the left of the equation and finding the first ")", then take the code between that and the preceeding "(".

Step 2
You search for function backwarks, ie start at the right and scan left, each time you find a function apply it to the number following it.

Moving onto the operators you search forward, ie left to right for * & /, followed by + & -. When you find one simply apply it to the number before and after it.

Can I recommend you run the Calc program, simply left click on it and it will display an equation along with the steps it took to solve it. Once you look at it you'll find its pretty simple understand the method. As for the code, its a bit of a mess, too much debuging stuff still in there. :(
Posted on 2002-06-03 12:07:19 by Eóin
Google: http://www.google.com/search?hl=en&lr=&q=parse+precedence+reduce

Use shift/reduce table to process the arguments.
Posted on 2002-06-03 12:19:03 by bitRAKE

thanks for the code and the tips. i will implement your algo, i think. my implementation is simpler: i only need handle 32b numbers, and 8 vars.

i am doing, for a softice linux clone, a more advanced parser, to be able to do things like "DISASM ecx+eax*4" and so on :)

thanks for the help

Posted on 2002-06-03 16:41:36 by ancev