Here is a solution using pyparsing instead of re's. You're already used to
re's from using Perl, so you may be more comfortable using that tool in
Python as well. But pyparsing has some builtin features for pattern
matching, calling out to callback routines during parsing, and a lineno
function to report the current line number, all wrapped up in a simple
transformString method call.

# setup a parse action that will insert line numbers
# parse actions are all called with 3 args:
# - the original string being parsed
# - the current parse location where the match occurred
# - the matching tokens
# if a value is returned from this function, transformString will
# insert it in place of the original content
def insertPrintStatement(st,loc,toks):
lineNumber = lineno(loc,st)
if toks[0]:
return r'print("%d\n"), %s' % (lineNumber,toks[0])
else:
return r'print("%d\n")' % lineNumber
forInitializer.setParseAction(insertPrintStatement)

# transform some code
# this is how you would read in a whole file as a single string
#testdata = file(inputfilename).read()
# to read the entire file into a list of strings, do:
#testdata = file(inputfilename).readlines()
# for now, just fake some source code
testData = """
for(i = 0; i <= 100; ++i)
{
/* some stuff */
}

for (;;
{
/* do this forever */
}

/* this for has been commented out
for(a = -1; a < 0; a++)
*/

"""

# use the grammar and the associated parse action to
# transform the source code
print forStmt.transformString(testData)

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!