我有这样一个反this语法:
grammar HelloGrammar1;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
STATEMENT : 'hello' ID ';' ;
WS : (' '|'\t'|'\r'|'\n')* ;我希望它解析以下文本:hello qwerty ;。是这样工作的。如果我将字符串更改为helloqwerty;,一切都很好。我还可以将语法更改为:
grammar HelloGrammar2;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
STATEMENT : 'hello' WS ID WS ';' ;
WS : (' '|'\t'|'\r'|'\n')* ;在这种情况下,hello qwerty ;工作得很好。是否可以使ANTLR自动跳过空白空间?(也就是说-我想让HelloGrammar1与hello qwerty ;一起工作)
更新
如果这有意义的话:我正在ANTLRWorks中测试它。
更新2
也尝试过这样做:
grammar HelloGrammar;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
STATEMENT : 'hello' ID ';' ;
WS : (' '|'\t'|'\r'|'\n') { $channel = HIDDEN; } ;还是不起作用。
更新3
我使用“解释器”选项卡,并选择了“语句”规则。
发布于 2011-09-05 17:03:14
我认为问题可能在于,您应该将语句(目前是一个词法规则)更改为语句(解析器规则)。
grammar HelloGrammar;
statement : 'hello' ID ';' ;
ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
WS : (' '|'\t'|'\r'|'\n') { $channel = HIDDEN; } ;在ANTLRWorks中,这可以接受:
hello qwerty;
hello qwerty;
hello loki2302;
hello qwerty ;但不接受:
helloqwerty;
helloqwerty ;
hello;
hello qwertyhttps://stackoverflow.com/questions/7310000
复制相似问题