Programming Trick: extracting source-embedded comments with Python

I have written a simple-minded script (comments.py, how do I link this?) to convert a C/C++ file into LaTeX source. Most of the code goes into alltt blocks: alltt is an alternative to verbatim that preserves the active character of "\", "{", and "}" (so the Python script must escape these when they appear in the code). However, comments embedded in "/** … */" or prefixed by "///" are turned into standard LaTeX text (respectively, by closing and reopening the alltt block, or by using \mbox{…}).

Almost no checks are made on the consistency of the comment delimiters. The re module of Python provides nice regexp commands (such as re.sub(r'regexp',r'replacement',string), where the r'…' raw string prevents Python from interpreting backslashes as character escapes).

Nov 24, 2003

comments (/progtricks)
2003-11-24, 09:00 [edit]


  Login:   Password:   [rss] [cc] [w3] [css]

© M. Vallisneri 2012 — last modified on 2010/01/29

Tantum in modicis, quantum in maximis