Example #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);
        }
Example #2
0
        private ContextfreeGrammar TestGetCGGrammar()
        {
            var grammar = new ContextfreeGrammar();

            grammar.Namespace   = "bitzhuwei.CGCompiler";
            grammar.GrammarName = "CG";

            var Start = new ProductionNode("Start", EnumProductionNodePosition.NonLeave);
            var PList = new ProductionNode("PList", EnumProductionNodePosition.NonLeave);
            var VList = new ProductionNode("VList", EnumProductionNodePosition.NonLeave);
            var V     = new ProductionNode("V", EnumProductionNodePosition.NonLeave);
            var VOpt  = new ProductionNode("VOpt", EnumProductionNodePosition.NonLeave);
            var Vn    = new ProductionNode("Vn", EnumProductionNodePosition.NonLeave);
            var Vt    = new ProductionNode("Vt", EnumProductionNodePosition.NonLeave);

            var colon_Colon_Equality_ = new ProductionNode("\"::=\"", EnumProductionNodePosition.Leave);
            var semicolon_            = new ProductionNode("\";\"", EnumProductionNodePosition.Leave);
            var or_               = new ProductionNode("\"|\"", EnumProductionNodePosition.Leave);
            var lessThan_         = new ProductionNode("\"<\"", EnumProductionNodePosition.Leave);
            var greaterThan_      = new ProductionNode("\">\"", EnumProductionNodePosition.Leave);
            var token_null        = new ProductionNode("\"null\"", EnumProductionNodePosition.Leave);
            var token_identifier  = new ProductionNode("\"identifier\"", EnumProductionNodePosition.Leave);
            var token_number      = new ProductionNode("\"number\"", EnumProductionNodePosition.Leave);
            var token_constString = new ProductionNode("\"constString\"", EnumProductionNodePosition.Leave);

            {
                var startProd = new ContextfreeProduction();
                startProd.Left = Start;
                var candidate = new ProductionNodeList();
                candidate.Add(Vn); candidate.Add(colon_Colon_Equality_); candidate.Add(VList); candidate.Add(semicolon_); candidate.Add(PList);
                startProd.RightCollection.Add(candidate);

                grammar.ProductionCollection.Add(startProd);
            }

            {
                var PListProd = new ContextfreeProduction();
                PListProd.Left = PList;
                var candidate = new ProductionNodeList();
                candidate.Add(Vn); candidate.Add(colon_Colon_Equality_); candidate.Add(VList); candidate.Add(semicolon_); candidate.Add(PList);
                PListProd.RightCollection.Add(candidate);
                candidate = new ProductionNodeList();
                candidate.Add(ProductionNode.tail_null);
                PListProd.RightCollection.Add(candidate);

                grammar.ProductionCollection.Add(PListProd);
            }

            {
                var VListProd = new ContextfreeProduction();
                VListProd.Left = VList;
                var candidate = new ProductionNodeList();
                candidate.Add(V); candidate.Add(VOpt);
                VListProd.RightCollection.Add(candidate);

                grammar.ProductionCollection.Add(VListProd);
            }

            {
                var VProd = new ContextfreeProduction();
                VProd.Left = V;
                var candidate = new ProductionNodeList();
                candidate.Add(Vn);
                VProd.RightCollection.Add(candidate);
                candidate = new ProductionNodeList();
                candidate.Add(Vt);
                VProd.RightCollection.Add(candidate);

                grammar.ProductionCollection.Add(VProd);
            }

            {
                var VOptProd = new ContextfreeProduction();
                VOptProd.Left = VOpt;
                var candidate = new ProductionNodeList();
                candidate.Add(V); candidate.Add(VOpt);
                VOptProd.RightCollection.Add(candidate);
                candidate = new ProductionNodeList();
                candidate.Add(or_); candidate.Add(V); candidate.Add(VOpt);
                VOptProd.RightCollection.Add(candidate);
                candidate = new ProductionNodeList();
                candidate.Add(ProductionNode.tail_null);
                VOptProd.RightCollection.Add(candidate);

                grammar.ProductionCollection.Add(VOptProd);
            }

            {
                var VnProd = new ContextfreeProduction();
                VnProd.Left = Vn;
                var candidate = new ProductionNodeList();
                candidate.Add(lessThan_); candidate.Add(ProductionNode.tail_identifier); candidate.Add(greaterThan_);
                VnProd.RightCollection.Add(candidate);

                grammar.ProductionCollection.Add(VnProd);
            }

            {
                var VtProd = new ContextfreeProduction();
                VtProd.Left = Vt;

                var candidate = new ProductionNodeList();
                candidate.Add(token_null);
                VtProd.RightCollection.Add(candidate);

                candidate = new ProductionNodeList();
                candidate.Add(token_identifier);
                VtProd.RightCollection.Add(candidate);

                candidate = new ProductionNodeList();
                candidate.Add(token_number);
                VtProd.RightCollection.Add(candidate);

                candidate = new ProductionNodeList();
                candidate.Add(token_constString);
                VtProd.RightCollection.Add(candidate);

                candidate = new ProductionNodeList();
                candidate.Add(ProductionNode.tail_identifier);
                VtProd.RightCollection.Add(candidate);

                candidate = new ProductionNodeList();
                candidate.Add(ProductionNode.tail_number);
                VtProd.RightCollection.Add(candidate);

                candidate = new ProductionNodeList();
                candidate.Add(ProductionNode.tail_constString);
                VtProd.RightCollection.Add(candidate);

                grammar.ProductionCollection.Add(VtProd);
            }

            return(grammar);
        }
Example #3
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, "代码生成成功!");
            }
        }