2013-12-19 65 views
0

我試圖用Antlr4解析MarkDown文本。爲了方便起見,我首先解析list視圖。 我找到了一個關於它的網頁。 http://www.cforcoding.com/2010/01/markdown-and-introduction-to-parsing.html解析MarkDown文件中的列表格式使用Antlr4

在該網頁上的語法似乎確定對我來說,我將其更改爲適合Antlr4格式是這樣的:

grammar MarkDown; 

listItem : ORDERED inline NEWLINE 
     | UNORDERED inline NEWLINE 
     ; 
inline  : (~ NEWLINE)+ ; 
ORDERED  : DIGIT+ '.' (' ' | '\t')+ ; 
UNORDERED : ('*' | '-' | '+') (' ' | '\t')+ ; 
DIGIT  : [0-9]+ ; 

NEWLINE  : '\r'? '\n' ; 

例如文件

1. abc 
2. kljjkj 
3. tree4545 

但它不工作,錯誤以下信息

line 1:3 token recognition error at: 'a' 
line 1:4 token recognition error at: 'b' 
line 1:5 token recognition error at: 'c' 
line 1:6 extraneous input '\r\n' expecting {ORDERED, UNORDERED, DIGIT} 
line 2:3 token recognition error at: 'k' 
line 2:4 token recognition error at: 'l' 
line 2:5 token recognition error at: 'j' 
line 2:6 token recognition error at: 'j' 
line 2:7 token recognition error at: 'k' 
line 2:8 token recognition error at: 'j' 
(listItem 1. (inline \r\n 2.) \r\n) 

你能幫我解決這個問題嗎?

回答

0

在解析器規則中,~取反記號,而不是字符。因此inline將嘗試匹配除NEWLINE之外的任何標記,它們是ORDERED,UNORDEREDDIGIT

ANTLR抱怨輸入​​,"kljjkj",...因爲沒有詞法分析規則匹配這些字符。

雖然以下是問答& A約爲ANTLR3,同樣的規則適用於ANTLR4:Negating inside lexer- and parser rules

+0

感謝您的答覆。你能否直接提出解決問題的建議?看看我能否輕鬆理解。 – jmuok