/** * @param string must contain a well-formed formula string of boolean literals and operators.. * @return Formula corresponding to the string */ public static ParseTree parse(String string) { // Create a stream of tokens using the lexer. CharStream stream = new ANTLRInputStream(string); FormulaLexer lexer = new FormulaLexer(stream); lexer.reportErrorsAsExceptions(); TokenStream tokens = new CommonTokenStream(lexer); // Feed the tokens into the parser. FormulaParser parser = new FormulaParser(tokens); parser.reportErrorsAsExceptions(); // Generate the parse tree using the starter rule. ParseTree tree = parser.orExpr(); // "root" is the starter rule. // debugging option #1: print the tree to the console // System.err.println(tree.toStringTree(parser)); // debugging option #2: show the tree in a window // ((RuleContext)tree).inspect(parser); // debugging option #3: walk the tree with a listener // new ParseTreeWalker().walk(new FormulaListener_PrintEverything(), tree); // Finally, construct a Document value by walking over the parse tree. // ParseTreeWalker walker = new ParseTreeWalker(); // FormulaListener_FormulaCreator listener = new FormulaListener_FormulaCreator(); // walker.walk(listener, tree); // return the Document value that the listener created return tree; }
@Test public void testParser() throws Exception { LTLBitmap.Type type = LTLBitmap.Type.EWAH; LTLBitmap[] states = new LTLBitmap[] { new LTLBitmap(type, "01010"), new LTLBitmap(type, "10101"), new LTLBitmap(type, "00001"), }; String form = "s0 R s1"; Formula result = FormulaParser.parse(form); LTLBitmap bm = result.ltlExpr.getResult(states); assertEquals("00001", bm.toString()); }