Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess
- from cStringIO import StringIO
- input = StringIO(source_code) # source_code is a string with the source code.
- output = StringIO()
- process = subprocess.Popen(['sed', '/path/to/remccoms3.sed'],
- input=input, output=output)
- return_code = process.wait()
- stripped_code = output.getvalue()
- //.*?n|/*.*?*/
- def stripcomments(text):
- return re.sub('//.*?n|/*.*?*/', '', text, re.S)
- //.*?(rn?|n)|/*.*?*/
- def comment_remover(text):
- def replacer(match):
- s = match.group(0)
- if s.startswith('/'):
- return ""
- else:
- return s
- pattern = re.compile(
- r'//.*?$|/*.*?*/|'(?:\.|[^\'])*'|"(?:\.|[^\"])*"',
- re.DOTALL | re.MULTILINE
- )
- return re.sub(pattern, replacer, text)
- " */ /* SCC has been trained to know about strings /* */ */"!
- ""Double quotes embedded in strings, \" too'!"
- "And
- newlines in them"
- "And escaped double quotes at the end of a string""
- aa '\
- n' OK
- aa """
- aa "
- n"
- This is followed by C++/C99 comment number 1.
- // C++/C99 comment with
- continuation character
- on three source lines (this should not be seen with the -C fla
- The C++/C99 comment number 1 has finished.
- This is followed by C++/C99 comment number 2.
- /
- /
- C++/C99 comment (this should not be seen with the -C flag)
- The C++/C99 comment number 2 has finished.
- This is followed by regular C comment number 1.
- /
- *
- Regular
- comment
- *
- /
- The regular C comment number 1 has finished.
- /
- / This is not a C++/C99 comment!
- This is followed by C++/C99 comment number 3.
- /
- / But this is a C++/C99 comment!
- The C++/C99 comment number 3 has finished.
- /
- * This is not a C or C++ comment!
- This is followed by regular C comment number 2.
- /
- */ This is a regular C comment *
- but this is just a routine continuation *
- and that was not the end either - but this is *
- /
- The regular C comment number 2 has finished.
- This is followed by regular C comment number 3.
- /
- * C comment */
- cpp -fpreprocessed foo.c
- #!/usr/bin/python
- """
- A simple script to remove block comments of the form /** */ from files
- Use example: ./strip_comments.py *.java
- Author: holdtotherod
- Created: 3/6/11
- """
- import sys
- import fileinput
- for file in sys.argv[1:]:
- inBlockComment = False
- for line in fileinput.input(file, inplace = 1):
- if "/**" in line:
- inBlockComment = True
- if inBlockComment and "*/" in line:
- inBlockComment = False
- # If the */ isn't last, remove through the */
- if line.find("*/") != len(line) - 3:
- line = line[line.find("*/")+2:]
- else:
- continue
- if inBlockComment:
- continue
- sys.stdout.write(line)
Add Comment
Please, Sign In to add comment