2013-01-14 26 views
1

我試圖編寫一個樹梢解析器來將一些乳膠命令解析爲HTML標記。隨着以下我得到生成的代碼deadspin。我已經建立與tt的源代碼,並通過加強,但它並沒有真正闡明潛在的問題是什麼(它只是在_nt_paragraph旋轉)解析樹枝文件時樹梢無限循環

Test input: "\emph{hey} and some more text."

grammar Latex 
    rule document 
    (paragraph)* { 
     def content 
     [:document, elements.map { |e| e.content }] 
     end 
    } 
    end 

    # Example: There aren't the \emph{droids you're looking for} \n\n. 
    rule paragraph 
    (text/tag)* eop { 
     def content 
     [:paragraph, elements.map { |e| e.content } ] 
     end 
    } 
    end 

    rule text 
    (!(tag_start/eop) .)* { 
     def content 
     [:text, text_value ] 
     end 
    } 
    end 

    # Example: \tag{inner_text} 
    rule tag 
    "\\emph{" inner_text '}' { 
     def content 
     [:tag, inner_text.content] 
     end 
    } 
    end 

    # Example: \emph{inner_text} 
    rule inner_text 
    (!'}' .)* { 
     def content 
     [:inner_text, text_value] 
     end 
    } 
    end 

    # End of paragraph. 
    rule eop 
    newline 2.. { 
     def content 
     [:newline, text_value] 
     end 
    } 
    end 

    rule newline 
    "\n" 
    end 

    # You know, what starts a tag 
    rule tag_start 
    "\\" 
    end 

end 

回答

0

對於任何人都好奇,克利福德在上谷歌集團的treetop開發人員想到了這一點。

問題出在段落和文字上。

文本是0個或更多字符,並且段落中可能有0個或更多文本,所以發生的是在第一個\ n之前有無限長度爲0的字符,導致解析器死循環。解決方法是將文本調整爲:

(!(tag_start/eop) .)+ 

因此,它必須至少有一個字符要匹配。

+0

隨意標記爲正確的答案。 –