Tokenizing an expression, strtok or other alternatives?
This is my first post on this forum, hopefully It'll be according to the posting rules.
For the small (school) project I'm currently working on I am trying to implement an infix-to-postfix translator (Reverse Polish Notation: RPN). T
he reason behind this is that I want to be able to execute expressions on a CUDA-capable GPU (CUDA) which doesn't have support for recursion (so no expression trees etc).
So for example I have an expression like: 3 + 4 * 2 / (1 - 5) ^ 2 ^ 3 and I want to translate that to the postfix notation like: 3 4 2 * 1 5 - 2 3 ^ ^ / +.
The expression will come from a different program and these will include variable names as well, so something like 3 + (2 / x) ^ y ^ z but that is not what I am having problems with now (or, not yet..).
Currently my program can deal with expressions like the ones above, but only because each element in the string literal is a token, so I can walk through the string doing something on each element. However, this does not work when values are floats or negative values. The expression 3.14 / -.2 + (2.5 * x) needs to be split into tokens in a whole different way.
I am not experienced enough to find a method that can split such an expression in the way I would like, namely: '3.14', '/', '-.2', '+', '(', '2.5', '*', 'x', ')'.
This data would then be stored in a struct holding both values and operators. I thought about using a simple Perl regex, but since I plan on distributing the program (later on) I don't feel much about asking people to also install Perl just to run it.
I tried using strtok() for this, but since I can't just split on, say, spaces since I can't be sure that all elements are separated by spaces, especially when there are brackets in the expression. My question is really more about a direction I should go instead of complete working code examples, but if someone knows about an existing infix->postfix translator in plain C (couldn't find one!) that would be very great indeed!
Thanks for your time,