/// <summary> /// 用于生成编译器的必要输入数据 /// <para>使用此类型可避免重复计算,以节省时间</para> /// </summary> /// <param name="grammar">要生成编译器的文法</param> public LL1GeneraterInput(ContextfreeGrammar grammar) { this.m_TerminalList = grammar.GetTerminalList(); this.m_NonTerminalList = grammar.GetNonterminalList(); this.m_firstCollectionInput = grammar.GetFirstCollection(); this.m_followCollectionInput = grammar.GetFollowCollection(); this.m_ll1ParserMapInput = grammar.GetLL1ParserMap(); }
/// <summary> /// 获取此上下文无关文法的复制品 /// </summary> /// <returns></returns> public object Clone() { var result = new ContextfreeGrammar(); result.m_GrammarName = this.m_GrammarName; result.m_namespace = this.m_namespace; result.m_ProductionCollection = this.m_ProductionCollection.Clone() as ContextfreeProductionList; return(result); }
/// <summary> /// 从<code>XElement</code>中读取一个<code>ContextfreeGrammar</code>对象 /// </summary> /// <param name="xContextfreeGrammar"></param> /// <returns></returns> public static ContextfreeGrammar From(XElement xContextfreeGrammar) { if (xContextfreeGrammar == null) { return(null); } if (xContextfreeGrammar.Name != strContextfreeGrammar) { return(null); } var result = new ContextfreeGrammar(); result.GrammarName = xContextfreeGrammar.Attribute(strGrammarName).Value; result.Namespace = xContextfreeGrammar.Attribute(strNamespace).Value; result.ProductionCollection = ContextfreeProductionList.From( xContextfreeGrammar.Element(ContextfreeProductionList.strContextfreeProductionList)); return(result); }
/// <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); }
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); }
/// <summary> /// 创建文法的FIRST集 /// </summary> /// <param name="grammar">对应的文法</param> public FIRSTCollection(ContextfreeGrammar grammar) { this.m_Grammar = grammar; }
/// <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; }
/// <summary> /// 创建文法的FOLLOW集 /// </summary> /// <param name="grammar">对应的文法</param> public FOLLOWCollection(ContextfreeGrammar grammar) { this.m_Grammar = grammar; }
/// <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; }