在 ANTLR 语法中使用不同的 case 关键字
我的语法中的一些关键字(字符串常量)包含大写字母 例如,
PREV_VALUE : 'PreviousValue';
这会导致奇怪的解析行为:包含相同大写字母('P','V')的其他标记被错误地解析。
这是词法分析器语法的简化版本:
lexer grammar ExpressionLexer;
COMMA : ',';
LPAREN : '(';
RPAREN : ')';
LBRACK : '[';
RBRACK : ']';
PLUS : '+';
MINUS : '-';
MULT : '*';
DIV : '/';
PREV_VALUE : 'PreviousValue';
fragment DIGIT : ('0'..'9');
fragment LETTER : ('a'..'z'|'A'..'Z'|'_');
fragment TAB : ('\t') ;
fragment NEWLINE : ('\r'|'\n') ;
fragment SPACE : (' ') ;
当我尝试解析这样的表达式时:
var expression = "P"; //Capital 'P' which included to the keyword 'PreviousValue'
var stringReader = new StringReader(expression);
var input = new ANTLRReaderStream(stringReader);
var expressionLexer = new ExpressionLexer(input);
var tokens = new CommonTokenStream(expressionLexer);
tokens._tokens
集合包含一个值
[0] = {[@0,1:1='<EOF>',<-1>,1:1]}
这是不正确的。
如果我将表达式
更改为“p”(小写字母) tokens._tokens
集合包含两个值
[0] = {[@0,0:0='p',<0>,1:0]}
[1] = {[@1,1:1='<EOF>',<-1>,1:1]}
这是正确的。
当字符串 PREV_VALUE : 'PreviousValue';
从语法中删除时,两个表达式都会被正确解析。
是否可以在关键字中使用不同的大小写? ANTLR语法中有使用此类关键字的例子吗?
Some keywords (string constant) in my grammar contain capital letters
e.g.
PREV_VALUE : 'PreviousValue';
This causes strange parsing behavior: other tokens that contain same capital letters ('P','V') are parsed incorrectly.
Here's a simplified version of the lexer grammar:
lexer grammar ExpressionLexer;
COMMA : ',';
LPAREN : '(';
RPAREN : ')';
LBRACK : '[';
RBRACK : ']';
PLUS : '+';
MINUS : '-';
MULT : '*';
DIV : '/';
PREV_VALUE : 'PreviousValue';
fragment DIGIT : ('0'..'9');
fragment LETTER : ('a'..'z'|'A'..'Z'|'_');
fragment TAB : ('\t') ;
fragment NEWLINE : ('\r'|'\n') ;
fragment SPACE : (' ') ;
When I try parsing such expression:
var expression = "P"; //Capital 'P' which included to the keyword 'PreviousValue'
var stringReader = new StringReader(expression);
var input = new ANTLRReaderStream(stringReader);
var expressionLexer = new ExpressionLexer(input);
var tokens = new CommonTokenStream(expressionLexer);
tokens._tokens
collection contains one value
[0] = {[@0,1:1='<EOF>',<-1>,1:1]}
It's incorrect.
If I change expression
to 'p' (lowercase letter)tokens._tokens
collection contains two values
[0] = {[@0,0:0='p',<0>,1:0]}
[1] = {[@1,1:1='<EOF>',<-1>,1:1]}
It's correct.
When string PREV_VALUE : 'PreviousValue';
is removed from grammar, both expressions are parsed correctly.
Is it possible to use different case in keywords?
Is there any example of using such keywords in ANTLR grammar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现很难相信
p
标记是根据您发布的语法创建的。前面有fragment
的词法分析器规则不会生成标记:这些规则仅由其他词法分析器规则使用。一个简单的演示展示了这一点:
现在生成词法分析器并编译
.java
源文件:并运行一些测试:
这是正确的,因为没有以 开头或匹配的(非片段)规则,一个
“p”
。这是正确的,因为以
"P"
开头的唯一(非片段)规则期望"r"
是下一个字符(不存在) 。I find it hard to believe a
p
token is created based on the grammar you posted. Lexer rules that havefragment
in front of them will not produce tokens: these rules are only used by other lexer rules.A simple demo shows this:
Now generate the lexer and compile the
.java
source file:and run a few tests:
which is correct since there is no (non-fragment) rule that starts with, or matches, a
"p"
.which is correct since the only (non-fragment) rule that starts with a
"P"
expects an"r"
to be the next character (which isn't there).