我的java应用程序需要一个解析器,所以我发现了JParsec库,并通过一个简单的计算器语法在他们的网站上完成了一个教程。似乎我应该能够使用库进行我的计划,因此开始原型化从示例计算器更改解析十进制文字到解析非常简单的标识符作为字符串。我现在唯一想要工作的操作是'+',以连接字符串,但我不断收到错误消息。这是我的代码:
import org.codehaus.jparsec.OperatorTable;
import org.codehaus.jparsec.Parser;
import org.codehaus.jparsec.Parsers;
import org.codehaus.jparsec.Scanners;
import org.codehaus.jparsec.Terminals;
import org.codehaus.jparsec.Tokens.Fragment;
import org.codehaus.jparsec.functors.Binary;
import org.codehaus.jparsec.functors.Map;
public class Test
{
enum BinaryOperator implements Binary<String> {
PLUS {
public String map(String a, String b) {
return a + b;
}
}
}
static final Parser<Void> IGNORED = Scanners.WHITESPACES.skipMany();
static final Parser<String> TEXT = Terminals.Identifier.TOKENIZER.map(new Map<Fragment, String>() {
public String map(Fragment arg0) {
return arg0.text()+"NEWTOSEEIFITSWORKING";
}
});
private static final Terminals OPERATORS = Terminals.operators("+");
static final Parser<?> TOKENIZER = Parsers.or(Terminals.Identifier.TOKENIZER, OPERATORS.tokenizer());
static Parser<?> term(String... names) {
return OPERATORS.token(names);
}
static <T> Parser<T> op(String name, T value) {
return term(name).retn(value);
}
static Parser<String> textparser(Parser<String> atom) {
Parser<String> parser = new OperatorTable<String>().infixl(op("+", BinaryOperator.PLUS), 10).build(atom);
return parser;
}
public static final Parser<String> TEXTPARSER = textparser(TEXT).from(TOKENIZER, IGNORED);
public static void main(String[] args)
{
System.out.println(TEXTPARSER.parse("hello+world"));
}}
这是我得到的错误:
Exception in thread "main" org.codehaus.jparsec.error.ParserException: Cannot scan characters on tokens.
line 1, column 1
at org.codehaus.jparsec.Parser.asParserException(Parser.java:591)
at org.codehaus.jparsec.Parser.run(Parser.java:600)
at org.codehaus.jparsec.MapParser.apply(MapParser.java:30)
at org.codehaus.jparsec.Parser.run(Parser.java:597)
at org.codehaus.jparsec.MapParser.apply(MapParser.java:30)
at org.codehaus.jparsec.Parser.run(Parser.java:597)
at org.codehaus.jparsec.BindNextParser.apply(BindNextParser.java:37)
at org.codehaus.jparsec.Parser.run(Parser.java:597)
at org.codehaus.jparsec.Sequence2Parser.apply(Sequence2Parser.java:32)
at org.codehaus.jparsec.Parser.run(Parser.java:597)
at org.codehaus.jparsec.ParserInternals.runNestedParser(ParserInternals.java:116)
at org.codehaus.jparsec.NestedParser.apply(NestedParser.java:32)
at org.codehaus.jparsec.Parser.run(Parser.java:597)
at org.codehaus.jparsec.Sequence2Parser.apply(Sequence2Parser.java:32)
at org.codehaus.jparsec.Parser.run(Parser.java:597)
at org.codehaus.jparsec.Parsers.parse(Parsers.java:97)
at org.codehaus.jparsec.Parser.parse(Parser.java:535)
at org.codehaus.jparsec.Parser.parse(Parser.java:546)
at org.codehaus.jparsec.Parser.parse(Parser.java:551)
at Test.main(Test.java:52)
Caused by: java.lang.IllegalStateException: Cannot scan characters on tokens.
at org.codehaus.jparsec.ParserState.characters(ParserState.java:56)
at org.codehaus.jparsec.PatternScanner.apply(PatternScanner.java:36)
at org.codehaus.jparsec.ReturnSourceParser.apply(ReturnSourceParser.java:32)
at org.codehaus.jparsec.Parser.run(Parser.java:597)
... 18 more
知道我做错了什么吗?非常感谢任何帮助!
答案 0 :(得分:0)
只是偶然发现浏览网页这个老问题。我认为您的问题是您正在使用Parser来解析String对象。解析器组合器在它们生成的对象类型上是强类型的,特别是终端上的组合器生成令牌,而不是字符串。