簡體   English   中英

antlr4語法-輸入錯誤不匹配

[英]antlr4 grammar - Mismatched input error

我在這里有一個非常簡單的語法:

grammar mygrammar;
prog  : E 'arrow' ('and' E PATTERN) ;
PATTERN : ( BPATTERN | ('and' PATTERN)+ | ('or' PATTERN)+ ) ;
BPATTERN : ( L | P ('and' E PATTERN) ) ;
L : ( E | 'not' E | U | 'not' U ) ;
P : 'p' ;
U : 'u' ;
E : 'e' ;
WS : [ \t\r\n]+ -> skip ;

我解析的所有輸入字符串,錯誤始終相同:

第1:0行輸入“ e”不匹配,期望“ e”

如果我將'e'令牌更改為'x',則得到相同的標記,但內部帶有'x',因此問題似乎與非終結符E有關。有人知道錯誤在哪里嗎?

這是因為輸入"e"被標記為L標記。 您需要使L成為解析器規則,而不是詞法分析器規則。

解析器規則以小寫字母開頭,因此您可以嘗試如下操作:

grammar mygrammar;

// parser rules
prog     : E 'arrow' 'and' E pattern ;

pattern  : bpattern 
         | ('and' pattern)+ 
         | ('or' pattern)+
         ;

bpattern : l 
         | P 'and' E pattern
         ;

l        : 'not'? ( E | U ) ;

// lexer rules (tokens)
P : 'p' ;
U : 'u' ;
E : 'e' ;

// skipped tokens
WS : [ \t\r\n]+ -> skip ;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM