/// <summary> /// 11 <Vt> ::= "null"; /// <para>12 <Vt> ::= "identifier";</para> /// <para>13 <Vt> ::= "number";</para> /// <para>14 <Vt> ::= "constString";</para> /// <para>15 <Vt> ::= identifier;</para> /// <para>16 <Vt> ::= number;</para> /// <para>17 <Vt> ::= 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); }
/// <summary> /// 7 <VOpt> ::= <V> <VOpt>; /// <para>8 <VOpt> ::= "|" <V> <VOpt>;</para> /// <para>9 <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())); } }
/// <summary> /// 11 <Vt> ::= "null"; /// <para>12 <Vt> ::= "identifier";</para> /// <para>13 <Vt> ::= "number";</para> /// <para>14 <Vt> ::= "constString";</para> /// <para>15 <Vt> ::= 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]); } }
/// <summary> /// 5 <V> ::= <Vn>; /// <para>6 <V> ::= <Vt>;</para> /// <para>7 <V> ::= 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())); } }
/// <summary> /// 7 <VOpt> ::= <V> <VOpt>; /// <para>8 <VOpt> ::= "|" <V> <VOpt>;</para> /// <para>9 <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]); } }
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); }
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)); } }
/// <summary> /// 2 <PList> ::= <Vn> "::=" <VList> ";" <PList>; /// <para>3 <PList> ::= 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]); } }
/// <summary> /// 2 <PList> ::= <Vn> "::=" <VList> ";" <PList>; /// <para>3 <PList> ::= 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]); } }
/// <summary> /// 5 <V> ::= <Vn>; /// <para>6 <V> ::= <Vt>;</para> /// <para>7 <V> ::= 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); }
/// <summary> /// 4 <VList> ::= <V> <VOpt>; /// </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); }
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); }