0

I have source files which contain preprocessor conditional statements. Looking like this:

#if A > 0 && (B < 1 || M == 0)
   #if U == 0
     do_something()
   #endif
   do_more()
#elif A == 0
  do_somethingelse()
#else
  print 'asd';
#endif

#else and #elif-statements are optional. Statements can be nested. I want to match all the blocks, then evaluate the conditions (using eval or something) and then keep the relevant blocks.

I know it can be done using unidef, sunidef, coan - but I'm not allowed to use any of those. I want to avoid using gcc, as it does MUCH more then ONLY evaluating the conditional statements.

I read a lot on SO, it's said that using RegExp is a bad idea with nested statements. So I'm looking for a Python Parser and found quite many, like PyParsing.

Has anyone of you achieved something like that already? I mean not only parsing nested structures and returning the contents of the blocks, but also replacing them in the original string when some condition is met.

Thanks a lot in advance!

tim
  • 9,896
  • 20
  • 81
  • 137
  • Add a comment if you vote to close the question. I think I went into detail enough - so don't be a troll! – tim Jun 14 '16 at 14:30
  • 1
    I think pyparsing *could* work for you, with some work, see this posted example: http://pyparsing.wikispaces.com/file/view/macroExpander.py/30529621/macroExpander.py – PaulMcG Jun 14 '16 at 15:47
  • That's still *only* a parsing within one line, so the big step is still to get it to work to match whole blocks and further to replace them based on the condition-evaluation. If I look at that example for such a simple macro replacement, maybe it is easier to write my own small parser going through the file line by line and remember which brackets were opened and closed with which condition :-(( – tim Jun 14 '16 at 16:30

0 Answers0