Пример #1
0
        /// <summary>
        /// 11 &lt;Vt&gt; ::= "null";
        /// <para>12 &lt;Vt&gt; ::= "identifier";</para>
        /// <para>13 &lt;Vt&gt; ::= "number";</para>
        /// <para>14 &lt;Vt&gt; ::= "constString";</para>
        /// <para>15 &lt;Vt&gt; ::= identifier;</para>
        /// <para>16 &lt;Vt&gt; ::= number;</para>
        /// <para>17 &lt;Vt&gt; ::= constString;</para>
        /// </summary>
        /// <param name="syntaxTree"></param>
        private static ProductionNode GetGrammarVt(SyntaxTree <EnumTokenTypeCG, EnumVTypeCG, TreeNodeValueCG> syntaxTree)
        {//<Vt> ::= "null" | "identifier" | "number" | "constString" | identifier | number | constString; // 11 12 13 14 15 16 17
            ProductionNode result = null;

            if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_Vt___tail_nullLeave())
            {
                result = ProductionNode.tail_null;
            }
            else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_Vt___tail_identifierLeave())
            {
                result = ProductionNode.tail_identifier;
            }
            else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_Vt___tail_numberLeave())
            {
                result = ProductionNode.tail_number;
            }
            else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_Vt___tail_constStringLeave())
            {
                result = ProductionNode.tail_constString;
            }
            else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_Vt___constStringLeave())
            {
                var name = GetGrammarconstString(syntaxTree.Children[0]);
                result = new ProductionNode(name, name, EnumProductionNodePosition.Leave);
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        /// 7 &lt;VOpt&gt; ::= &lt;V&gt; &lt;VOpt&gt;;
        /// <para>8 &lt;VOpt&gt; ::= "|" &lt;V&gt; &lt;VOpt&gt;;</para>
        /// <para>9 &lt;VOpt ::= null;</para>
        /// </summary>
        /// <param name="vlist"></param>
        /// <param name="candidate"></param>
        /// <param name="syntaxTree"></param>
        private static object GetGrammarVOpt(RightSection vlist,
                                             ProductionNodeList candidate, SyntaxTree <EnumTokenTypeCG, EnumVTypeCG, TreeNodeValueCG> syntaxTree)
        {//<VOpt> ::= <V> <VOpt> | "|" <V> <VOpt> | null; // 7 8 9
            if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___tail_nullLeave() ||
                syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___tail_identifierLeave() ||
                syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___tail_numberLeave() ||
                syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___tail_constStringLeave() ||
                syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___constStringLeave() ||
                syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___tail_lessThan_Leave()
                )
            {
                var v = GetGrammarV(syntaxTree.Children[0]);
                candidate.Add(v);

                return(GetGrammarVOpt(vlist, candidate, syntaxTree.Children[1]));
            }
            else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___tail_or_Leave())
            {
                ProductionNodeList newCandidate = new ProductionNodeList();
                vlist.Add(newCandidate);

                var v = GetGrammarV(syntaxTree.Children[1]);
                newCandidate.Add(v);

                return(GetGrammarVOpt(vlist, newCandidate, syntaxTree.Children[2]));
            }
            else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___tail_semicolon_Leave())
            {
                return(GetGrammarnull(syntaxTree.Children[0]));
            }
            else
            {
                return(string.Format("{0}", syntaxTree.CandidateFunc.ToString()));
            }
        }
Пример #3
0
 /// <summary>
 /// 11 &lt;Vt&gt; ::= "null";
 /// <para>12 &lt;Vt&gt; ::= "identifier";</para>
 /// <para>13 &lt;Vt&gt; ::= "number";</para>
 /// <para>14 &lt;Vt&gt; ::= "constString";</para>
 /// <para>15 &lt;Vt&gt; ::= constString;</para>
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="syntaxTree"></param>
 private static void GetFormattedVt(StringBuilder builder, SyntaxTree <EnumTokenTypeCG, EnumVTypeCG, TreeNodeValueCG> syntaxTree)
 {//<Vt> ::= "null" | "identifier" | "number" | "constString" | constString; // 11 12 13 14 15
     if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_Vt___tail_nullLeave())
     {
         builder.Append(ProductionNode.tail_null.NodeName);
         //GetFormattednull(builder, syntaxTree.Children[0]);
     }
     else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_Vt___tail_identifierLeave())
     {
         builder.Append(ProductionNode.tail_identifier.NodeName);
         //GetFormattedidentifierLeave(builder, syntaxTree.Children[0]);
     }
     else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_Vt___tail_numberLeave())
     {
         builder.Append(ProductionNode.tail_number.NodeName);
         //GetFormattednumberLeave(builder, syntaxTree.Children[0]);
     }
     else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_Vt___tail_constStringLeave())
     {
         builder.Append(ProductionNode.tail_constString.NodeName);
         //GetFormattedconstString(builder, syntaxTree.Children[0]);
     }
     else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_Vt___constStringLeave())
     {
         GetFormattedconstString(builder, syntaxTree.Children[0]);
     }
 }
Пример #4
0
 /// <summary>
 /// 5 &lt;V&gt; ::= &lt;Vn&gt;;
 /// <para>6 &lt;V&gt; ::= &lt;Vt&gt;;</para>
 /// <para>7 &lt;V&gt; ::= identifier;</para>
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="syntaxTree"></param>
 private static void GetFormattedV(StringBuilder builder, SyntaxTree <EnumTokenTypeCG, EnumVTypeCG, TreeNodeValueCG> syntaxTree)
 {//<V> ::= <Vn> | <Vt> | identifier; 5 6 7
     if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_V___tail_lessThan_Leave())
     {
         GetFormattedVn(builder, syntaxTree.Children[0]);
     }
     else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_V___constStringLeave() ||
              syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_V___tail_constStringLeave() ||
              syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_V___tail_identifierLeave() ||
              syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_V___tail_nullLeave() ||
              syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_V___tail_numberLeave())
     {
         GetFormattedVt(builder, syntaxTree.Children[0]);
     }
     else
     {
         builder.Append(string.Format("error: {0} not matching [{1}] or [{2}] or [{3}] or [{4}] or [{5}] or [{6}]",
                                      syntaxTree.Children[0].CandidateFunc,
                                      LL1SyntaxParserCG.GetFuncParsecase_V___tail_lessThan_Leave(),
                                      LL1SyntaxParserCG.GetFuncParsecase_V___constStringLeave(),
                                      LL1SyntaxParserCG.GetFuncParsecase_V___tail_constStringLeave(),
                                      LL1SyntaxParserCG.GetFuncParsecase_V___tail_identifierLeave(),
                                      LL1SyntaxParserCG.GetFuncParsecase_V___tail_nullLeave(),
                                      LL1SyntaxParserCG.GetFuncParsecase_V___tail_numberLeave()));
     }
 }
Пример #5
0
 /// <summary>
 /// 7 &lt;VOpt&gt; ::= &lt;V&gt; &lt;VOpt&gt;;
 /// <para>8 &lt;VOpt&gt; ::= "|" &lt;V&gt; &lt;VOpt&gt;;</para>
 /// <para>9 &lt;VOpt ::= null;</para>
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="syntaxTree"></param>
 private static void GetFormattedVOpt(StringBuilder builder, SyntaxTree <EnumTokenTypeCG, EnumVTypeCG, TreeNodeValueCG> syntaxTree)
 {//<VOpt> ::= <V> <VOpt> | "|" <V> <VOpt> | null; // 7 8 9
     if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___constStringLeave() ||
         syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___tail_constStringLeave() ||
         syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___tail_identifierLeave() ||
         syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___tail_lessThan_Leave() ||
         syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___tail_nullLeave() ||
         syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___tail_numberLeave())
     {
         builder.Append(" ");
         GetFormattedV(builder, syntaxTree.Children[0]);
         //builder.Append(" ");
         GetFormattedVOpt(builder, syntaxTree.Children[1]);
     }
     else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___tail_or_Leave())
     {
         builder.Append(" ");
         GetFormattedorLeave(builder, syntaxTree.Children[0]);
         builder.Append(" ");
         GetFormattedV(builder, syntaxTree.Children[1]);
         //builder.Append(" ");
         GetFormattedVOpt(builder, syntaxTree.Children[2]);
     }
     else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VOpt___tail_semicolon_Leave())
     {
         GetFormattednull(builder, syntaxTree.Children[0]);
     }
 }
Пример #6
0
        static void Main(string[] args)
        {
            //var sourceCode =
            //    "<Start> ::= <Value> <RightOpt>;"
            //    +"<Value> ::= number | identifier;"
            //    +"<RightOpt> ::= <Opt> <Value> <RightOpt> | null;"
            //    +"<Opt> ::= \"Add\" | \"-\";";

            var sourceCode =
                "<Start>  ::= <Vn> \"::=\" <VList> \";\" <PList>;"
                + "<PList>  ::= <Vn> \"::=\" <VList> \";\" <PList> | null;"
                + "<VList>  ::= <V> <VOpt>;"
                + "<V>      ::= <Vn> | <Vt>;"
                + "<VOpt>   ::= <V> <VOpt> | \"|\" <V> <VOpt> | null;"
                + "<Vn>     ::= \"<\" identifier \">\";"
                + "<Vt>     ::= \"null\" | \"identifier\" | \"number\" | \"constString\" | constString;";

            var lex = new LexicalAnalyzerCG();

            lex.SetSourceCode(sourceCode);
            var tokens = lex.Analyze();

            Console.WriteLine(tokens);
            var parser = new LL1SyntaxParserCG();

            parser.SetTokenListSource(tokens);
            var tree = parser.Parse();

            Console.WriteLine(tree);
            var grammar = tree.GetGrammar();

            Console.WriteLine(grammar);
        }
Пример #7
0
 private static void _GetFormattedSourceCode(StringBuilder builder, SyntaxTree <EnumTokenTypeCG, EnumVTypeCG, TreeNodeValueCG> tree)
 {
     if (tree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_Start___tail_lessThan_Leave())
     {//<Start> ::= <Vn> "::=" <VList> ";" <PList>; 1
         GetFormattedVn(builder, tree.Children[0]);
         builder.Append(" ");
         GetFormattedpointToLeave(builder, tree.Children[1]);
         builder.Append(" ");
         GetFormattedVList(builder, tree.Children[2]);
         GetFormattedsemicolonLeave(builder, tree.Children[3]);
         builder.AppendLine();
         GetFormattedPList(builder, tree.Children[4]);
     }
     else
     {
         builder.Append(string.Format("error: ProductionNodeList {0} not found", tree.CandidateFunc));
     }
 }
Пример #8
0
 /// <summary>
 /// 2 &lt;PList&gt; ::= &lt;Vn&gt; "::=" &lt;VList&gt; ";" &lt;PList&gt;;
 /// <para>3 &lt;PList&gt; ::= null;</para>
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="syntaxTree"></param>
 private static void GetFormattedPList(StringBuilder builder, SyntaxTree <EnumTokenTypeCG, EnumVTypeCG, TreeNodeValueCG> syntaxTree)
 {//<PList> ::= <Vn> "::=" <VList> ";" <PList> | null; 2 3
     if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_PList___tail_lessThan_Leave())
     {
         GetFormattedVn(builder, syntaxTree.Children[0]);
         builder.Append(" ");
         GetFormattedpointToLeave(builder, syntaxTree.Children[1]);
         builder.Append(" ");
         GetFormattedVList(builder, syntaxTree.Children[2]);
         GetFormattedsemicolonLeave(builder, syntaxTree.Children[3]);
         builder.AppendLine();
         GetFormattedPList(builder, syntaxTree.Children[4]);
     }
     else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_PList___tail_startEndLeave())
     {
         // nothing to do
         //GetFormattednull(builder, syntaxTree.Children[0]);
     }
 }
Пример #9
0
        /// <summary>
        /// 2 &lt;PList&gt; ::= &lt;Vn&gt; "::=" &lt;VList&gt; ";" &lt;PList&gt;;
        /// <para>3 &lt;PList&gt; ::= null;</para>
        /// </summary>
        /// <param name="plist"></param>
        /// <param name="syntaxTree"></param>
        private static void GetGrammarPList(
            ContextfreeProductionList plist, SyntaxTree <EnumTokenTypeCG, EnumVTypeCG, TreeNodeValueCG> syntaxTree)
        {//<PList> ::= <Vn> "::=" <VList> ";" <PList> | null; 2 3
            if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_PList___tail_lessThan_Leave())
            {
                ContextfreeProduction production = new ContextfreeProduction();
                plist.Add(production);

                var vn = GetGrammarVn(syntaxTree.Children[0]);
                production.Left = vn;

                var vlist = GetGrammarVList(syntaxTree.Children[2]);
                production.RightCollection = vlist;

                GetGrammarPList(plist, syntaxTree.Children[4]);
            }
            else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_PList___tail_startEndLeave())
            {
                // nothing to do
                //GetGrammarnull(syntaxTree.Children[0]);
            }
        }
Пример #10
0
        /// <summary>
        /// 5 &lt;V&gt; ::= &lt;Vn&gt;;
        /// <para>6 &lt;V&gt; ::= &lt;Vt&gt;;</para>
        /// <para>7 &lt;V&gt; ::= identifier;</para>
        /// </summary>
        /// <param name="syntaxTree"></param>
        private static ProductionNode GetGrammarV(SyntaxTree <EnumTokenTypeCG, EnumVTypeCG, TreeNodeValueCG> syntaxTree)
        {//<V> ::= <Vn> | <Vt>; 5 6
            ProductionNode result = null;

            if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_V___tail_lessThan_Leave())
            {
                result = GetGrammarVn(syntaxTree.Children[0]);
            }
            else if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_V___constStringLeave() ||
                     syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_V___tail_constStringLeave() ||
                     syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_V___tail_identifierLeave() ||
                     syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_V___tail_nullLeave() ||
                     syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_V___tail_numberLeave())
            {
                result = GetGrammarVt(syntaxTree.Children[0]);
            }
            else
            {
                result = new ProductionNode(EnumVTypeCG.case_V.ToString(), EnumVTypeCG.case_V.ToString(), EnumProductionNodePosition.Unknown);
            }
            return(result);
        }
Пример #11
0
        /// <summary>
        /// 4 &lt;VList&gt; ::= &lt;V&gt; &lt;VOpt&gt;;
        /// </summary>
        /// <param name="syntaxTree"></param>
        private static RightSection GetGrammarVList(SyntaxTree <EnumTokenTypeCG, EnumVTypeCG, TreeNodeValueCG> syntaxTree)
        {//<VList> ::= <V> <VOpt>; 4
            RightSection vlist = new RightSection();

            if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VList___constStringLeave() ||
                syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VList___tail_constStringLeave() ||
                syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VList___tail_identifierLeave() ||
                syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VList___tail_lessThan_Leave() ||
                syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VList___tail_nullLeave() ||
                syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_VList___tail_numberLeave())
            {
                var candidate = new ProductionNodeList();
                vlist.Add(candidate);

                var v = GetGrammarV(syntaxTree.Children[0]);
                candidate.Add(v);

                var info = GetGrammarVOpt(vlist, candidate, syntaxTree.Children[1]);
            }

            return(vlist);
        }
Пример #12
0
        private static ContextfreeGrammar _GetGrammar(SyntaxTree <EnumTokenTypeCG, EnumVTypeCG, TreeNodeValueCG> syntaxTree)
        {
            ContextfreeGrammar result = null;

            if (syntaxTree.CandidateFunc == LL1SyntaxParserCG.GetFuncParsecase_Start___tail_lessThan_Leave())
            {//<Start> ::= <Vn> "::=" <VList> ";" <PList>; 1
                result = new ContextfreeGrammar();

                var startProduction = new ContextfreeProduction();
                result.ProductionCollection.Add(startProduction);

                var left = GetGrammarVn(syntaxTree.Children[0]);
                startProduction.Left = left;
                result.GrammarName   = left.NodeName;

                var vlist = GetGrammarVList(syntaxTree.Children[2]);
                startProduction.RightCollection = vlist;

                GetGrammarPList(result.ProductionCollection, syntaxTree.Children[4]);
            }

            return(result);
        }