2016-11-17 34 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,您可以編寫一個轉換,實現您的效果並處理所有情況(以及我所描述的更多內容)。 –