Exemplo n.º 1
0
 /// <summary>
 /// Using http://programming-pages.com/2013/12/14/antlr-4-with-c-and-visual-studio-2012/ as a guide.
 /// </summary>
 /// <param name="args"></param>
 static void Main(string[] args)
 {
     string test = "a = 10*10";
     var input = new AntlrInputStream(test);
     var lexer = new calculatorLexer(input);
     CommonTokenStream tokens = new CommonTokenStream(lexer);
     var parser = new calculatorParser(tokens);
     var tree = parser.equation();
     Console.WriteLine(tree.ToStringTree(parser));
 }
Exemplo n.º 2
0
        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);
        }