int visitorWay() { CharStream stream = new ANTLRInputStream(EXPRESSION); CalculatorLexer lexer = new CalculatorLexer(stream); CommonTokenStream tokenStream = new CommonTokenStream(lexer); CalculatorParser parser = new CalculatorParser(tokenStream); // System.out.println(parser.c()); ParseTree tree = parser.c(); CalculatorBaseVisitor<Integer> visitor = new CalculatorEvaluationVisitor(); Integer sum = visitor.visit(tree); return sum; }
int listenerWay() { CharStream stream = new ANTLRInputStream(EXPRESSION); CalculatorLexer lexer = new CalculatorLexer(stream); CommonTokenStream tokenStream = new CommonTokenStream(lexer); CalculatorParser parser = new CalculatorParser(tokenStream); // System.out.println(parser.c()); ParseTree tree = parser.c(); ParseTreeWalker walker = new ParseTreeWalker(); CalculatorWalker calcWalker = new CalculatorWalker(); walker.walk(calcWalker, tree); return calcWalker.getSum(); }