简体   繁体   English

读取文件时避免使用C样式注释

[英]Avoid C style comments while reading a file

I am parsing a C file for LOC in a function using python. 我在使用python的函数中解析LOC的C文件。 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. 我只想跳过注释中出现的所有“ {”。

eg 例如

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. 什么是实现这一目标的最佳方法。

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: 使用原始代码段作为src,将输出:

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. 您可以在内存中完成所有这些操作,因此不必首先创建无注释文件。

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. 但是例如,您可以在字符串文字中使用双斜杠,等等。

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). 如果您有gcc,则可以将gcc -E input_file用作预处理器,以剥离注释(但还会扩展宏-可能会更改LOC)。 For your example the output would be: 对于您的示例,输出为:

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


 int func(
         int i,
         float f,
         char c

         )
{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM