【问题标题】:parsing or reading a cpp file using python and adding data by ignoring cpp file comments使用python解析或读取cpp文件并通过忽略cpp文件注释来添加数据
【发布时间】:2017-03-31 12:39:40
【问题描述】:

我有一个 cpp 文件,我正在读取该文件并尝试使用 python 脚本将行/行添加到该 cpp 文件中。我遇到了一个问题 - 因为它正在读取单行 cmets 和多行 cmets 也因此产生了问题,因为如果我在某个字符串之后/之前编写,有时它也会被添加到 cmets 部分中,这根本不应该发生。

有人可以指导我在使用 python 解析 cpp 文件时如何忽略 cpp 样式 {NOT REMOVE} 吗?

例如,我正在从后面读取文件并尝试添加some text,一旦找到两个}(右大括号),它就会添加some text

例如:

namespace A {
    namespace B {
        class C { 

        };
    <Want to add some data here>
    }
}

但如果我有一些注释代码:

namespace A {
    namespace B {
        class C { 

        };
    <Want to add some data here>
    }
}

如果我有这样的 cmets:

namespace A {
    namespace B {
        class C { 

        };
    <Want to add some data here>
    }
}
// }
/*
int fun() {
}
*/

从背面读取两个}后,如何忽略这些cmets并添加some text

注意 - 你也可以帮助我获得一些不同示例的代码。 请让我知道以获取更多信息。

【问题讨论】:

  • 要正确执行此操作,您确实需要一个 C++ 解析器。您可以通过gcc-xml 运行您的文件并使用 XML 模块来处理它。
  • 感谢稻谷的回复,但实际上我没有得到。我可以要求你用任何例子让我理解吗?请
  • 也许你应该解释一下为什么你需要用 Python 来做这件事,以及你使用它的确切目的......因为可能有很多你没有想到的问题的解决方案。
  • 我想用 python 来做这件事,因为我的整个工具将用 python 编写,并且部分已经开发好了。这是我被严重卡住的地方之一。如果有解决方案请帮忙
  • 这是我用于 // 注释类型的代码 cpat = re.compile("//.*") print re.findall(cpat,t) 和这个 /* / style 我正在使用这条评论 = re.compile(r'/*(.?)*/', re.DOTALL) print re.findall(comment,t)

标签: python c++ python-2.7 python-3.x parsing


【解决方案1】:
import re,pdb
cpat = re.compile('}')
f = open('3.txt','w')
brace = 0
newragex = re.compile(r'/\*.*\*/')
#pdb.set_trace()

for line in reversed(open("xvz.cpp").readlines()):
#       if '/*' and '*/' in line.rstrip():
        if re.search(newragex,line):
#               print "Line is - ", line.strip()
                temp = line.split('/*')
#               print "Temp is - ",temp
#               print "Temp[0] is - ",temp[0]
                if temp[0] == None or temp[0] == ' ' or temp[0] == '':
                        print "Inside Single Line comment - and No Extra element is found in this line"
                        f.write(line.rstrip() + '\n')
                        continue
                else:
                        print "INside Sinle Line comment - but There is extra non comment portion so checking pattern matching\n"
                        if re.search(cpat,line):
                                print "Pattern matched - increasing brace value\n"
                                brace = brace + 1
                                print "Brace Found - Value of Brace now - ", brace
                                f.write(line.rstrip() + '\n')
#                               if brace == 2:
#                                       print "Two braces found"
 #                                      f.write('Testing add\n')
                                print line.rstrip()
#                print "Single Line commment Found"
                continue
        if '*/' in line.rstrip():
                count = count + 1
                f.write(line.rstrip() + '\n')
                continue
        if '/*' in line.rstrip():
                count = count - 1
                f.write(line.rstrip() + '\n')
                continue
        if '//' in line.rstrip():
                temp = line.split('//')
                if temp[0] == None or temp[0] == ' ':
                        f.write(line.rstrip() + '\n')
                        continue
                else:
                        if re.search(cpat,line):
                                print "Brace Value Before matching - ",brace
                                brace = brace + 1
                                print "Brace Found - Value of Brace now - ", brace
                                f.write(line.rstrip() + '\n')
                                if brace == 2:
                                        print "Two braces found"
                                        f.write('Testing add\n')
#                               print line.rstrip()
                continue
        if count == 0:
if re.search(cpat,line):
                        brace = brace + 1
                        print "Brace Found - Value of Brace now - ", brace
                        f.write(line.rstrip() + '\n')
                        print "Brace Value now -", brace
                        if brace == 2:
                                print "Two braces found"
                                f.write('Testing add\n')


#       if brace == 2:
#               print "Two braces found"
#               f.write('Testing add\n')

        else:
                pass

        f.write(line.rstrip() + '\n')

【讨论】:

  • regex 不能可靠地为此工作,但您似乎没有在听。此外,您正在按行处理;您如何处理多线 /* ... */ 构造? /* xx /* yy */ 呢?如果 /* xx */ } /* yy */ 怎么办?如果你有一个预处理指令序列#if cond newline x } newline #else y } newline #endif 怎么办?你需要一个解析器来做这件事。
  • 我完全同意正则表达式不会可靠地工作,但上面的代码涵盖了一些领域,并且我可以像你提到的那样思考和添加更多场景 /* xx / } / i> y */?和别的 ?您对此有何要求,关于解析器,我知道python中没有这样的库可以帮助我进行解析,那么如何获取该解析器?我必须编写类似我自己的编译器的东西吗?只是想知道请帮忙。
  • 您不想为 C++ 编写自己的编译器;如果您拥有出色的工具,那将需要 10 年(根据经验)。不知道你为什么坚持使用 Python,但如果你坚持的话,还有 Clang 及其 Python 接口库(不知道名字)。我们的 DMS Software Reengineering Toolkit 及其 C++ 前端;使用 DMS,您可以编写一个转换来完成您的效果并处理所有情况(以及我描述的更多情况)。
猜你喜欢
  • 2015-12-22
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
  • 2022-06-29
  • 1970-01-01
  • 1970-01-01
  • 2012-01-04
  • 2019-06-23
相关资源
最近更新 更多