Parsing C Preprocessor files

Bram Stolk bram at nospam.sara.nl
Wed Jun 23 10:47:51 EDT 2004


pyHi(),

I would like to thank the people who responded on my question about
preprocessor parsing. However, I think I will just roll my own, as I
found out that it takes a mere 16 lines of code to create a #ifdef tree.

I simply used a combination of lists and tuples. A tuple denotes a #if
block (startline,body,endline). A body is a list of lines/tuples.

This will parse the following text:

Top level line
#if foo
on foo level
#if bar
on bar level
#endif
#endif
#ifdef bla
on bla level
#ifdef q
q
#endif
#if r
r
#endif
#endif

into:

['Top level line\n', ('#if foo\n', ['on foo level\n', ('#if bar\n', ['on bar level\n'], '#endif\n')], '#endif\n'), ('#ifdef bla\n', ['on bla level\n', ('#ifdef q\n', ['q\n'], '#endif\n'), ('#if r\n', ['r\n'], '#endif\n')], '#endif\n')]

Which is very suitable for me.

Code is:

def parse_block(lines) :
  retval = []
  while lines :
    line = lines.pop(0)
    if line.find("#if") != -1 :
      headline = line
      b=parse_block(lines)
      endline = lines.pop(0)
      retval.append( (headline, b, endline) )
    else :
      if line.find("#endif") != -1 :
        lines.insert(0, line)
        return retval
      else :
        retval.append(line)
  return retval

And pretty pretting with indentation is easy:

def traverse_block(block, indent) :
  while block:
    i = block.pop(0)
    if type(i) == type((1,2,3)) :
      print indent*"\t"+i[0],
      traverse_block(i[1], indent+1)
      print indent*"\t"+i[2],
    else :
      print indent*"\t"+i,

I think extending it with '#else' is trivial. Handling includes and 
expressions is much harder ofcourse, but not immediately req'd for me.

  Bram

On Wed, 23 Jun 2004 14:01:51 +0200
Bram Stolk <bram at nospam.sara.nl> wrote:

> Hi there,
> 
> What could I use to parse CPP macros in Python?
> I tried the Parnassus Vaults, and python lib docs, but could not
> find a suitable module.
> 

-- 
------------------------------------------------------------------------------
 Bram Stolk, VR Engineer.
 SARA Academic Computing Services Amsterdam, PO Box 94613, 1090 GP  AMSTERDAM
 email: bram at nospam.sara.nl   Phone +31-20-5923059  Fax +31-20-6683167

"Software is math. Math is not patentable."
OR
"Software is literature. Literature is not patentable." -- slashdot comment
------------------------------------------------------------------------------



More information about the Python-list mailing list