2014-03-24 51 views
1

的ANTLR4書處理引用了一個多模式的例子不一致令牌ANTLR4

https://github.com/stfairy/learn-antlr4/blob/master/tpantlr2-code/lexmagic/ModeTagsLexer.g4

lexer grammar ModeTagsLexer; 

// Default mode rules (the SEA) 
OPEN : '<'  -> mode(ISLAND) ;  // switch to ISLAND mode 
TEXT : ~'<'+ ;       // clump all text together 

mode ISLAND; 
CLOSE : '>'  -> mode(DEFAULT_MODE) ; // back to SEA mode 
SLASH : '/' ; 
ID : [a-zA-Z]+ ;      // match/send ID in tag to parser 

https://github.com/stfairy/learn-antlr4/blob/master/tpantlr2-code/lexmagic/ModeTagsParser.g4

parser grammar ModeTagsParser; 

options { tokenVocab=ModeTagsLexer; } // use tokens from ModeTagsLexer.g4 

file: (tag | TEXT)* ; 

tag : '<' ID '>' 
    | '<' '/' ID '>' 
    ; 

我試圖建立這個例子,但使用用於分隔符的«»字符。如果我可以替換爲我收到錯誤126

cannot create implicit token for string literal in non-combined grammar: '«'

事實上,這似乎是我在解析器tag規則«字符儘快發生。

tag : '«' ID '>'; 

OPEN : '«' -> pushMode(ISLAND); 
TEXT : ~'«'+; 

有一些ANTLR富我失蹤?這是使用antlr4-maven-plugin4.2

維基提到了這方面的內容,但我讀到的方式與github上的例子和使用<時的軼事經驗相矛盾。請參閱「冗餘字符串文字」在https://theantlrguy.atlassian.net/wiki/display/ANTLR4/Lexer+Rules

回答

1

以下情況之一發生的事情:

  1. 你忘了在更新OPEN規則ModeTagsLexer.g4使用以下格式:

    OPEN : '«' -> mode(ISLAND) ; 
    
  2. 您在ANTLR 4中發現了一個錯誤,該錯誤應報告給issue tracker

+0

我當然更新了OPEN規則,上面的第二個片段是剪切/粘貼。我會試着敲一張票的測試用例。謝謝 – ptomli

+0

這看起來像是一個antlr4-maven-plugin和/或m2e的問題。除此之外,我還沒有真正能夠追蹤它。你知道比我更多的方式,你可能有更好的運氣,看到示例項目https://github.com/ptomli/antlr4-22613293 – ptomli

+0

FWIW,我爲此創建了一張票,最終https:// github。 com/antlr/antlr4/issues/563 – ptomli

0

您是否指定了ANTLR在讀取語法時應該使用的文件編碼?它應該可以與歐洲字符少於255,但...

+0

除了指定源/資源爲UTF-8之外,我可以做到這一點嗎?看到示例github回購破損是可重複的https://github.com/antlr/antlr4/issues/563 – ptomli

+0

不確定關於maven,但在命令行上鍵入「antlr4」說: -encoding ___指定語法文件編碼;例如,euc-jp –