Ejemplo n.º 1
0
        /// <summary>
        /// 判定给定的候选式能否推导出ε(空)
        /// </summary>
        /// <param name="candidate"></param>
        /// <returns></returns>
        public bool CanInferNull(ProductionNodeList candidate)
        {
            foreach (var node in candidate)
            {
                if (node.Position == EnumProductionNodePosition.Leave)
                {
                    if (node != ProductionNode.tail_null)
                    {
                        return(false);
                    }
                }
                else if (node.Position == EnumProductionNodePosition.Unknown)
                {
                    throw new Exception(string.Format("文法中存在不合法的候选式{0}", candidate));
                }
            }

            foreach (var node in candidate)
            {
                if (node == ProductionNode.tail_null)
                {
                    continue;
                }

                var production = this.GetProduction(node);
                //AppendSpaces(builder, prefixSpace);
                //builder.AppendLine(string.Format("parse next production: {0}", production));
                if (!this.CanInferNull(production))
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 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()));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 获取此产生式结点列表(候选式)的复制品
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            var result = new ProductionNodeList();

            result.AddRange(
                from item in this
                select(item.Clone() as ProductionNode));
            return(result);
        }
Ejemplo n.º 4
0
 public object GetEnumVTypeSGItem(ProductionNodeList productionNodeList)
 {
     var result = new StringBuilder();
     foreach (var item in productionNodeList)
     {
         result.Append(GetEnumVTypeSGItem(item));
     }
     return result.ToString();
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 获取与候选式对应的FIRST集的项
 /// </summary>
 /// <param name="candidate"></param>
 /// <returns></returns>
 public FIRSTCollectionItem GetItem(ProductionNodeList candidate)
 {
     foreach (var item in this)
     {
         if (item.ObjectiveCandidate == candidate)
         {
             return(item);
         }
     }
     return(null);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 获取推导式
 /// </summary>
 /// <param name="left">左部</param>
 /// <param name="right">右部</param>
 public Derivation(ProductionNode left, ProductionNodeList right)
 {
     if (left != null)
     {
         this.Left = left;
     }
     if (right != null)
     {
         this.Right = right;
     }
 }
Ejemplo n.º 7
0
 /// <summary>
 /// 判定此产生式右部是否包含给定的候选式
 /// </summary>
 /// <param name="candidate"></param>
 /// <returns></returns>
 public bool Contains(ProductionNodeList candidate)
 {
     if (candidate == null)
     {
         return(false);
     }
     foreach (var originalCandidate in this.RightCollection)
     {
         if (originalCandidate.Equals(candidate))
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 8
0
        /// <summary>
        /// From
        /// </summary>
        /// <param name="xDerivation"></param>
        /// <returns></returns>
        public static Derivation From(XElement xDerivation)
        {
            if (xDerivation == null)
            {
                return(null);
            }
            if (xDerivation.Name != strDerivation)
            {
                return(null);
            }
            var result = new Derivation();

            result.Left  = ProductionNode.From(xDerivation.Element(strLeft));
            result.Right = ProductionNodeList.From(xDerivation.Element(strRight));
            return(result);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// From
        /// </summary>
        /// <param name="xProductionNodeList"></param>
        /// <returns></returns>
        public static ProductionNodeList From(XElement xProductionNodeList)
        {
            if (xProductionNodeList == null)
            {
                return(null);
            }
            if (xProductionNodeList.Name != strCandidate)
            {
                return(null);
            }
            var result = new ProductionNodeList();

            result.AddRange(
                from item in xProductionNodeList.Elements(ProductionNode.strProductionNode)
                select ProductionNode.From(item)
                );
            return(result);
        }
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// 创建一个文法的FIRST集中的一项
 /// <para>一项也是一个集合,其元素为产生式结点(只有叶结点)</para>
 /// </summary>
 /// <param name="candiate">指向的候选式</param>
 /// <param name="production">指向的产生式</param>
 /// <param name="grammar">指向的文法</param>
 public FIRSTCollectionItem(ProductionNodeList candiate, ContextfreeProduction production, ContextfreeGrammar grammar)
 {
     this.m_ObjectiveCandidate  = candiate;
     this.m_ObjectiveProduction = production;
     this.m_ObjectiveGrammar    = grammar;
 }