コード例 #1
0
        private static List <ProductionNode> GetNotImplementedNodeList(string sourceCode)
        {
            var lexAna = new bitzhuwei.CGCompiler.LexicalAnalyzerCG();

            lexAna.SetSourceCode(sourceCode);
            var tokens       = lexAna.Analyze();
            var syntaxParser = new bitzhuwei.CGCompiler.LL1SyntaxParserCG();

            syntaxParser.SetTokenListSource(tokens);
            var tree = syntaxParser.Parse();
            ContextfreeGrammar grammar = tree.GetGrammar();

            grammar.GrammarName = "TmpGrammarName";
            grammar.Namespace   = "TmpNamespace";

            List <ProductionNode> testedNodeList         = new List <ProductionNode>();
            List <ProductionNode> notImplementedNodeList = new List <ProductionNode>();

            foreach (var production in grammar.ProductionCollection)
            {
                foreach (var candidate in production.RightCollection)
                {
                    foreach (var node in candidate)
                    {
                        if (node.Position != EnumProductionNodePosition.NonLeave)
                        {
                            continue;
                        }
                        if (testedNodeList.Contains(node))
                        {
                            continue;
                        }

                        bool implemented = false;
                        foreach (var p in grammar.ProductionCollection)
                        {
                            if (p.Left == node)
                            {
                                testedNodeList.Add(node);
                                implemented = true;
                                break;
                            }
                        }

                        if ((!implemented) && (!notImplementedNodeList.Contains(node)))
                        {
                            notImplementedNodeList.Add(node);
                        }
                    }
                }
            }
            return(notImplementedNodeList);
        }
コード例 #2
0
        private void codeGenerator_DoWork(object sender, DoWorkEventArgs e)
        {
            var param = e.Argument as CodeGeneratorParam;
            ContextfreeGrammar grammar = null;
            LL1GeneraterInput  input   = null;

            {
                codeGenerator.ReportProgress(6, "文法代码已加载!");
                var lexAna = new bitzhuwei.CGCompiler.LexicalAnalyzerCG();
                lexAna.SetSourceCode(param.sourceCode);
                var tokens = lexAna.Analyze();
                codeGenerator.ReportProgress(12, "得到文法代码的单词列表!");
                var syntaxParser = new bitzhuwei.CGCompiler.LL1SyntaxParserCG();
                syntaxParser.SetTokenListSource(tokens);
                var tree = syntaxParser.Parse();
                codeGenerator.ReportProgress(18, "得到文法代码的语法树!");
                grammar             = tree.GetGrammar();
                grammar.GrammarName = param.compilerName;
                grammar.Namespace   = param._namespace;
                codeGenerator.ReportProgress(24, "得到文法的数据结构!");
            }
            //var grammar = TestGetCGGrammar();
            {
                var xml         = grammar.ToXElement();
                var xmlFullname = Path.Combine(param.folder, param.compilerName + ".xml");
                xml.Save(xmlFullname);
                codeGenerator.ReportProgress(30, string.Format("文法数据结构已保存为[{0}]!", xmlFullname));
            }
            {
                var txt         = grammar.ToString();
                var txtFullname = Path.Combine(param.folder, param.compilerName + ".txt");
                txt.Save(txtFullname);
                codeGenerator.ReportProgress(33, string.Format("文法已保存为[{0}]!", txtFullname));
            }
            {
                var firstCollection = grammar.GetFirstCollection();
                DumpFirstCollection(param, firstCollection);
                codeGenerator.ReportProgress(36, "得到文法的FIRST集!");
                if (firstCollection.Conflicts())
                {
                    var conflicted = firstCollection.GetConflicted();
                    codeGenerator.ReportProgress(37, "冲突的FIRST集所在的产生式:" + Environment.NewLine + conflicted.ToString());
                    throw new Exception("您给定的文法的FIRST集有冲突,详情见状态框。");
                }
            }
            {
                var    followCollection = grammar.GetFollowCollection();
                string fullname         = Path.Combine(param.folder, "FollowCollection" + param.compilerName + ".txt");
                File.WriteAllText(fullname, followCollection.ToString());
            }
            codeGenerator.ReportProgress(42, "得到文法的FOLLOW集!");
            {
                var    ll1ParserMap = grammar.GetLL1ParserMap();
                string fullname     = Path.Combine(param.folder, "LL1ParserMap" + param.compilerName + ".txt");
                File.WriteAllText(fullname, ll1ParserMap.ToString());
                codeGenerator.ReportProgress(48, "得到文法的LL1分析表!");
            }
            input = new LL1GeneraterInput(grammar);
            {
                string fullname = Path.Combine(param.folder, "LexicalAnalyzer" + param.compilerName + ".cs");
                grammar.GenerateLexicalAnalyzer(fullname, input);
                codeGenerator.ReportProgress(54, string.Format("自动生成\"{0}\"的代码!", grammar.GetLexicalAnalyzerName()));
            }
            {
                var ll1ParserGenerated = grammar.GenerateLL1SyntaxParser(input);
                codeGenerator.ReportProgress(60, string.Format("自动生成\"{0}\"的代码!", grammar.GetLL1SyntaxParserName()));
                ll1ParserGenerated.Save(Path.Combine(param.folder, "LL1SyntaxParser" + param.compilerName + ".cs"));
            }
            {
                var enumCharTypeGenerated = grammar.GenerateEnumCharType(input);
                codeGenerator.ReportProgress(76, string.Format("自动生成\"{0}\"的代码!", grammar.GetEnumCharTypeSG()));
                enumCharTypeGenerated.Save(Path.Combine(param.folder, "EnumCharType" + param.compilerName + ".cs"));
            }
            {
                var enumTokenTypeGenerated = grammar.GenerateEnumTokenType(input);
                codeGenerator.ReportProgress(82, string.Format("自动生成\"{0}\"的代码!", grammar.GetEnumTokenTypeSG()));
                enumTokenTypeGenerated.Save(Path.Combine(param.folder, "EnumTokenType" + param.compilerName + ".cs"));
            }
            {
                var enumVTypeGenerated = grammar.GenerateEnumVType(input);
                codeGenerator.ReportProgress(88, string.Format("自动生成\"{0}\"的代码!", grammar.GetEnumVTypeSG()));
                enumVTypeGenerated.Save(Path.Combine(param.folder, "EnumVType" + param.compilerName + ".cs"));
            }
            {
                var treeNodeValueTypeGenerated = grammar.GenerateTreeNodeValueType();
                codeGenerator.ReportProgress(94, string.Format("自动生成\"{0}\"的代码!", grammar.GetTreeNodeValueSG()));
                treeNodeValueTypeGenerated.Save(Path.Combine(param.folder, "SyntaxTreeNodeValue" + param.compilerName + ".cs"));
            }
            {
                File.Copy("bitzhuwei.CompilerBase.dll", Path.Combine(param.folder, "bitzhuwei.CompilerBase.dll"), true);
                File.Copy("使用说明.txt", Path.Combine(param.folder, "使用说明.txt"), true);
                codeGenerator.ReportProgress(98, string.Format("复制基类库文件和使用说明文件!"));
            }
            {
                e.Result = param;
                codeGenerator.ReportProgress(100, "代码生成成功!");
            }
        }