我想匹配形式的輸入::ANTLR4:不匹配的輸入
commit a1b2c3
Author: Michael <[email protected]>
commit d3g4
Author: David <[email protected]>
這是我寫的語法:
grammar commit;
file : commitinfo+;
commitinfo : commitdesc authordesc;
commitdesc : 'commit' COMMITHASH NEWLINE;
authordesc : 'Author:' AUTHORNAME '<' EMAIL '>' NEWLINE;
COMMITHASH : [a-z0-9]+;
AUTHORNAME : [a-zA-Z]+;
EMAIL : [[email protected]]+;
NEWLINE : '\r'?'\n';
WHITESPACE : [ \t]->skip;
上述解析器的問題是,對於上面的輸入它完美匹配。但是,當輸入變爲:
commit c1d2
Author: michael <[email protected]>
它引發等的錯誤:期待AUTHORNAME 8不匹配的輸入 '邁克爾':
線2。
當我打印的標記,似乎字符串「邁克爾」獲取令牌COMMITHASH而不是AUTHORNAME匹配。
如何解決上述情況?
RISHABH您好,感謝。我使用詞法模式解決它。 – Ramg