java输入时怎样选择可输入_java – 输入时没有可行的选择

关于我的语法我有一个小问题.

我想解析字符串,如下所示:

"(ICOM LIKE '%bridge%' or ICOM LIKE '%Munich%')"

我最终得到了以下语法(比我知道的要复杂得多):

//旨在解析完整的BQS形成的查询

grammar Logic;

options {

output=AST;

}

tokens {

NOT_LIKE;

}

/*------------------------------------------------------------------

* PARSER RULES

*------------------------------------------------------------------*/

// precedence order is (low to high): or, and, not, [comp_op, geo_op, rel_geo_op, like, not like, exists], ()

parse

: expression EOF -> expression

; // ommit the EOF token

expression

: query

;

query

: term (OR^ term)* // make `or` the root

;

term

: factor (AND^ factor)*

;

factor

: (notexp -> notexp) ( NOT LIKE e=notexp -> ^(NOT_LIKE $factor $e))?

;

notexp

: NOT^ like

| like

;

like // this one has to be completed (a lot)

: atom (LIKE^ atom)*

;

atom

: ID

| | '(' expression ')' -> expression

;

/*------------------------------------------------------------------

* LEXER RULES

*------------------------------------------------------------------*/

// GENERAL OPERATORS:

//NOTLIKE : 'notlike' | 'NOTLIKE'; // whitespaces have been removed

LIKE : 'like' | 'LIKE';

OR : 'or' | 'OR';

AND : 'and' | 'AND';

NOT : 'not' | 'NOT';

//ELEMENTS

CONSTANT_EXPRESSION : DATE | NUMBER | QUOTED_STRING;

ID : (CHARACTER|DIGIT)+;

WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { $channel = HIDDEN; } ;

fragment DATE : '\'' YEAR '/' MONTH '/' DAY (' ' HOUR ':' MINUTE ':' SECOND)? '\'';

fragment QUOTED_STRING : '\'' (CHARACTER)+ '\'' ;

//UNITS

fragment CHARACTER : ('a'..'z' | 'A'..'Z'|'.'|'\''|'%'); // FIXME: Careful, should be all ASCII

fragment DIGIT : '0'..'9' ;

fragment DIGIT_SEQ :(DIGIT)+;

fragment DEL : SPACE ',' SPACE ; //Delimiter + may be space behind

fragment NUMBER : (SIGN)? DIGIT_SEQ ('.' (DIGIT_SEQ)?)?; // should be given in decimal degrees, North is 0 and direction is clockwise, range is 0 to 360

fragment SIGN : '+' | '-';

fragment YEAR : DIGIT DIGIT DIGIT DIGIT;

fragment MONTH : DIGIT DIGIT;

fragment DAY : DIGIT DIGIT;

fragment HOUR : DIGIT DIGIT;

fragment MINUTE : DIGIT DIGIT;

fragment SECOND : DIGIT (DIGIT)? ('.' (DIGIT)+)?;

fragment SPACE : (' ')?;// used to increase compatibility

事实是,我在创建AST时有这样的信息:

line 1:11 no viable alternative at input ''%bridge%''

line 1:35 no viable alternative at input ''%Munich%''

生成的树虽然正确(至少我担心):

那么,有人能给我一个关于那里有什么问题的暗示吗?我认为character包含了correclty解析这个表达式所需的所有额外字符. . .

谢谢 !

像往常一样,一些Java代码可以快速测试语法:

import org.antlr.runtime.*;

import org.antlr.runtime.tree.*;

import org.antlr.stringtemplate.*;

public class Main {

public static void main(String[] args) throws Exception {

// the expression

String src = "(ICOM LIKE '%bridge%' or ICOM LIKE '%Munich%')";

// create a lexer & parser

//LogicLexer lexer = new LogicLexer(new ANTLRStringStream(src));

//LogicParser parser = new LogicParser(new CommonTokenStream(lexer));

LogicLexer lexer = new LogicLexer(new ANTLRStringStream(src));

LogicParser parser = new LogicParser(new CommonTokenStream(lexer));

// invoke the entry point of the parser (the parse() method) and get the AST

CommonTree tree = (CommonTree)parser.parse().getTree();

// print the DOT representation of the AST

DOTTreeGenerator gen = new DOTTreeGenerator();

StringTemplate st = gen.toDOT(tree);

System.out.println(st);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值