code_junkie

Python snippet to remove C and C++ comments

Nov 11th, 2011
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. import subprocess
  2. from cStringIO import StringIO
  3.  
  4. input = StringIO(source_code) # source_code is a string with the source code.
  5. output = StringIO()
  6.  
  7. process = subprocess.Popen(['sed', '/path/to/remccoms3.sed'],
  8. input=input, output=output)
  9. return_code = process.wait()
  10.  
  11. stripped_code = output.getvalue()
  12.  
  13. //.*?n|/*.*?*/
  14.  
  15. def stripcomments(text):
  16. return re.sub('//.*?n|/*.*?*/', '', text, re.S)
  17.  
  18. //.*?(rn?|n)|/*.*?*/
  19.  
  20. def comment_remover(text):
  21. def replacer(match):
  22. s = match.group(0)
  23. if s.startswith('/'):
  24. return ""
  25. else:
  26. return s
  27. pattern = re.compile(
  28. r'//.*?$|/*.*?*/|'(?:\.|[^\'])*'|"(?:\.|[^\"])*"',
  29. re.DOTALL | re.MULTILINE
  30. )
  31. return re.sub(pattern, replacer, text)
  32.  
  33. " */ /* SCC has been trained to know about strings /* */ */"!
  34. ""Double quotes embedded in strings, \" too'!"
  35. "And
  36. newlines in them"
  37.  
  38. "And escaped double quotes at the end of a string""
  39.  
  40. aa '\
  41. n' OK
  42. aa """
  43. aa "
  44. n"
  45.  
  46. This is followed by C++/C99 comment number 1.
  47. // C++/C99 comment with
  48. continuation character
  49. on three source lines (this should not be seen with the -C fla
  50. The C++/C99 comment number 1 has finished.
  51.  
  52. This is followed by C++/C99 comment number 2.
  53. /
  54. /
  55. C++/C99 comment (this should not be seen with the -C flag)
  56. The C++/C99 comment number 2 has finished.
  57.  
  58. This is followed by regular C comment number 1.
  59. /
  60. *
  61. Regular
  62. comment
  63. *
  64. /
  65. The regular C comment number 1 has finished.
  66.  
  67. /
  68. / This is not a C++/C99 comment!
  69.  
  70. This is followed by C++/C99 comment number 3.
  71. /
  72.  
  73.  
  74. / But this is a C++/C99 comment!
  75. The C++/C99 comment number 3 has finished.
  76.  
  77. /
  78. * This is not a C or C++ comment!
  79.  
  80. This is followed by regular C comment number 2.
  81. /
  82. */ This is a regular C comment *
  83. but this is just a routine continuation *
  84. and that was not the end either - but this is *
  85.  
  86. /
  87. The regular C comment number 2 has finished.
  88.  
  89. This is followed by regular C comment number 3.
  90. /
  91.  
  92.  
  93.  
  94. * C comment */
  95.  
  96. cpp -fpreprocessed foo.c
  97.  
  98. #!/usr/bin/python
  99. """
  100. A simple script to remove block comments of the form /** */ from files
  101. Use example: ./strip_comments.py *.java
  102. Author: holdtotherod
  103. Created: 3/6/11
  104. """
  105. import sys
  106. import fileinput
  107.  
  108. for file in sys.argv[1:]:
  109. inBlockComment = False
  110. for line in fileinput.input(file, inplace = 1):
  111. if "/**" in line:
  112. inBlockComment = True
  113. if inBlockComment and "*/" in line:
  114. inBlockComment = False
  115. # If the */ isn't last, remove through the */
  116. if line.find("*/") != len(line) - 3:
  117. line = line[line.find("*/")+2:]
  118. else:
  119. continue
  120. if inBlockComment:
  121. continue
  122. sys.stdout.write(line)
Add Comment
Please, Sign In to add comment