public static void Main(String[] args) { var data = File.ReadAllText("input.txt"); var input = new AntlrInputStream(data); CalculatorLexer lexer = new CalculatorLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); CalculatorParser parser = new CalculatorParser(tokens); var tree = parser.input(); CalculatorBaseVisitorImpl calcVisitor = new CalculatorBaseVisitorImpl(); double result = calcVisitor.Visit(tree); Console.WriteLine("Result: " + result); }
public void Test_Visitor_With_SingleExpression_And_MultipleVariables_Returns_Variables() { var expressions = @"x = (((nrm / (1 + pct)) * (1 + (iss/100) + (pis/100))) / (1 + (iss/100) + (pis/100))) * (1 + (iss/100) + (pis/100) + frd)"; AntlrInputStream input = new AntlrInputStream(expressions); CalculatorLexer lexer = new CalculatorLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); CalculatorParser parser = new CalculatorParser(tokens); IParseTree tree = parser.input(); CalculatorBaseVisitorImpl visitor = new CalculatorBaseVisitorImpl(); visitor.Variables.Add("nrm", 1223.5251m); visitor.Variables.Add("pct", 0.05m); visitor.Variables.Add("iss", 5m); visitor.Variables.Add("pis", 3m); visitor.Variables.Add("frd", 0.012m); visitor.Visit(tree); var result = visitor.Variables["x"]; }
static void Main(string[] args) { try { StreamReader inputStream = new StreamReader(Console.OpenStandardInput()); AntlrInputStream input = new AntlrInputStream(inputStream.ReadToEnd()); CalculatorLexer lexer = new CalculatorLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); CalculatorParser parser = new CalculatorParser(tokens); IParseTree tree = parser.input(); Console.WriteLine(tree.ToStringTree(parser)); CalculatorVisitor visitor = new CalculatorVisitor(); Console.WriteLine(visitor.Visit(tree)); } catch (DivideByZeroException ex) { Console.WriteLine("Cannot Divide By Zero"); } Console.ReadLine(); }
static void Main(string[] args) { var expressions = @"x = (((nrm / (1 + pct)) * (1 + (iss/100) + (pis/100))) / (1 + (iss/100) + (pis/100))) * (1 + (iss/100) + (pis/100) + frd)"; AntlrInputStream input = new AntlrInputStream(expressions); CalculatorLexer lexer = new CalculatorLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); CalculatorParser parser = new CalculatorParser(tokens); IParseTree tree = parser.input(); CalculatorBaseVisitorImpl visitor = new CalculatorBaseVisitorImpl(); visitor.Variables.Add("nrm", 1223.5251m); visitor.Variables.Add("pct", 0.05m); visitor.Variables.Add("iss", 5m); visitor.Variables.Add("pis", 3m); visitor.Variables.Add("frd", 0.012m); visitor.Visit(tree); var result = visitor.Variables["x"]; Console.ReadLine(); }