Thanks!
No problem mate
I was reading Bjarne's C++ book.
There was an example with this same algo.
I could understand this. Also, your code is very well organized and works.
Yea, it's a very simple method, I'm not really surprised. Basically you just create procedures to handle each layer of precedence then call through them as you grab each token until you reach the end of your input (in my case it's a CTRL-C).
But I will still try to make this in pure C.
Would it be too much work? C++ has pretty handy thing like maps.
Nah, I don't think it would be that much work. The most complex part, and the reason I used C++, will be the translation of the symbol table from use of maps. In C you would accomplish this simply by using a linked list, if you want to optimize you would add a hashing function to save your symbols as 32-bit hashes then compare the hash values. That usually speeds up things rather than having to do string comparisons. This is actually something assemblers and compilers will do because they need to be able to quickly search through the symbol table, and they also tend to store many symbols. Hashing the strings into smaller 32-bit values makes the searches faster and the memory usage less.
One other thing you will have to do yourself is implement a put_back() system. If I was you I'd probably just make my own I/O wrapper that handled it for you. Two most important ones would be getnext() and undo(). The reason for this is because at times you will read a character and find out that you might need to put it back onto the stream to be "read" again once you finish processing. An example of this is where you have an expression like
(2+pi) because there is no space between these tokens you will need to read some of them and undo() the one at the end of the token. Like in that expression we read p, then i, then ), upon seeing ) we know that we've reached the end of the identifier so we undo() the ) putting it back up to be read again later, and processes the
pi identifier. The undo() function simply has to push values onto a custom stack (or linked list) and the getnext() checks that "stack" for NULL, if it's not NULL it pops the value off of the stack. If it is NULL then it just reads from the normal input (be it a file or stdin). C++ does this for me using cin.get() and cin.putback(). Check out lines 135 and 136 to see it in action.
But like I said, if I can get some time away from school I'll throw something together for you (or look and see if I can't find something from back when I was still doing a lot of C programming). But for now, I'm going to bed (I've got classes tomorrow).