示例#1
0
 /// <summary>
 /// 添加元素到此FIRST集
 /// <para>若不包含此元素,则添加,并返回true</para>
 /// <para>若元素为null或已包含此元素或元素不是叶结点,则不会添加,并返回false</para>
 /// </summary>
 /// <param name="item"></param>
 /// <returns>若添加,返回true;若没有添加,返回false</returns>
 public bool Add(ProductionNode item)
 {
     if (item == null ||
         this.m_Value.Contains(item)
         //|| (item.Position != EnumProductionNodePosition.Leave && item.Position != EnumProductionNodePosition.My)
         )
     {
         return(false);
     }
     this.m_Value.Add(item);
     return(true);
 }
 /// <summary>
 /// 添加元素到此FOLLOW集
 /// <para>若不包含此元素,则添加,并返回true</para>
 /// <para>若元素为null或已包含此元素或元素不是叶结点或元素为ε(空结点),则不会添加,并返回false</para>
 /// </summary>
 /// <param name="item"></param>
 /// <returns>若添加,返回true;若没有添加,返回false</returns>
 public bool Add(ProductionNode item)
 {
     if (item == null ||
         this.m_Value.Contains(item) ||
         item.Position != EnumProductionNodePosition.Leave ||
         item == ProductionNode.tail_null
         )
     {
         return(false);
     }
     this.m_Value.Add(item);
     return(true);
 }
示例#3
0
 private bool IsCGKeyword(ProductionNode item)
 {
     if (item == ProductionNode.tail_null ||
         item == ProductionNode.tail_identifier ||
         item == ProductionNode.tail_number ||
         item == ProductionNode.tail_constString)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
示例#4
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);
        }
        /// <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);
        }
示例#6
0
        ///// <summary>
        ///// 设置给定行、列位置的推导式
        ///// </summary>
        ///// <param name="line"></param>
        ///// <param name="column"></param>
        ///// <param name="derivation"></param>
        //public void SetCell(int line, int column, Derivation derivation)
        //{
        //    if (0 <= line && line < this.m_LineCount
        //        && 0 <= column && column < this.m_ColumnCount)
        //    {
        //        //this.m_ParserMap[line, column].Add(derivation);
        //        string key = string.Format("{0}-{1}",)
        //    }
        //}
        /// <summary>
        /// 设置给定语法类型、单词类型所对应的推导式
        /// </summary>
        /// <param name="leftNode"></param>
        /// <param name="nextLeave"></param>
        /// <param name="function"></param>
        public void SetCell(ProductionNode leftNode, ProductionNode nextLeave, Derivation function)
        {
            //SetCell(this.m_LeftNodes[leftNode], this.m_NextLeaves[nextLeave], function);
            string         key  = string.Format("Left:{0} Terminal:{1}", leftNode, nextLeave);
            DerivationList list = null;

            if (this.m_ParserMap.TryGetValue(key, out list))
            {
                list.Add(function);
            }
            else
            {
                list = new DerivationList();
                list.Add(function);
                this.m_ParserMap.Add(key, list);
            }
        }
示例#7
0
 /// <summary>
 /// 获取<code>EnumVType</code>的一项的名称
 /// <para>constStringLeave</para>
 /// <para>VList</para>
 /// </summary>
 /// <param name="node">此项对应的结点</param>
 /// <returns></returns>
 public string GetEnumVTypeSGItem(ProductionNode node)
 {
     var result = string.Format("{0}", ConvertToIdentifier(node));
     if (node.Position == EnumProductionNodePosition.Leave)
     {
         if ('A' <= result[0] && result[0] <= 'Z')
             result = ((char)('a' - 'A' + result[0])) + result.Substring(1);
         if (!result.EndsWith("Leave"))
             result += "Leave";
         result = (IsCGKeyword(node) ? "" : GetKeywordPrefix4SyntaxTreeNodeLeave(node.Position)) + result;
     }
     else if (node.Position == EnumProductionNodePosition.NonLeave)
     {
         if (result.EndsWith("Leave"))
             result = result.Substring(0, result.Length - 5);
         result = (IsCGKeyword(node) ? "" : GetKeywordPrefix4SyntaxTreeNodeLeave(node.Position)) + result;
     }
     return result;
 }
示例#8
0
        /// <summary>
        /// From
        /// </summary>
        /// <param name="xProductionNode"></param>
        /// <returns></returns>
        public static ProductionNode From(XElement xProductionNode)
        {
            if (xProductionNode == null)
            {
                return(null);
            }
            if (xProductionNode.Name != strProductionNode)
            {
                return(null);
            }
            var result = new ProductionNode(
                xProductionNode.Attribute(strName).Value,
                xProductionNode.Attribute(strNote).Value,
                (EnumProductionNodePosition)Enum.Parse(
                    typeof(EnumProductionNodePosition),
                    xProductionNode.Attribute(strPosition).Value));

            return(result);
        }
示例#9
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);
        }
示例#10
0
        /// <summary>
        /// 获取推导式列表
        /// </summary>
        /// <param name="leftNode">当前结非终点类型</param>
        /// <param name="nextLeave">要处理的终结点类型</param>
        /// <returns></returns>
        public DerivationList GetDerivationList(ProductionNode leftNode, ProductionNode nextLeave)
        {
            //#if DEBUG
            //            if (this.m_LeftNodes.ContainsKey(leftNode)
            //                && this.m_NextLeaves.ContainsKey(nextLeave))
            //#endif
            //                return this.GetDerivationList(this.m_LeftNodes[leftNode], this.m_NextLeaves[nextLeave]);
            //#if DEBUG
            //            else
            //                return null;
            //#endif
            //                return this.GetDerivationList(this.m_LeftNodes[leftNode], this.m_NextLeaves[nextLeave]);
            string         key  = string.Format("Left:{0} Terminal:{1}", leftNode, nextLeave);
            DerivationList list = null;

            if (this.m_ParserMap.TryGetValue(key, out list))
            {
                return(this.m_ParserMap[key]);
            }
            else
            {
                return(new DerivationList());
            }
        }
示例#11
0
 /// <summary>
 /// 设置某列的结点类型
 /// </summary>
 /// <param name="column"></param>
 /// <param name="nextLeave"></param>
 public void SetColumn(int column, ProductionNode nextLeave)
 {
     //if (0 <= column && column < this.m_ColumnCount)
     //    this.m_NextLeaves.Add(nextLeave, column);
 }
示例#12
0
 /// <summary>
 /// 设置某行的结点类型
 /// </summary>
 /// <param name="line"></param>
 /// <param name="leftNode"></param>
 public void SetLine(int line, ProductionNode leftNode)
 {
     //if (0 <= line && line < this.m_LineCount)
     //    this.m_LeftNodes.Add(leftNode, line);
 }
示例#13
0
 /// <summary>
 /// 对 叶结点<code>leave</code>进行分析
 /// </summary>
 /// <param name="leave">叶结点</param>
 /// <returns></returns>
 public string GetFieldFuncParseName(ProductionNode leave)
 {
     //return string.Format("FuncParse{0}_", ConvertToIdentifier(leave));
     return string.Format("FuncParse{0}_", GetEnumVTypeSGItem(leave));
 }
示例#14
0
 /// <summary>
 /// 获取用于堆栈操作的一个字段
 /// <para>m_constStringLeave</para>
 /// <para>m_VList</para>
 /// </summary>
 /// <param name="node"></param>
 /// <returns></returns>
 public string GetFieldStackItem(ProductionNode node)
 {
     var result = string.Format("m_{0}", GetEnumVTypeSGItem(node));
     return result;
 }
示例#15
0
        /// <summary>
        /// 获取此结点的复制品
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            var result = new ProductionNode(m_NodeName, m_NodeNote, m_Position);

            return(result);
        }