static void ParseInput(string inputString) { List <Lexem> lexems = new List <Lexem>(); if (inputString == "debug") { Debug = !Debug; Console.WriteLine($"Debug mode is {(Debug ? "on" : "off")}"); return; } if (inputString == "test") { inputString = ""; using (var testFile = File.OpenText("test.txt")) { while (!testFile.EndOfStream) { inputString += testFile.ReadLine(); } } } //1. Лексика //1.1 Разделение лексем var lexemBlock = new LexicalBlock(); List <string> rawLexems = lexemBlock.Parse(inputString); if (Debug) { Console.Write($"\nRaw lexems: {string.Join(",",rawLexems.Select(lexem => $"[{lexem}]"))}\n"); } //1.2 Разбор лексем lexems = lexemBlock.RecognizeLexems(rawLexems); //1.3 Формат для вывода string outputString = $"Lexical analysis output:\t{string.Join("",lexems.Select(lexem => $"({lexem.Key},{(lexem.Key == LexemType.SEPARATOR ? ((char) lexem.ValuePtr).ToString() : lexem.ValuePtr.ToString())})"))}"; if (Debug) { Console.WriteLine(outputString); } //2. Синтаксический анализ try { var syntaticBlock = new SyntacticBlock(); syntaticBlock.ProcessInput(lexems); } catch (CompilatorException ex) { Console.WriteLine($"Compilator exception: {ex.Message}"); } catch (Exception ex) { Console.WriteLine($"C# exception: {ex.Message}"); Console.WriteLine($"StackTrace: {ex.StackTrace}"); } }
static void Main(string[] args) { ConWorker conWorker = new ConWorker(); SyntacticBlock.GetGrammarRules(); conWorker.AddHandler(ParseInput); conWorker.Start(); }