/// <summary> /// ContextfreeGrammar compiler的工作过程。 /// </summary> /// <param name="sourceCode"></param> /// <param name="directory"></param> /// <param name="grammarId"></param> /// <returns></returns> private static bool GetGrammar(string sourceCode, string directory, string grammarId, out RegulationList grammar, out string errorInfo) { grammar = null; errorInfo = string.Empty; Console.WriteLine(" Get grammar from source code..."); var lexi = new ContextfreeGrammarLexicalAnalyzer(); TokenList tokenList = lexi.Analyze(sourceCode); if (!tokenList.Check(out errorInfo)) { return(false); } Console.WriteLine(" Dump {0}", grammarId + ".TokenList.log"); tokenList.Dump(Path.Combine(directory, grammarId + ".TokenList.log")); var parser = new ContextfreeGrammarSyntaxParser(); SyntaxTree tree = parser.Parse(tokenList); Console.WriteLine(" Dump {0}", grammarId + ".Tree.log"); tree.Dump(Path.Combine(directory, grammarId + ".Tree.log")); grammar = tree.DumpGrammar(); Console.WriteLine(" Dump {0}", grammarId + ".FormatedGrammar.log"); grammar.Dump(Path.Combine(directory, grammarId + ".FormatedGrammar.log")); return(true); }
private static void TestCode(Assembly asm, string directory, string compilerDir, string grammarId, SyntaxParserMapAlgorithm syntaxParserMapAlgorithm) { if (asm == null) { Console.WriteLine(" Get Compiled Compiler Failed..."); return; } { string conflicts = File.ReadAllText(Path.Combine(compilerDir, "Conflicts.log")); if (int.Parse(conflicts) > 0) { Console.WriteLine(" No need to Test Code with conflicts in SyntaxParser"); return; } } try { LexicalAnalyzer lexi = asm.CreateInstance( GetNamespace(grammarId) + "." + GetLexicalAnalyzerName(grammarId)) as LexicalAnalyzer; LRSyntaxParser parser = asm.CreateInstance( GetNamespace(grammarId) + "." + GetParserName(grammarId, syntaxParserMapAlgorithm)) as LRSyntaxParser; string[] sourceCodeFullnames = Directory.GetFiles( directory, "*.Code", SearchOption.TopDirectoryOnly); foreach (var fullname in sourceCodeFullnames) { try { FileInfo fileInfo = new FileInfo(fullname); string sourceCode = File.ReadAllText(fullname); TokenList tokenList = lexi.Analyze(sourceCode); tokenList.Dump(Path.Combine(compilerDir, fileInfo.Name + ".TokenList.log")); SyntaxTree tree = parser.Parse(tokenList); tree.Dump(Path.Combine(compilerDir, fileInfo.Name + ".Tree.log")); } catch (Exception e) { Console.WriteLine("Testing Error for {0}:", fullname); Console.WriteLine(e); } } } catch (Exception ex) { Console.WriteLine("Running Errors:"); Console.Write(" "); Console.WriteLine(ex); } }