precompiler for modifications on c/c++ source code?

hey guys!
i have the following problem where i can't find a suitable solution for.
i'd like to write a small precompiler that knows basic c++ language components like (for, while, do while, switch and if-else if-else). what i try to do is...

for example i make an annotiation in a c++ source code just before a for-loop (just an example) like this:

//@instrument=for
for(int i=0; i<10; i++)
{
printf("just an example");

}

now i wanna run my precompiler with the name of the source code file as argument. the precompiler shall read the source code file and make the following modifications to it:

count();
startTime();
for(int i=0; i<10; i++)
{
countEach();
startTimeEach();
printf("just an example");
stopTimeEach();
}
stopTime();

The inserted function calls are just examples. The precompiler knows the right function calls from a config file.

but how can i get sure that the modifations are made at the right place.
i don't wanna build a huge parser with syntay trees and all the stuff, cause the i have to implement the whole c/c++ syntax and grammar.

i'm only interested in looking for annotations, check if the following statement is accepted and then, finally, make the modifations to it.

can anyone there give me a hint where to start?

i read the manual of lex/flex and i found some interesting things like searching for symbols with regular expressions and performing special actions when a symbol is found. but thats not enough...

any ideas?
[1651 byte] By [EricCartman] at [2007-11-20 11:46:17]
# 1 Re: precompiler for modifications on c/c++ source code?
lex is only a regular expression parser--not enough to handle your problem. But it can be part of the solution.

I suggest you read up on yacc as well. Together with lex, you can make a fairly powerful parser without much trouble.
Lindley at 2007-11-9 1:26:14 >
# 2 Re: precompiler for modifications on c/c++ source code?
Conceptually this is what (albeit advanced/high intermediate) templates do.

Possibly one of the chain of responsibility or strategy patterns implemented using templates, perhaps a series of commands, etc.

It might seem that convoluted macros might work, but I sense the flexibility you want might not fit well, and macros are an old C style of thinking.

What you have here is a kind of text processing solution, but templates do this in generic ways that, once you get used to the idea, are much more powerful and flexible.

Is there a reason you'd reject templates and design patterns?
JVene at 2007-11-9 1:27:14 >