找到你要的答案

Q:How to define alphanumeric token in antlr?

Q:如何定义数字ANTLR的象征?

I'd like to have an alphanumeric lexer rule, a token of any combination of letters and digits, here's my grammar

grammar Equery;

query:  queryTerm+;

queryTerm:  filter
        |   '(' queryTerm ')'
        ;

filter: kvpair
    |   'NOT' filter
    ;

kvpair: ID '=' VALUE;

ID: [a-zA-Z]+;
VALUE: [a-z0-9]+;
WS: [ \r\n\t]+ -> skip;

When I tested the kvpair rule with a=12, this error occurred:

mismatched input '12' expecting VALUE

I could work around this, but I'd like to know why 12 is not recognized as a VALUE?

我想有一个字母的词法规则,表示字母和数字的任意组合,这是我的语法

grammar Equery;

query:  queryTerm+;

queryTerm:  filter
        |   '(' queryTerm ')'
        ;

filter: kvpair
    |   'NOT' filter
    ;

kvpair: ID '=' VALUE;

ID: [a-zA-Z]+;
VALUE: [a-z0-9]+;
WS: [ \r\n\t]+ -> skip;

当我测试的kvpair规则= 12,这个错误发生:

输入不匹配的“12”期望值

我可以这样做,但我想知道为什么12是不承认作为一个价值?

answer1: 回答1:

Your grammar is correct, as far as I can tell. On my machine using Antlr4, I tested a = 12 through your kvpair rule, and it parsed fine. As far as I can tell by visual inspection, your code should work on previous versions of Antlr as well. I would try deleting all your Antlr generated files, and rebuilding the grammar to see if that is your issue.

据我所知,你的语法是正确的。在我的机器上使用antlr4,我测试了一= 12通过你的kvpair规则,并解析好。据我目测告诉,你的代码应该在ANTLR的以前的版本以及。我会删除你所有的ANTLR生成文件,重建语法看那是你的问题。

parsing  antlr  grammar