Parsing C Preprocessor files

Pierre-Frédéric Caillaud peufeu at
Wed Jun 23 22:59:34 CEST 2004

	Nice and simple algorithm, but you should use an iterator to iterate over  
your lines, or else shifting your big array of lines with pop() is gonna  
be very slow.

	Instead of :

>     line = lines.pop(0)

	Try :

	lines = iter( some line array )

	Or just pass the file handle ; python will split the lines for you.

	You can replace your "while lines" with a "for" on this iterator. You'll  
need to avoid pushing data in the array (think about it)...

	also "#if" in line is prettier.

	Another way to do it is without recursion : have an array which is your  
stack, advance one level when you get a #if, go back one level at #endif ;  
no more recursion.

	Have fun !


More information about the Python-list mailing list