ANTLR 在输入“”时没有可行的替代方案;
我仍在 ANTLR 的学习路上。我已经构建了一个语法,并且在很大程度上它符合我的预期,但我需要它能够静默运行(没有输出到 stdout 或 stderr)。
Grammar
grammar MyPredicate;
options
{
output=AST;
}
parse : expression EOF
;
expression
: field WS? OPERATOR_BINARY WS? value
;
OPERATOR_BINARY
: '=' | '<' | '>' | '<=' | '>=' | '!=' | 'has'
;
value : VALUE_STRING
| VALUE_NUMERIC
| VALUE_BOOLEAN
;
VALUE_STRING
: '""'
| '"' (ESC_SEQ | ~('\\'|'"'))+ '"'
;
VALUE_NUMERIC
: ('0'..'9')+ ('.' ('0'..'9')+)?
;
VALUE_BOOLEAN
: 'true'
| 'false'
;
field : FIELD_NAME
;
FIELD_NAME
: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
ESC_SEQ
: '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
;
WS : (' ' | '\t' | '\r' | '\n') {skip();}
;
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 {
MyPredicateParser parser = new MyPredicateParser(new CommonTokenStream(new MyPredicateLexer(new ANTLRStringStream(args[0]))));
MyPredicateParser.parse_return r = parser.parse();
parser.parse();
if ( r.tree!=null ) {
System.out.println(((Tree)r.tree).toStringTree());
((CommonTree)r.tree).sanityCheckParentAndChildIndexes();
}
}
}
输入
a = 1
输出
line 0:-1 mismatched input '<EOF>' expecting FIELD_NAME
a = 1 null
我不确定为什么会收到 EOF 错误。据我了解,我的语法解析正确,并且在评估“解析”解析器后出现错误,但该节点正在寻找 EOF。使用ANTLR 3.2
I'm still on the learning path with ANTLR. I've built a grammar and for the most part it does what I expect, but I need it to be able to run silently (no output to stdout or stderr).
Grammar
grammar MyPredicate;
options
{
output=AST;
}
parse : expression EOF
;
expression
: field WS? OPERATOR_BINARY WS? value
;
OPERATOR_BINARY
: '=' | '<' | '>' | '<=' | '>=' | '!=' | 'has'
;
value : VALUE_STRING
| VALUE_NUMERIC
| VALUE_BOOLEAN
;
VALUE_STRING
: '""'
| '"' (ESC_SEQ | ~('\\'|'"'))+ '"'
;
VALUE_NUMERIC
: ('0'..'9')+ ('.' ('0'..'9')+)?
;
VALUE_BOOLEAN
: 'true'
| 'false'
;
field : FIELD_NAME
;
FIELD_NAME
: ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
ESC_SEQ
: '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
;
WS : (' ' | '\t' | '\r' | '\n') {skip();}
;
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 {
MyPredicateParser parser = new MyPredicateParser(new CommonTokenStream(new MyPredicateLexer(new ANTLRStringStream(args[0]))));
MyPredicateParser.parse_return r = parser.parse();
parser.parse();
if ( r.tree!=null ) {
System.out.println(((Tree)r.tree).toStringTree());
((CommonTree)r.tree).sanityCheckParentAndChildIndexes();
}
}
}
Input
a = 1
Output
line 0:-1 mismatched input '<EOF>' expecting FIELD_NAME
a = 1 null
I'm not sure why I get the EOF error. From what I understand my grammar is parsing correctly, and I get the error after the "parse" parser is evaluated, but that node is looking for the EOF. Using ANTLR 3.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您在 Main 类中调用了两次 parse() 。删除行:
只保留:
原位。
The problem is that you're calling
parse()
twice in your Main class. Remove the line:leaving only:
in place.