private RuleElement GotoLastRule(RuleElement re) { while (re.GetNext() != null) { re = re.GetNext(); } return(re); }
protected virtual MyArrayList GenerateFirstSet(RuleStart rs) { MyArrayList res = new MyArrayList(); RuleElement re = null; if (rs != null) { re = rs.GetNext(); } else { return(null); } while (re != null) { if (re.GetToken().Equals("")) { re = re.GetNext(); } else { if (re.IsTerminal()) { res.Add(re); return(res); } else { RuleToken rt = (RuleToken)re; RuleStart conStr = GoToFirstStartRule(rt.GetConnected()); MyArrayList res2 = null; while (conStr != null) { res2 = GenerateFirstSet(conStr); res = ParseHelper.UnionSet(res, res2); conStr = conStr.GetAlternate(); } if (TotalRuleEpsFree(rt.GetConnected())) { return(res); } else { re = re.GetNext(); } } } } return(res); }
public string GetAllElements() { RuleElement re = m_Next; string Elements = ""; while (re != null) { if (re.GetToken().Length > 0) { if (re.IsTerminal()) { Elements += "\'" + re.GetToken() + "\',"; } else { Elements += re.GetToken() + ","; } } else { Elements += " ,"; } re = re.GetNext(); } return(Elements); }
public string GetRuleString(RuleElement Start, int PntPos) { string res = Start.GetToken() + "->"; RuleElement iterator = Start.GetNext(); int Pos = 1; while (iterator != null) { if (PntPos == Pos) { res += "."; } if (iterator.IsTerminal()) { res += "'" + iterator.GetToken() + "'"; } else { res += iterator.GetToken(); } iterator = iterator.GetNext(); if (iterator != null) { res += ","; } Pos++; } if (PntPos == Pos) { res += "."; } res += ";"; return(res); }
protected bool BetaTotalRuleEpsFree(RuleElement re) { while (re != null) { if (re.IsTerminal()) { return(true); } else { if (re.GetToken().Length > 0) { RuleToken rt = (RuleToken)re; if (TotalRuleEpsFree(rt.GetConnected())) { return(true); } } else { return(false); } } re = re.GetNext(); } return(false); }
protected MyArrayList BetaTotalRuleFirstSet(RuleElement re) { MyArrayList BetaFirst = new MyArrayList(); while (re != null) { if (re.IsTerminal()) { BetaFirst.Add(re); return(BetaFirst); } else { if (re.GetToken().Length > 0) { RuleToken rt = (RuleToken)re; if (rt != null) { RuleStart conStr = GoToFirstStartRule(rt.GetConnected()); MyArrayList FirstSet = TotalRuleFirstSet(conStr); BetaFirst = ParseHelper.UnionSet(BetaFirst, FirstSet); if (TotalRuleEpsFree(rt.GetConnected())) { return(BetaFirst); } } } } re = re.GetNext(); } return(BetaFirst); }
private MyArrayList GenerateFollowList(MyArrayList NonTerms) { MyArrayList Follows = new MyArrayList(); for (int i = 0; i < NonTerms.Count; i++) { RuleElement re = (RuleElement)NonTerms[i]; RuleStart rs = re.GetBegin(); BNFRule rl = rs.GetRuleConnect(); RuleElement nextRe = re.GetNext(); if (nextRe == null) { Follows = ParseHelper.UnionSet(Follows, rl.FollowSet); } else { if (nextRe.IsTerminal()) { Follows.Add(nextRe); } else { RuleToken rt = (RuleToken)nextRe; MyArrayList AllFirstSet = BetaTotalRuleFirstSet(rt); Follows = ParseHelper.UnionSet(Follows, AllFirstSet); if (!BetaTotalRuleEpsFree(rt)) { Follows = ParseHelper.UnionSet(Follows, rl.FollowSet); } } } } return(Follows); }
public int RuleLength() { int rl_len = 0; RuleElement iterator = Start.GetNext(); while (iterator != null) { rl_len++; iterator = iterator.GetNext(); } return(rl_len); }
private string[] Rule2String(RuleStart rs) { MyArrayList elements = new MyArrayList(); if (rs != null) { RuleElement re = rs.GetNext(); while (re != null) { elements.Add(re.GetToken()); re = re.GetNext(); } } return((string[])elements.ToArray(typeof(string))); }
public MyArrayList GetAllTokens() { MyArrayList tokens = new MyArrayList(); RuleElement re = m_Next; while (re != null) { if (!re.IsTerminal() && !re.GetToken().Equals("")) { tokens.Add(re); } re = re.GetNext(); } return(tokens); }
private MyArrayList Rules2Array(RuleStart rs) { MyArrayList invRules = new MyArrayList(); if (rs != null) { RuleElement re = rs.GetNext(); while (re != null) { invRules.Add(re); re = re.GetNext(); } } return(invRules); }
public RuleElement GetRulePos(int pos) { RuleElement re = GetStart(); for (int i = 0; i < pos; i++) { if (re != null) { re = re.GetNext(); } else { break; } } return(re); }
protected virtual bool RuleIsEpsilonFree(RuleStart rs) { RuleElement re = null; if (rs != null) { re = rs.GetNext(); } else { return(false); } while (re != null) { if (re.IsTerminal()) { rs.GetRuleConnect().EpsilonFree = true; return(true); } else { if (re.GetToken().Equals("")) { rs.GetRuleConnect().EpsilonFree = false; return(false); } else { RuleToken rt = (RuleToken)re; bool res = RuleIsEpsilonFree(rt.GetConnected()); if (res) { rs.GetRuleConnect().EpsilonFree = res; return(res); } else { re = re.GetNext(); } } } } rs.GetRuleConnect().EpsilonFree = false; return(false); }
public MyArrayList GetAllTerminals() { MyArrayList ts = new MyArrayList(); if (Start != null) { RuleElement pos = Start.GetNext(); while (pos != null) { if (pos.IsTerminal()) { ts.Add(pos); } pos = pos.GetNext(); } } return(ts); }
/// <summary> /// Find all Tokens inside Rule /// </summary> /// <param name="Token">Token String to find</param> /// <returns></returns> public MyArrayList FindToken(string Token) { MyArrayList sr = new MyArrayList(); if (Start != null) { RuleElement pos = Start.GetNext(); while (pos != null) { if (pos.GetToken().Equals(Token)) { sr.Add(pos); } pos = pos.GetNext(); } } return(sr); }
public void SetNextToken(String Token, bool Terminal) { if (Token == null || Token.Equals("")) { act.SetNext(new RuleEpsilon(Start)); } else { if (Terminal) { act.SetNext(new RuleTerminal(Token, Start)); } else { act.SetNext(new RuleToken(Token, Start)); } } act = act.GetNext(); }
private void EleminateDirectLeftRecursion(RuleStart strt) { string ElemStartTkn = ""; if (strt != null) { ElemStartTkn = FindUnusedToken(strt.GetToken()); } while (strt != null) { if (IsRuleLeftRekursiv(strt)) { RuleElement re = strt.GetNext(); if (re != null) { strt.SetNext(re.GetNext()); re = GotoLastRule(strt); re.SetNext(new RuleToken(ElemStartTkn, strt)); strt.SetToken(ElemStartTkn); } BNFRule bnfRl = new BNFRule(m_Rules.Count); bnfRl.SetStart(ElemStartTkn); bnfRl.GetStart().SetNext(new RuleEpsilon(bnfRl.GetStart())); m_Rules.Add(bnfRl); } else { RuleElement re = strt.GetNext(); if (re != null) { re = GotoLastRule(strt); re.SetNext(new RuleToken(ElemStartTkn, strt)); } } strt = strt.GetAlternate(); } }
public string GetRuleString() { string res = Start.GetToken() + "->"; RuleElement iterator = Start.GetNext(); while (iterator != null) { if (iterator.IsTerminal()) { res += "'" + iterator.GetToken() + "'"; } else { res += iterator.GetToken(); } iterator = iterator.GetNext(); if (iterator != null) { res += ","; } } res += "."; return(res); }