Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ANTLR4重复序列_Java_Parsing_Grammar_Antlr4_Expr - Fatal编程技术网

Java ANTLR4重复序列

Java ANTLR4重复序列,java,parsing,grammar,antlr4,expr,Java,Parsing,Grammar,Antlr4,Expr,根据以下规则: expr: '(' expr ')' #exprExpr | expr ( AND expr )+ #exprAnd | expr ( OR expr )+ #exprOr | atom #exprAtom | ID #exprId ; atom: '[' ID RELOP INT ']' ; 我允许这样的陈述: [a<3] and [b<4] [a<3] or [b<4] [a<3] or ([b<4]and [c

根据以下规则:

expr:
 '(' expr ')'  #exprExpr
|   expr ( AND expr )+  #exprAnd
|  expr ( OR expr )+  #exprOr
|  atom #exprAtom
|  ID  #exprId
;

atom:
  '[' ID RELOP INT ']'
;
我允许这样的陈述:

[a<3] and [b<4]
[a<3] or [b<4]
[a<3] or ([b<4]and [c<5])
[a<3] or [b<4] and [c<5]
 public String visitExprAnd(myParser.ExprAndContext ctx)  {
String res = "";
int type=-1;

int nAtoms = ctx.atom().size();
for (int i=0;i<nAtoms;i++) { 
  String s = visit(ctx.expr(i));
}
return s;

}
[a<3] or [b<4] and [c<5]
[a]
但禁止这样的言论:

[a<3] and [b<4]
[a<3] or [b<4]
[a<3] or ([b<4]and [c<5])
[a<3] or [b<4] and [c<5]
 public String visitExprAnd(myParser.ExprAndContext ctx)  {
String res = "";
int type=-1;

int nAtoms = ctx.atom().size();
for (int i=0;i<nAtoms;i++) { 
  String s = visit(ctx.expr(i));
}
return s;

}
[a<3] or [b<4] and [c<5]
编辑 一个完整的例子:

语法测试;
作语法分析
:expr EOF
;
expr
:orExpr
;
orExpr
:andExpr(或andExpr)*
;
andExpr
:原子(和原子)*
;
原子
:“(“expr”)”#atomExpr
|“['expr RELOP expr']”atomBracket
|ID#atomId
|国际原子薄荷
;
重新排序:[]'='?;
和:'和';
或:‘或’;
内部:[0-9]+;
ID:[a-zA-Z_][a-zA-Z_0-9]*;
空格:[\t\r\n]->跳过;

除非我遗漏了什么,否则您的语法仍然接受
[aI尝试了单独的orExpr和andExpr规则。它似乎对以下带括号的语句有问题:[12我尝试了这样做,以包括偏执规则:andExpr:bid(和bid)*|'('expr');但这也给出了一个错误(->“期望”['))嗯,
[12抱歉,我的帖子有点误导。我确实简化了语法。最初它是这样的:Ident:INT | ID;andatom:…|'['Ident RELOP INT']'