Esempio n. 1
0
  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;
  }
Esempio n. 2
0
  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();
  }