General Discussion > Computer Programming
Searching Operator Precedence in C
Halifax:
Alright how would you search for operator precedence when programming in C. And how would you do it effciently. I have been raking my brain looking for and idea but I cannot find a speed effcient way to do it. Does anyone have any ideas I am checking for these operators.
Level 1:
(
Level 2:
!, ~
Level 3:
*, /, %
Level 4:
+, -
Level 5:
Level 6:
=
Level 7:
==, !=
Level 8:
&
Level 9:
^
Level 10:
|
Level 11:
&&
Level 12:
||
JincS:
http://www.difranco.net/cop2220/op-prec.htm
Good luck!
**EDIT**
Ok, I just re-read what you said: do you mean you want to check a C file and figure out which operators come first in that? If so, you would have to read the C file into a buffer, tokenize it, and then run the tokens through an algorithm to find out which comes first. No really fast way to do it though, sadly.
Example:
c1-->CODE ec1(i>=0)&&(i= (operator)
3: 0 (number)
4: ) (operator)
5: && (operator)
6: ( (operator)
7: i (variable)
8: < (operator)
9: 10 (number)
10: ) (operator)
Then, if you leave everything except the operators in the order they appear, this code snippet would be executed in the following order (just using token numbers here):
0,1,2,3,4,6,7,8,9,10,5
Like this:
Do this: i>=0 (relational logic)
Then this i
Halifax:
Lol funny you say that but no GCC handles the parsing in that thank god. I am just making a simple script compiler for a language of my own for two reasons. First to just get experience with the whole compiler process. And two because I need practice for this AP course I am taking next year. I have really hit a stand still in z8-GCC with the object file parsing. I am still working on 'ld'. Thanks though JincS that is a good way to parse it.
Halifax:
I actually have seemed to have found a way that is more efficient than tokenizing that I would have never thought of. After reading up on the internals of how GCC parses I have found that it uses abstract syntax trees to parse data efficiently. Of course it is hard to implement, but once implemented I think it would be much easier to handle a lot of the parsing.
JincS:
Sounds fun. Where can I find this info? It sounds a lot easier than using a million slow "if(token==operator)" statements...
**EDIT**
nvm, found it (and it was a stupid question->everyone knows the answer is Google!).
Navigation
[0] Message Index
[#] Next page
Go to full version