Search This Blog

Showing posts with label tokens. Show all posts
Showing posts with label tokens. Show all posts

Tuesday, April 13, 2010

Simulation: Update on the Complex Parser

This is a quick update on the software.  I was able to separate the string into separate tokens.  There are 3 types of tokens:

0 = Real Token
1 = Imaginary Token
2 = Inner Token (within parenthesis).

What will happen on a later date, this "Evaluate" subroutine will turn the real token into a complex number and the imaginary token into a complex number.  The inner token will go back through the "Evaluate" subroutine (without the parenthesis) until a complex number is returned.  This method is using recursion to evaluate a full complex string.

The next step will be to perform the math in operator precedence.  This may get a little tricky since they aren't in order when I produce the tokens.  Based on my current structure, this isn't easily done.  But now that I have a structure I can possibly work around this. Wish me luck

---------
Justin Coulston
Justin.Coulston@gmail.com

Monday, April 12, 2010

Simulation: Complex Math Parser

Introduction
In my project to build a SPICE program, I ran across a problem; I need to be able to perform complex math from strings.  This has already been done a thousand times before but finding a free, good parser isn't easy.  So I decided to do this myself.  This article will explain briefly the process I'm using to parse a complex math string.

Basic Process
  1. First check the string for incorrect characters.  This can be done while performing step 2
  2. Define every character in the string as a type (ie. 0=operator, 1=real number, 2=imaginary number, 3=left parenthesis, 4=right parenthesis).  Define this into a separate string.  The reason you do this is to take in different number types (real, imaginary, scientific notation, etc.)
  3. Next separate the string into tokens to be evaluated separately.  The most open parenthesis should be it's own token. (ie. str = "2*2j+(20+2j)^12" will turn into tokens "@1*@2+@3^@4") This is only an example.  I doubt i'll have a string like this.
  4. Evaluate each token by recursion through the same function until it outputs a complex number. (a custom class)
  5. With a complex number for the tokens you can then evaluate in precedent order of functions.
  6. Output complex number 
That's the quick and easy of it.  I have only completed steps 1 and 2 so far.  The rest will hopefully be completed in the week.  Hope this is helpful...