2016-11-17 189 views
1

我有一个cpp文件,并且正在读取该文件并尝试使用python脚本将行/行添加到该cpp文件中。我遇到了一个问题 - 因为它正在阅读单行注释和多行注释,所以造成问题,因为如果我在某些字符串之后/之前写入,有时它也会被添加到注释部分中,这根本不应该发生。使用python解析或读取cpp文件并通过忽略cpp文件添加数据注释

有人可以请指导如何在使用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> 
    } 
} 

如果我有这样的评论:

namespace A { 
    namespace B { 
     class C { 

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

我怎能无视这些意见,并在从背面读取两个}后添加some text

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

+0

要做到这一点,你真的需要一个C++解析器。你可以通过[gcc-xml](http://gccxml.github.io/HTML/Index.html)运行你的文件并使用XML模块来处理它。 – paddy

+0

感谢您的回应稻谷,但我没有得到实际。我可以请求你让我理解任何一个例子吗?请 – user2564083

+0

也许你应该解释为什么你需要用Python来做这件事,以及你正在使用它的原因......因为可能有很多解决方案是你没有想到的问题。 – paddy

回答

-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') 
+0

正则表达式将无法可靠地为此工作,但你似乎不听。此外,您正在按行处理;你怎么处理多行/ * ... **/*构造?那么/ * xx/* yy **/*?如果/ * xx **/*}/* yy **/*会怎么样?如果你有一个预处理器指令序列#if cond newline x} newline #else y} newline #endif?你需要一个解析器来做到这一点。 –

+0

我完全同意正则表达式不能可靠地工作,但是上面的代码覆盖了很少的区域,并且我可以用相同的方式思考并添加更多像上面提到的方案/ * xx * /}/* y * /?和别的 ?你在说什么,关于解析器我知道在python中没有这样的库可以帮助我解析,所以如何获得解析器?我是否必须编写类似我自己的编译器的东西?只是好奇知道请帮助。 – user2564083

+0

你不想为C++编写自己的编译器;如果你有很棒的工具,那需要10年(从经验中)。不知道你为什么坚持使用Python,但是如果你坚持的话,Clang的Python接口库(不知道名字)。我们的DMS Software Reengineering Toolkit具有C++前端;使用DMS,您可以编写一个转换,实现您的效果并处理所有情况(以及我所描述的更多内容)。 –