Ejemplo n.º 1
0
        public ContextfreeProduction GetConflicted()
        {
            ContextfreeProduction result = null;

            foreach (var production in this.m_Grammar.ProductionCollection)
            {
                var firsts = new List <FIRSTCollectionItem>();
                foreach (var candidate in production.RightCollection)
                {
                    var first = this.GetItem(candidate);
                    if (first != null)
                    {
                        for (int i = 0; i < firsts.Count; i++)
                        {
                            foreach (var value in firsts[i].Value)
                            {
                                foreach (var value2 in first.Value)
                                {
                                    if (value.Equals(value2))
                                    {
                                        return(result);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取给定产生式对应的所有FIRST集
        /// </summary>
        /// <param name="production">产生式</param>
        /// <returns></returns>
        public IEnumerable <FIRSTCollectionItem> GetFIRSTCollectionItems(ContextfreeProduction production)
        {
            var items = from item in this
                        where item.ObjectiveProduction == production
                        select item;

            return(items.AsEnumerable());
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 判定给定的产生式能否推导出ε(空)
 /// </summary>
 /// <param name="production">产生式</param>
 /// <returns></returns>
 public bool CanInferNull(ContextfreeProduction production)
 {
     foreach (var candidate in production.RightCollection)
     {
         if (this.CanInferNull(candidate))
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取此上下文无关产生式的复制品
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            var result = new ContextfreeProduction();

            if (this.Left != null)
            {
                result.Left = this.Left.Clone() as ProductionNode;
            }
            if (this.RightCollection != null)
            {
                result.RightCollection = this.RightCollection.Clone() as RightSection;
            }
            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// From
        /// </summary>
        /// <param name="xContextfreeProduction"></param>
        /// <returns></returns>
        public static ContextfreeProduction From(XElement xContextfreeProduction)
        {
            if (xContextfreeProduction == null)
            {
                return(null);
            }
            if (xContextfreeProduction.Name != strContextfreeProduction)
            {
                return(null);
            }
            var result = new ContextfreeProduction();

            result.Left            = ProductionNode.From(xContextfreeProduction.Element(ProductionNode.strProductionNode));
            result.RightCollection = RightSection.From(xContextfreeProduction.Element(RightSection.strRightSection));
            return(result);
        }
Ejemplo n.º 6
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]);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 获取规范化之后的上下文无关文法
        /// <para>只合并左侧相同的产生式,不调整文法结构</para>
        /// </summary>
        /// <returns></returns>
        public ContextfreeGrammar Normalize()
        {
            var result = new ContextfreeGrammar();

            result.GrammarName = this.GrammarName;

            var startProduction = this.ProductionCollection[0].Dump();

            //this.ProductionCollection[0].Clone() as ContextfreeProduction<TEnumVType>;
            result.ProductionCollection.Add(startProduction);

            ContextfreeProduction production = null;

            for (int i = 1; i < this.ProductionCollection.Count; i++)
            {//将production加入文法中
                production = this.m_ProductionCollection[i];
                bool thisAdded = false;
                foreach (var added in result.ProductionCollection) //尝试并入原有产生式
                {
                    if (added.Left.Equals(production.Left))        //左部相同,应该合并
                    {
                        foreach (var waiting in production.RightCollection)
                        {//将新的候选式加入右部
                            if (!added.Contains(waiting))
                            {
                                added.RightCollection.Add(waiting);
                            }
                        }
                        thisAdded = true;
                    }
                }
                if (!thisAdded)//添加新的产生式
                {
                    result.ProductionCollection.Add(production);
                }
            }

            return(result);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// 创建一个文法的FOLLOW集中的一项
 /// <para>一项也是一个集合,其元素为产生式结点(只有叶结点)</para>
 /// </summary>
 /// <param name="production">指向的产生式</param>
 /// <param name="grammar">指向的文法</param>
 public FOLLOWCollectionItem(ContextfreeProduction production, ContextfreeGrammar grammar)
 {
     this.m_ObjectiveProduction = production;
     this.m_ObjectiveGrammar    = grammar;
 }
Ejemplo n.º 10
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;
 }