public override void EnterEquation(CalculatorParser.EquationContext context) { op = context.OPERATOR().GetText(); values = new List <int>(); foreach (ITerminalNode number in context.NUMBERS()) { values.Add(int.Parse(number.GetText())); } }
static void Main(string[] args) { AntlrInputStream inputStream = new AntlrInputStream("2 + 2"); CalculatorLexer calcLexer = new CalculatorLexer(inputStream); CommonTokenStream commonTokenStream = new CommonTokenStream(calcLexer); CalculatorParser calcParser = new CalculatorParser(commonTokenStream); CalculatorParser.EquationContext equationContext = calcParser.equation(); CalculatorVisitor calcVisitor = new CalculatorVisitor(); calcVisitor.EnterEquation(equationContext); calcVisitor.printResult(); Console.ReadLine(); }
/// <summary> /// Visit a parse tree produced by <see cref="CalculatorParser.equation"/>. /// <para> /// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/> /// on <paramref name="context"/>. /// </para> /// </summary> /// <param name="context">The parse tree.</param> /// <return>The visitor result.</return> public virtual Result VisitEquation([NotNull] CalculatorParser.EquationContext context) { return(VisitChildren(context)); }
/// <summary> /// Exit a parse tree produced by <see cref="CalculatorParser.equation"/>. /// <para>The default implementation does nothing.</para> /// </summary> /// <param name="context">The parse tree.</param> public virtual void ExitEquation([NotNull] CalculatorParser.EquationContext context) { }