2011-09-22 35 views
0

我必須從c代碼中提取xml註釋。我嘗試使用perl regexp,但我無法提取註釋。誰能幫我。我的代碼如下所示。在perl中從源代碼中提取xml標籤的解析器創建?

Dima_chkTimeValidation(&dacl_ts_pumpPWMLowNoDos_str, 
         &dacl_ti_pumpPWMLowNoDos_U16, 
         ti_valid_U16, 
         ti_inval_U16, 
         (tB)(dacl_r_pumpPwmResidualFilt_S16 < r_testlimit_S16), 
         (tB)((testCond_B == TRUE) && (dosingActive_B == FALSE)), 
         TRUE); 
    /*****************************************/ 
    /*xml comments*/ 
    /****************************************/ 

<DTC> 
    <TroubleCode>1101</TroubleCode> 
    <Classification>FAULT</Classification> 
    <SelfHealing>No selfhealing</SelfHealing> 
    <WarningLamp>No Warning Lamp</WarningLamp> 
    <DirectDegradation>No Action</DirectDegradation> 
    <Order>PRIMARY</Order> 
    </DTC> 
    /*******************************/ 
    /* Dosing clogg test   */ 
    /*******************************/ 
    /* special test when run i sequence test mode SMHD_DOSVALVE_E */ 
    if ((s_seqTestCtrlStatus_E == SMHD_RUNNING_E) && (s_seqTestMainState_SMHD_DOSVALVE_E)) 
    { 
    /* Use result from DDOS test */ 
    Dima_chkValidation(&dacl_ts_pumpPWMLowDos_str, 
         (tB)(s_dosValveTest_E == SMHD_TESTFAILED_E), 
         (tB)(s_dosValveTest_E != SMHD_TESTNOTFINISHED_E)); 
    } 

如上圖我有很多的C代碼行之前和XML的意見後,但我只是張貼小C代碼,我加入了C代碼的一些意見,我需要提取的意見,因爲它是。所以任何機構都可以幫助我如何使用perl進行提取。

+0

你的「代碼」不包含任何XML! – tadmc

+0

嗨,我的源代碼是c代碼和xml註釋的組合。我的代碼有XML註釋。 – verendra

+0

XML註釋使用<! - cooment here - >分隔。您向我們展示的內容中沒有任何XML註釋。 XML中的結束標記在<之後有一個斜槓。您沒有XML結束標記,因此您沒有XML。 – tadmc

回答

0

它不是一個好主意,寫你的工作全部代碼,但我仍然這樣做,這樣你可以得到如何處理一個想法特別的問題。

在這裏,我爲您提供最簡單的方法(可能是沒有效率)

1.請您輸入數據的簡單,使您的生活更簡單。確定一個特定的模式,使用它可以識別XML的開始和結束。

 
Dima_chkTimeValidation(&dacl_ts_pumpPWMLowNoDos_str, 
         &dacl_ti_pumpPWMLowNoDos_U16, 
         ti_valid_U16, 
         ti_inval_U16, 
         (tB)(dacl_r_pumpPwmResidualFilt_S16 < r_testlimit_S16), 
         (tB)((testCond_B == TRUE) && (dosingActive_B == FALSE)), 
         TRUE); 
    /*****************************************/ 

    /*[[[ Start XML 

< DTC > 
    < TroubleCode > 1101 < /TroubleCode > 
    < Classification > FAULT < /Classification > 
    < SelfHealing > No selfhealing < /SelfHealing > 
    < WarningLamp > No Warning Lamp lt /WarningLamp > 
    < DirectDegradation > No Action < /DirectDegradation > 
    < Order > PRIMARY < /Order > 
    < /DTC > 

    End XML]]]*/ 

    /*******************************/ 


    /* special test when run i sequence test mode SMHD_DOSVALVE_E */ 
    if ((s_seqTestCtrlStatus_E == SMHD_RUNNING_E) && (s_seqTestMainState_SMHD_DOSVALVE_E)) 
    { 
    /* Use result from DDOS test */ 
    Dima_chkValidation(&dacl_ts_pumpPWMLowDos_str, 
         (tB)(s_dosValveTest_E == SMHD_TESTFAILED_E), 
         (tB)(s_dosValveTest_E != SMHD_TESTNOTFINISHED_E)); 
    } 

在這裏,你可以找出我一直檢測到XML的起點和xml 結束

2.接下來的模式,是代碼。現在我試圖用「C」方式寫出,除了正則表達式。

 
#!/usr/bin/perl 
# 
# 
open(FD,"< Code.cpp") or die "unable to open file: $!\n"; 

my $start_xml = 0 ; ## 0 indicates false condition ..i.e either XML not started or XML ended 
        ## 1 means xml has started. 

while(< FD >){ 

     chomp($_); 

     ## Handling only single Line comments 

     my $temp = $_; 

     if($temp =~ m/\[\[\[\s*start\s*xml/ig && $start_xml == 0){ ## Check if start xml pattern found 

       $start_xml = 1; 
       next;  ## equivalent to continue of C 
     } 

     if(($temp =~ m/< [a-z0-9 [email protected]]+ >.*/ig) && ($start_xml == 1)){ ## You can add additional letters that may come 
                   ## In such cases pattern matching wont be necessary as you know 
                   # you have got XML data between start and end xml pattern. But still... 
                   # some case you might need it 

       print "$temp\n"; ## I am printing it out , but you may write it to file 

     }elsif($temp =~ m/end\s*xml\s*\]\]\]/ig){ 

       $start_xml = 0; 
       last; ## equivalent to break in C 
     } 
} 
close FD; 

注::沒有&LT空間&GT後 「&LT」,並在文本中的 「&GT」 標記之後並在代碼。因此,在運行代碼時刪除該空間。

的一種模式的選擇,檢測從「巨蟒COG」 :)採取XML

+0

嗨,首先非常感謝你,現在我有了一些想法。我會嘗試這樣。 – verendra

+0

嗨這段代碼沒有被執行,我正在使用未使用的$ _錯誤。我試圖使用$ line = $ _;但仍然出現錯誤。 – verendra

+0

我得到的錯誤就像在標量chomp和pattran匹配中使用未使用的$ _一樣。你可以幫我避免這個錯誤 – verendra

4

您的數據很奇怪,至少可以說。我在這裏做了兩個假設:'是示例字符串的起始分隔符,並且您想要提取尖括號(根據標準,既不是XML也不是XML註釋)之間的東西。無法保證不會出現錯誤分析的嵌入式C代碼。

use 5.010; 
use Data::Dumper qw(Dumper); 

say Dumper \%+ while 
'<dtcnumber>1223<dtcnumber> 
<discription>battery short circuited<discription> 
    <cause>due to unproper connections<cause> 
    main(); 
    { 
    .......... 
    ... 
    c code. 
    ... 
    };' =~ /<(?<key>[^>]+)>(?<value>[^<]+)<\g{key}>/g; 

輸出

$VAR1 = { 
      'value' => '1223', 
      'key' => 'dtcnumber' 
     }; 

$VAR1 = { 
      'value' => 'battery short circuited', 
      'key' => 'discription' 
     }; 

$VAR1 = { 
      'value' => 'due to unproper connections', 
      'key' => 'cause' 
     }; 
+0

很酷的方法。+1 – Arunmu

+0

嗨,非常感謝您的回覆,我有一些想法,我會像上面提到的那樣嘗試。 – verendra