2014-01-30 32 views
0

我正在研究語法和解析(計算理論)。什麼是C風格評論的語法,以/*開頭,以*/結尾並且不包含*/。什麼是字符串/**a/*a**/的解析樹?計算理論 - 語法和解析

回答

1

確保一個*註釋文本里面要麼:

  • <text> ::= "*":最後出現在註釋文本(終端符號),如/*969969**/。注意最後*之前*/屬於評論文本。

  • <text> ::= "*" "[not a slash]" <text>:除斜槓之外的任何內容都只能被父規則捕獲,並被視爲終端符號。

更正式地說:

<comment> ::= "/*" <text> "*/" 
<text> ::= <starless> <text> | "*" <slashless> <text> | "*" | "" 

<starless> ::= "a" | "b" | "c" |... all printable except "*" 
<slashless> ::= "a" | "b" | "c" |... all printable except "/" 

正如你看到的,一個*被允許出現在僅在兩個給定的規則被滿足的文字。

/**a/*a**/的分析樹,如圖產生:

    comment 
       +--------+----+ 
      /  text \ 
      /  |  \ 
     /+--------+  \ 
     //  /|  \ 
     //  /|   \ 
     //  /|   \ 
    // slashless text  : 
    // /  |   :  
    // /  +--+   :  
    // / / |   :  
// /starless text  :    
// //  |   :     
// // +---+---+  :    
: : : : / | \  :  
: : : : /slashless text :  
: : : : : :  : : 
/* * a / * a  * */ 

這些規則並不適用於真正的C源代碼。您需要定義可能「競爭」這些符號的規則(str = "/*Not a comment*/"):導出雙引號字符串,內聯註釋,預處理器指令等。