private static void DumpSyntaxParserCode( RegulationList grammar, LRParsingMap map, string grammarId, string LR0Directory, SyntaxParserMapAlgorithm algorithm) { var parserType = new CodeTypeDeclaration(GetParserName(grammarId, algorithm)); parserType.IsClass = true; parserType.BaseTypes.Add(typeof(LRSyntaxParser)); DumpSyntaxParserFields(grammar, parserType, grammarId, algorithm); DumpSyntaxParserMethod_GetGrammar(grammar, parserType); DumpSyntaxParserMethod_GetParsingMap(grammar, map, parserType); var parserNamespace = new CodeNamespace(GetNamespace(grammarId)); parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Object).Namespace)); parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Collections.Generic.List <int>).Namespace)); parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(LALR1Compiler.HashCache).Namespace)); parserNamespace.Types.Add(parserType); //生成代码 string parserCodeFullname = Path.Combine(LR0Directory, GetParserName(grammarId, algorithm) + ".cs"); using (var stream = new StreamWriter(parserCodeFullname, false)) { CSharpCodeProvider codeProvider = new CSharpCodeProvider(); CodeGeneratorOptions geneOptions = new CodeGeneratorOptions();//代码生成选项 geneOptions.BlankLinesBetweenMembers = true; geneOptions.BracingStyle = "C"; geneOptions.ElseOnClosing = false; geneOptions.IndentString = " "; geneOptions.VerbatimOrder = true; codeProvider.GenerateCodeFromNamespace(parserNamespace, stream, geneOptions); } }
private static void DumpTreeNodeTypeConstCode( RegulationList grammar, LRParsingMap map, string grammarId, string LR0Directory, SyntaxParserMapAlgorithm algorithm) { var treeNodeConstType = new CodeTypeDeclaration(GetTreeNodeConstTypeName(grammarId, algorithm)); treeNodeConstType.IsClass = true; DumpTreeNodeConstFields(grammar, treeNodeConstType); var parserNamespace = new CodeNamespace(GetNamespace(grammarId)); parserNamespace.Imports.Add(new CodeNamespaceImport(typeof(System.Object).Namespace)); parserNamespace.Types.Add(treeNodeConstType); //生成代码 string fullname = Path.Combine(LR0Directory, GetTreeNodeConstTypeName(grammarId, algorithm) + ".cs"); using (var stream = new StreamWriter(fullname, false)) { CSharpCodeProvider codeProvider = new CSharpCodeProvider(); CodeGeneratorOptions geneOptions = new CodeGeneratorOptions();//代码生成选项 geneOptions.BlankLinesBetweenMembers = true; geneOptions.BracingStyle = "C"; geneOptions.ElseOnClosing = false; geneOptions.IndentString = " "; geneOptions.VerbatimOrder = true; codeProvider.GenerateCodeFromNamespace(parserNamespace, stream, geneOptions); } }
private static bool TestParsingMap(string directory, LRParsingMap parsingMap) { int conflicts = 0; foreach (var item in parsingMap) { if (item.Value.Count > 1) { conflicts += item.Value.Count - 1; } } if (conflicts > 0) { Console.WriteLine(" 【Exists {0} Conflicts in Parsingmap】", conflicts); } File.WriteAllText(Path.Combine(directory, "Conflicts.log"), conflicts.ToString()); return(conflicts > 0); }
private static void DumpSyntaxParserMethod_GetParsingMap( RegulationList grammar, LRParsingMap map, CodeTypeDeclaration parserType) { var method = new CodeMemberMethod(); method.Name = "GetParsingMap"; method.Attributes = MemberAttributes.Override | MemberAttributes.Family; method.ReturnType = new CodeTypeReference(typeof(LRParsingMap)); // if (parsingMap != null) { return parsingMap; } DumpSyntaxParserMethod_GetParsingMap_1(method); string varName = "map"; // LALR1Compiler.LRParsingMap map = new LALR1Compiler.LRParsingMap(); DumpSyntaxParserMethod_GetParsingMap_2(method, varName); int setActionCount = 0; foreach (var action in map) { setActionCount += action.Value.Count; //var parametes = new List<CodeExpression>(); string[] parts = action.Key.Split('+'); var stateId = new CodePrimitiveExpression(int.Parse(parts[0])); var nodeType = new CodeVariableReferenceExpression( GetNodeNameInParser(new TreeNodeType(parts[1], parts[1], parts[1]))); if (action.Value.Count > 1) { // action.Value超过1项,就说明有冲突。列出这些冲突应该是有价值的。 method.Comments.Add(new CodeCommentStatement( string.Format("{0} confilicts:", action.Value.Count), false)); foreach (var value in action.Value) { string str = string.Format("map.SetAction({0}, {1}, new {2}({3}));", parts[0], GetNodeNameInParser(new TreeNodeType(parts[1], parts[1], parts[1])), value.GetType().Name, value.ActionParam()); method.Comments.Add(new CodeCommentStatement(str, false)); } } foreach (var value in action.Value) { // map.SetAction(1, NODE__S, new LALR1Compiler.LR1GotoAction(2)); Type t = value.GetType(); CodeObjectCreateExpression ctor = null; string actionParam = value.ActionParam(); if (t == typeof(LR1AceptAction)) { ctor = new CodeObjectCreateExpression(value.GetType()); } else { ctor = new CodeObjectCreateExpression(t, new CodePrimitiveExpression(int.Parse(actionParam))); } var SetActionMethod = new CodeMethodReferenceExpression( new CodeVariableReferenceExpression(varName), "SetAction"); var setAction = new CodeMethodInvokeExpression( SetActionMethod, stateId, nodeType, ctor); method.Statements.Add(setAction); } } { method.Comments.Insert(0, new CodeCommentStatement(string.Format( "{0} SetAction() items", setActionCount))); } { // parsingMap = map; var assign = new CodeAssignStatement( new CodeVariableReferenceExpression("parsingMap"), new CodeVariableReferenceExpression("map")); method.Statements.Add(assign); } { // return map; var returnMap = new CodeMethodReturnStatement( new CodeVariableReferenceExpression("parsingMap")); method.Statements.Add(returnMap); } parserType.Members.Add(method); }
protected override LRParsingMap GetParsingMap() { if (parsingMap != null) { return(parsingMap); } var map = new LRParsingMap(); map.SetAction(1, __Grammar, new LR1GotoAction(2)); map.SetAction(1, __Production, new LR1GotoAction(3)); map.SetAction(1, __Vn, new LR1GotoAction(4)); map.SetAction(1, __left_angleLeave__, new LR1ShiftInAction(5)); map.SetAction(3, __ProductionList, new LR1GotoAction(6)); map.SetAction(3, __Production, new LR1GotoAction(7)); map.SetAction(3, __Vn, new LR1GotoAction(4)); map.SetAction(3, __left_angleLeave__, new LR1ShiftInAction(5)); map.SetAction(4, __colon_colon_equalLeave__, new LR1ShiftInAction(8)); map.SetAction(5, identifierLeave__, new LR1ShiftInAction(9)); map.SetAction(7, __ProductionList, new LR1GotoAction(10)); map.SetAction(7, __Production, new LR1GotoAction(7)); map.SetAction(7, __Vn, new LR1GotoAction(4)); map.SetAction(7, __left_angleLeave__, new LR1ShiftInAction(5)); map.SetAction(8, __Canditate, new LR1GotoAction(11)); map.SetAction(8, __V, new LR1GotoAction(12)); map.SetAction(8, __Vn, new LR1GotoAction(13)); map.SetAction(8, __Vt, new LR1GotoAction(14)); map.SetAction(8, __left_angleLeave__, new LR1ShiftInAction(5)); map.SetAction(8, __nullLeave__, new LR1ShiftInAction(15)); map.SetAction(8, __identifierLeave__, new LR1ShiftInAction(16)); map.SetAction(8, __numberLeave__, new LR1ShiftInAction(17)); map.SetAction(8, __constStringLeave__, new LR1ShiftInAction(18)); map.SetAction(8, constStringLeave__, new LR1ShiftInAction(19)); map.SetAction(9, __right_angleLeave__, new LR1ShiftInAction(20)); map.SetAction(11, __RightPartList, new LR1GotoAction(21)); map.SetAction(11, __vertical_barLeave__, new LR1ShiftInAction(22)); map.SetAction(12, __VList, new LR1GotoAction(23)); map.SetAction(12, __V, new LR1GotoAction(24)); map.SetAction(12, __Vn, new LR1GotoAction(13)); map.SetAction(12, __Vt, new LR1GotoAction(14)); map.SetAction(12, __left_angleLeave__, new LR1ShiftInAction(5)); map.SetAction(12, __nullLeave__, new LR1ShiftInAction(15)); map.SetAction(12, __identifierLeave__, new LR1ShiftInAction(16)); map.SetAction(12, __numberLeave__, new LR1ShiftInAction(17)); map.SetAction(12, __constStringLeave__, new LR1ShiftInAction(18)); map.SetAction(12, constStringLeave__, new LR1ShiftInAction(19)); map.SetAction(21, __semicolonLeave__, new LR1ShiftInAction(25)); map.SetAction(22, __Canditate, new LR1GotoAction(26)); map.SetAction(22, __V, new LR1GotoAction(12)); map.SetAction(22, __Vn, new LR1GotoAction(13)); map.SetAction(22, __Vt, new LR1GotoAction(14)); map.SetAction(22, __left_angleLeave__, new LR1ShiftInAction(5)); map.SetAction(22, __nullLeave__, new LR1ShiftInAction(15)); map.SetAction(22, __identifierLeave__, new LR1ShiftInAction(16)); map.SetAction(22, __numberLeave__, new LR1ShiftInAction(17)); map.SetAction(22, __constStringLeave__, new LR1ShiftInAction(18)); map.SetAction(22, constStringLeave__, new LR1ShiftInAction(19)); map.SetAction(24, __VList, new LR1GotoAction(27)); map.SetAction(24, __V, new LR1GotoAction(24)); map.SetAction(24, __Vn, new LR1GotoAction(13)); map.SetAction(24, __Vt, new LR1GotoAction(14)); map.SetAction(24, __left_angleLeave__, new LR1ShiftInAction(5)); map.SetAction(24, __nullLeave__, new LR1ShiftInAction(15)); map.SetAction(24, __identifierLeave__, new LR1ShiftInAction(16)); map.SetAction(24, __numberLeave__, new LR1ShiftInAction(17)); map.SetAction(24, __constStringLeave__, new LR1ShiftInAction(18)); map.SetAction(24, constStringLeave__, new LR1ShiftInAction(19)); map.SetAction(26, __RightPartList, new LR1GotoAction(28)); map.SetAction(26, __vertical_barLeave__, new LR1ShiftInAction(22)); map.SetAction(2, end_of_token_listLeave__, new LR1AceptAction()); map.SetAction(3, end_of_token_listLeave__, new LR1ReducitonAction(3)); map.SetAction(6, end_of_token_listLeave__, new LR1ReducitonAction(1)); map.SetAction(7, end_of_token_listLeave__, new LR1ReducitonAction(3)); map.SetAction(10, end_of_token_listLeave__, new LR1ReducitonAction(2)); map.SetAction(11, __semicolonLeave__, new LR1ReducitonAction(9)); map.SetAction(12, __vertical_barLeave__, new LR1ReducitonAction(7)); map.SetAction(12, __semicolonLeave__, new LR1ReducitonAction(7)); map.SetAction(13, __left_angleLeave__, new LR1ReducitonAction(10)); map.SetAction(13, __nullLeave__, new LR1ReducitonAction(10)); map.SetAction(13, __identifierLeave__, new LR1ReducitonAction(10)); map.SetAction(13, __numberLeave__, new LR1ReducitonAction(10)); map.SetAction(13, __constStringLeave__, new LR1ReducitonAction(10)); map.SetAction(13, constStringLeave__, new LR1ReducitonAction(10)); map.SetAction(13, __vertical_barLeave__, new LR1ReducitonAction(10)); map.SetAction(13, __semicolonLeave__, new LR1ReducitonAction(10)); map.SetAction(14, __left_angleLeave__, new LR1ReducitonAction(11)); map.SetAction(14, __nullLeave__, new LR1ReducitonAction(11)); map.SetAction(14, __identifierLeave__, new LR1ReducitonAction(11)); map.SetAction(14, __numberLeave__, new LR1ReducitonAction(11)); map.SetAction(14, __constStringLeave__, new LR1ReducitonAction(11)); map.SetAction(14, constStringLeave__, new LR1ReducitonAction(11)); map.SetAction(14, __vertical_barLeave__, new LR1ReducitonAction(11)); map.SetAction(14, __semicolonLeave__, new LR1ReducitonAction(11)); map.SetAction(15, __left_angleLeave__, new LR1ReducitonAction(13)); map.SetAction(15, __nullLeave__, new LR1ReducitonAction(13)); map.SetAction(15, __identifierLeave__, new LR1ReducitonAction(13)); map.SetAction(15, __numberLeave__, new LR1ReducitonAction(13)); map.SetAction(15, __constStringLeave__, new LR1ReducitonAction(13)); map.SetAction(15, constStringLeave__, new LR1ReducitonAction(13)); map.SetAction(15, __vertical_barLeave__, new LR1ReducitonAction(13)); map.SetAction(15, __semicolonLeave__, new LR1ReducitonAction(13)); map.SetAction(16, __left_angleLeave__, new LR1ReducitonAction(14)); map.SetAction(16, __nullLeave__, new LR1ReducitonAction(14)); map.SetAction(16, __identifierLeave__, new LR1ReducitonAction(14)); map.SetAction(16, __numberLeave__, new LR1ReducitonAction(14)); map.SetAction(16, __constStringLeave__, new LR1ReducitonAction(14)); map.SetAction(16, constStringLeave__, new LR1ReducitonAction(14)); map.SetAction(16, __vertical_barLeave__, new LR1ReducitonAction(14)); map.SetAction(16, __semicolonLeave__, new LR1ReducitonAction(14)); map.SetAction(17, __left_angleLeave__, new LR1ReducitonAction(15)); map.SetAction(17, __nullLeave__, new LR1ReducitonAction(15)); map.SetAction(17, __identifierLeave__, new LR1ReducitonAction(15)); map.SetAction(17, __numberLeave__, new LR1ReducitonAction(15)); map.SetAction(17, __constStringLeave__, new LR1ReducitonAction(15)); map.SetAction(17, constStringLeave__, new LR1ReducitonAction(15)); map.SetAction(17, __vertical_barLeave__, new LR1ReducitonAction(15)); map.SetAction(17, __semicolonLeave__, new LR1ReducitonAction(15)); map.SetAction(18, __left_angleLeave__, new LR1ReducitonAction(16)); map.SetAction(18, __nullLeave__, new LR1ReducitonAction(16)); map.SetAction(18, __identifierLeave__, new LR1ReducitonAction(16)); map.SetAction(18, __numberLeave__, new LR1ReducitonAction(16)); map.SetAction(18, __constStringLeave__, new LR1ReducitonAction(16)); map.SetAction(18, constStringLeave__, new LR1ReducitonAction(16)); map.SetAction(18, __vertical_barLeave__, new LR1ReducitonAction(16)); map.SetAction(18, __semicolonLeave__, new LR1ReducitonAction(16)); map.SetAction(19, __left_angleLeave__, new LR1ReducitonAction(17)); map.SetAction(19, __nullLeave__, new LR1ReducitonAction(17)); map.SetAction(19, __identifierLeave__, new LR1ReducitonAction(17)); map.SetAction(19, __numberLeave__, new LR1ReducitonAction(17)); map.SetAction(19, __constStringLeave__, new LR1ReducitonAction(17)); map.SetAction(19, constStringLeave__, new LR1ReducitonAction(17)); map.SetAction(19, __vertical_barLeave__, new LR1ReducitonAction(17)); map.SetAction(19, __semicolonLeave__, new LR1ReducitonAction(17)); map.SetAction(20, __colon_colon_equalLeave__, new LR1ReducitonAction(12)); map.SetAction(20, __left_angleLeave__, new LR1ReducitonAction(12)); map.SetAction(20, __nullLeave__, new LR1ReducitonAction(12)); map.SetAction(20, __identifierLeave__, new LR1ReducitonAction(12)); map.SetAction(20, __numberLeave__, new LR1ReducitonAction(12)); map.SetAction(20, __constStringLeave__, new LR1ReducitonAction(12)); map.SetAction(20, constStringLeave__, new LR1ReducitonAction(12)); map.SetAction(20, __vertical_barLeave__, new LR1ReducitonAction(12)); map.SetAction(20, __semicolonLeave__, new LR1ReducitonAction(12)); map.SetAction(23, __vertical_barLeave__, new LR1ReducitonAction(5)); map.SetAction(23, __semicolonLeave__, new LR1ReducitonAction(5)); map.SetAction(24, __vertical_barLeave__, new LR1ReducitonAction(7)); map.SetAction(24, __semicolonLeave__, new LR1ReducitonAction(7)); map.SetAction(25, __left_angleLeave__, new LR1ReducitonAction(4)); map.SetAction(25, end_of_token_listLeave__, new LR1ReducitonAction(4)); map.SetAction(26, __semicolonLeave__, new LR1ReducitonAction(9)); map.SetAction(27, __vertical_barLeave__, new LR1ReducitonAction(6)); map.SetAction(27, __semicolonLeave__, new LR1ReducitonAction(6)); map.SetAction(28, __semicolonLeave__, new LR1ReducitonAction(8)); parsingMap = map; return(parsingMap); }