public Value CalculateFormula(string formula) { AntlrInputStream input = new AntlrInputStream(formula); //2. parse is to AntlrIS calculatorLexer lexer = new calculatorLexer(input); //3. lexer created from AntlrIS CommonTokenStream tokens = new CommonTokenStream(lexer); //4. want to save tokens from the lexer calculatorParser parser = new calculatorParser(tokens); //5. feed these tokens from lexer when creating the parser IParseTree tree = parser.block(); //6. run parser using parser.prog() and save output in IParseTree //Console.WriteLine(tree.ToStringTree(parser)); //7. CalcVisitor visitor = new CalcVisitor(); //8. CalcVisitor will do the evaluations Value ret = visitor.Visit(tree.GetChild(0)); //Console.WriteLine(ret); //z. CalcVisitor performs the actual evaluations + then write output return(ret); }
static void Main(string[] args) { StreamReader inputStream = new StreamReader(Console.OpenStandardInput()); //1. type in our expressions AntlrInputStream input = new AntlrInputStream(inputStream.ReadLine()); //2. parse is to AntlrIS calculatorLexer lexer = new calculatorLexer(input); //3. lexer created from AntlrIS CommonTokenStream tokens = new CommonTokenStream(lexer); //4. want to save tokens from the lexer calculatorParser parser = new calculatorParser(tokens); //5. feed these tokens from lexer when creating the parser IParseTree tree = parser.block(); //6. run parser using parser.prog() and save output in IParseTree //Console.WriteLine(tree.ToStringTree(parser)); //7. CalcVisitor visitor = new CalcVisitor(); //8. CalcVisitor will do the evaluations //Console.WriteLine("Tree: " + visitor.Visit(tree.GetChild(0))); //z. CalcVisitor performs the actual evaluations + then write output //dont dropout before i can see return Console.ReadKey(); }