示例#1
0
 public Calculator(CalculatorParser parser, CalculatorConverter converter,
                   CalculatorOperation calculatorOperation)
 {
     _parser              = parser;
     _converter           = converter;
     _calculatorOperation = calculatorOperation;
 }
 public static void Main(string[] args)
 {
     Stream inputStream = Console.OpenStandardInput();
     AntlrInputStream input = new AntlrInputStream(inputStream);
     CalculatorLexer lexer = new CalculatorLexer(input);
     CommonTokenStream tokens = new CommonTokenStream(lexer);
     CalculatorParser parser = new CalculatorParser(tokens);
 }
示例#3
0
 public static void Main(String[] args)
 {
     while (true)
     {
         Console.Write(">> ");
         Lexer  lexer  = new Lexer(Console.ReadLine());
         Parser parser = new CalculatorParser(lexer);
         Console.WriteLine(parser.ParseExpression());
     }
 }
 public static void Main(string[] args)
 {
     Stream inputStream = Console.OpenStandardInput();
     ANTLRInputStream input = new ANTLRInputStream(inputStream);
     CalculatorLexer lexer = new CalculatorLexer(input);
     CommonTokenStream tokens = new CommonTokenStream(lexer);
     CalculatorParser parser = new CalculatorParser(tokens);
     var tree = parser.stat().Tree;
     Console.WriteLine(tree.ToStringTree());
     DumpTree(tree, "", "  ");
 }
        private int EvaluateInput(string input)
        {
            CalculatorLexer lexer = new CalculatorLexer(new AntlrInputStream(input));

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new ThrowingErrorListener <int>());

            CalculatorParser parser = new CalculatorParser(new CommonTokenStream(lexer));

            parser.RemoveErrorListeners();
            parser.AddErrorListener(new ThrowingErrorListener <IToken>());

            return(new CalculatorVisitor().Visit(parser.expression()));
        }
示例#6
0
        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();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Calculadora (Ctrl+Z para salir):");
            StreamReader inputStream = new StreamReader(Console.OpenStandardInput());

            while (!inputStream.EndOfStream)
            {
                AntlrInputStream  input  = new AntlrInputStream(inputStream.ReadLine());
                CalculatorLexer   lexer  = new CalculatorLexer(input);
                CommonTokenStream tokens = new CommonTokenStream(lexer);
                CalculatorParser  parser = new CalculatorParser(tokens);
                IParseTree        tree   = parser.prog();
                //Console.WriteLine(tree.ToStringTree(parser));
                CalculatorVisitor visitor = new CalculatorVisitor();
                Console.WriteLine(visitor.Visit(tree));
            }
        }
示例#8
0
文件: Program.cs 项目: battyone/Txt
        private static void Main(string[] args)
        {
            ShowHelp();
            CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
            var grammar = new CalculatorGrammar();

            grammar.Initialize();
            var    lexer      = grammar.Rule <Expression>("expression");
            var    parser     = new CalculatorParser();
            string expression = null;

            while (expression != "")
            {
                expression = PromptLine();
                if (string.IsNullOrEmpty(expression))
                {
                    return;
                }
                using (var stringTextSource = new StringTextSource(expression))
                    using (var textScanner = new TextScanner(stringTextSource))
                    {
                        var readResult = lexer.Read(textScanner);
                        if (readResult != null)
                        {
                            var textRepresentation = readResult.Text;
                            var parseResult        = parser.ParseExpression(readResult);
                            Console.WriteLine("{0}={1}", textRepresentation, parseResult);
                        }
                        else
                        {
                            Console.WriteLine("Invalid input detected");
                            Console.ForegroundColor = ConsoleColor.Red;
                            using (var reader = new TextSourceReader(stringTextSource))
                            {
                                Console.WriteLine(reader.ReadToEnd());
                            }
                            Console.ForegroundColor = DefaultForegroundColor;
                        }
                    }
            }
        }