Exemplo n.º 1
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());
     }
 }
Exemplo n.º 2
0
        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;
                        }
                    }
            }
        }