anonymous
anonymous

Reputation: 11

Avoid C style comments while reading a file

I am parsing a C file for LOC in a function using python. I am starting from first line of function definition and skipping all lines till i met first "{".

The issue is that "{" can also come as a part of comment. I just want to skip all "{" present inside comments.

e.g

100: int func(
102:         int i, // some comment { ....
103:         float f,
104:         char c /* some comment here { ...
105:                .... more comment */
106:         )
107:{

Whats the best pythonic way to acheive this.

Upvotes: 1

Views: 589

Answers (3)

PaulMcG
PaulMcG

Reputation: 63709

Here is a comment stripper that should also comprehend comment introducers within quoted strings:

from pyparsing import cppStyleComment,dblQuotedString

cppStyleComment.ignore(dblQuotedString)
src = cppStyleComment.suppress().transformString(src)

print src

With your original snippet as src, this prints:

int func(
             int i, 
             float f,
             char c 
             )
    {

You can do all this in memory, so you don't have to create a comment-less file first.

Upvotes: 7

Viliam
Viliam

Reputation: 4414

If you have gcc, you can use gcc -E input_file as a preprocessor which will strip-off comments (but also expand macros - might change LOC). For your example the output would be:

# 1 "_.c"
# 1 ""
# 1 ""
# 1 "_.c"


 int func(
         int i,
         float f,
         char c

         )
{

Upvotes: 0

Ned Batchelder
Ned Batchelder

Reputation: 375574

You're going to find that getting this right is very difficult without a real lexer and parser.

This will find the opening brace you're looking for:

f = open("myfile.c")
for l in f.readlines():
    l = l.split('//')[0]
    if '{' in l:
        break

But for example, you could have double-slashes inside string literals, etc.

Upvotes: 3

Related Questions