public TopDownParser(TopDownParseTabelle pt, RuleStart rs, string EOFSign) { m_pt = pt; m_rs = rs; m_ParseVerlaufArray = new MyArrayList(); m_EofSign = EOFSign; }
protected void CheckEpsilonInRules() { bool bChanged = true; while (bChanged) { bChanged = false; for (int i = 0; i < m_Rules.Count; i++) { bool EpsFree = false; BNFRule bnfRl = (BNFRule)m_Rules[i]; RuleStart strt = bnfRl.GetStart(); if (strt != null) { RuleElement re = strt.GetNext(); EpsFree = BetaTotalRuleEpsFree(re); } if (strt.GetRuleConnect().EpsilonFree != EpsFree) { bChanged = true; } strt.GetRuleConnect().EpsilonFree = EpsFree; } } }
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); }
protected void GenerateAllFirstSets() { bool bChanged = true; while (bChanged) { bChanged = false; for (int i = 0; i < m_Rules.Count; i++) { MyArrayList FirstSet = new MyArrayList(); BNFRule bnfRl = (BNFRule)m_Rules[i]; RuleStart strt = bnfRl.GetStart(); if (strt != null) { RuleElement re = strt.GetNext(); FirstSet = BetaTotalRuleFirstSet(re); } int FrstSize = 0; if (strt.GetRuleConnect().FirstSet != null) { FrstSize = strt.GetRuleConnect().FirstSet.Count; } strt.GetRuleConnect().FirstSet = ParseHelper.UnionSet(strt.GetRuleConnect().FirstSet, FirstSet); FrstSize = strt.GetRuleConnect().FirstSet.Count - FrstSize; if (FrstSize != 0) { bChanged = true; } } } }
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); }
public BNFRule(int index) { Start = null; act = null; m_Index = index; m_EpsilonFree = true; }
private bool FirstClosureFromRule(MyArrayList Huelle, string Token, MyArrayList FirstSet) { bool bChanged = false; MyArrayList TkRules = base.GetStartRulesOneToken(Token); if (TkRules != null && Huelle != null) { for (int i = 0; i < TkRules.Count; i++) { RuleStart rs = (RuleStart)TkRules[i]; int RuleNr = rs.GetRuleConnect().Index; int HuelPos = RuleInsideHuelle(Huelle, RuleNr, 1); if (HuelPos < 0) { LR1Element LrElm = new LR1Element(rs.GetRuleConnect().Index, m_HuellenCounter, m_LRMECnter++); LrElm.FirstSet = FirstSet; Huelle.Add(LrElm); bChanged = true; } else { LR1Element LrElm = (LR1Element)Huelle[HuelPos]; int FrstSize = LrElm.FirstSet.Count; LrElm.FirstSet = ParseHelper.UnionSet(LrElm.FirstSet, FirstSet); if (FrstSize != LrElm.FirstSet.Count) { bChanged = true; } } } } return(bChanged); }
public string Print() { string TableOut = ms_Seperator; for (int i = 0; i < m_Terminals.Count; i++) { RuleTerminal rt = (RuleTerminal)m_Terminals[i]; TableOut += rt.GetToken() + ms_Seperator; } TableOut += "\n"; for (int i = 0; i < m_StartTerms.Count; i++) { RuleElement re = (RuleElement)m_StartTerms[i]; TableOut += re.GetToken() + ms_Seperator; for (int j = 0; j < m_Terminals.Count; j++) { RuleTerminal rt = (RuleTerminal)m_Terminals[j]; RuleStart rs = Get(rt.GetToken(), re.GetToken()); if (rs == null) { TableOut += ms_Seperator; } else { TableOut += rs.GetAllElements() + ms_Seperator; } } TableOut += "\n"; } return(TableOut); }
public bool Add(string col, string row, RuleStart Rule) { int iCol = GetColPos(col); int iRow = GetRowPos(row); return(base.Add(iCol, iRow, Rule)); }
public void SetAlternate(RuleStart alternate) { AlternateRule = alternate; if (alternate != null) { alternate.SetAlternateBack(this); } }
protected RuleStart GoToFirstStartRule(RuleStart rs) { if (rs != null) { while (rs.GetAlternateBack() != null) { rs = rs.GetAlternateBack(); } } return(rs); }
protected void ConnectAlternateStartRules() { MyArrayList StartTokens = GetDifStartTokens(); for (int i = 0; i < StartTokens.Count; i++) { RuleStart strt = (RuleStart)StartTokens[i]; MyArrayList OneTokenRules = GetStartRulesOneToken(strt.GetToken()); ConnectAlternateStartRules(OneTokenRules); } }
private MyArrayList TotalRuleFollowSet(RuleStart rs) { MyArrayList res = new MyArrayList(); rs = GoToFirstStartRule(rs); while (rs != null) { res = ParseHelper.UnionSet(res, rs.GetRuleConnect().FollowSet); rs = rs.GetAlternate(); } return(res); }
public TopDownParseStack(RuleStart rs) { m_Stack = new MyStack(); m_TreeStack = new MyStack(); if (rs != null) { m_Stack.Push(rs); m_TreeRoot = new ASTElement(rs); m_ActRoot = m_TreeRoot; m_TreeStack.Push(m_TreeRoot); } }
private bool IsLeftRecursionInStartToken(RuleStart rs) { while (rs != null) { if (IsRuleLeftRekursiv(rs)) { return(true); } rs = rs.GetAlternate(); } return(false); }
protected void ConnectTokens() { MyArrayList StartTokens = GetDifStartTokens(); for (int i = 0; i < m_Rules.Count; i++) { BNFRule rule = (BNFRule)m_Rules[i]; RuleStart strt = rule.GetStart(); MyArrayList AllTkns = strt.GetAllTokens(); ConnectToken2Start(StartTokens, AllTkns); } }
// protected virtual void GenerateAllFirstSets() // { // for(int i=0;i<m_Rules.Count;i++) // { // BNFRule bnfRl = (BNFRule)m_Rules[i]; // RuleStart strt = bnfRl.GetStart(); // strt.GetRuleConnect().FirstSet = GenerateFirstSet(strt); // } // } // protected virtual void CheckEpsilonInRules() // { // for(int i=0;i<m_Rules.Count;i++) // { // BNFRule bnfRl = (BNFRule)m_Rules[i]; // RuleStart strt = bnfRl.GetStart(); // RuleIsEpsilonFree(strt); // } // } protected bool TotalRuleEpsFree(RuleStart rs) { rs = GoToFirstStartRule(rs); while (rs != null) { if (!rs.GetRuleConnect().EpsilonFree) { return(false); } rs = rs.GetAlternate(); } return(true); }
public void Push(RuleStart Transition) { MyArrayList TransRules = Rules2Array(Transition); for (int i = 0; i < TransRules.Count; i++) { RuleElement re = (RuleElement)TransRules[TransRules.Count - i - 1]; m_Stack.Push(re); re = (RuleElement)TransRules[i]; ASTElement astElm = m_ActRoot.AddNode(re); m_TreeStack.Push(astElm); } }
private void ConnectToken2Start(MyArrayList StartTokens, MyArrayList AllRuleTokens) { for (int i = 0; i < AllRuleTokens.Count; i++) { RuleToken rlTkn = (RuleToken)AllRuleTokens[i]; RuleStart rlStartTkn = FindStartTokenList(StartTokens, rlTkn); if (rlStartTkn != null) { rlStartTkn = GoToFirstStartRule(rlStartTkn); rlTkn.SetConnected(rlStartTkn); } } }
public RuleStart GetStartRule(string Token) { for (int i = 0; i < m_Rules.Count; i++) { BNFRule rl = (BNFRule)m_Rules[i]; RuleStart rs = rl.GetStart(); if (Token.Equals(rs.GetToken())) { return(rs); } } return(null); }
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); }
private bool IsRuleLeftRekursiv(RuleStart rs) { RuleElement re = rs.GetNext(); if (re != null) { string StartToken = rs.GetToken(); string FirstToken = re.GetToken(); if (StartToken.Equals(FirstToken)) { return(true); } } return(false); }
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))); }
protected MyArrayList GetDifStartTokens() { MyArrayList TokenList = new MyArrayList(); for (int i = 0; i < m_Rules.Count; i++) { BNFRule rule = (BNFRule)m_Rules[i]; RuleStart Strt = rule.GetStart(); if (!ParseHelper.IsTokenInTokenList(TokenList, Strt)) { TokenList.Add(Strt); } } return(TokenList); }
private void EleminateAllLeftRecursion() { MyArrayList StartTokens = GetDifStartTokens(); for (int i = 0; i < StartTokens.Count; i++) { RuleStart strt = (RuleStart)StartTokens[i]; strt = GoToFirstStartRule(strt); if (IsLeftRecursionInStartToken(strt)) { EleminateDirectLeftRecursion(strt); } } ConnectAlternateStartRules(); }
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); }
private RuleStart FindStartTokenList(MyArrayList StartTokens, RuleToken rlTkn) { RuleStart rlStrt = null; for (int i = 0; i < StartTokens.Count; i++) { RuleStart rlStartTkn = (RuleStart)StartTokens[i]; if (rlTkn.GetToken().Equals(rlStartTkn.GetToken())) { rlStrt = rlStartTkn; break; } } return(rlStrt); }
protected virtual bool GenerateParseTable() { MyArrayList Terminals = GetAllTerminals(); Terminals.Add(new RuleTerminal(m_EofTerminal, null)); MyArrayList StartTokens = GetDifStartTokens(); m_LL1ParseTabelle = new TopDownParseTabelle(Terminals, StartTokens); bool dblEntry = false; for (int i = 0; i < m_Rules.Count; i++) { BNFRule rl = (BNFRule)m_Rules[i]; RuleStart rs = rl.GetStart(); int len = rs.GetRuleConnect().FirstSet.Count; for (int j = 0; j < len; j++) { RuleElement re = (RuleElement)rs.GetRuleConnect().FirstSet[j]; string colSign = re.GetToken(); string rowSign = rs.GetToken(); if (!m_LL1ParseTabelle.Add(colSign, rowSign, rs)) { dblEntry = true; } } if (!rl.EpsilonFree) { len = rs.GetRuleConnect().FollowSet.Count; for (int j = 0; j < len; j++) { RuleElement re = (RuleElement)rs.GetRuleConnect().FollowSet[j]; string colSign = re.GetToken(); string rowSign = rs.GetToken(); if (!m_LL1ParseTabelle.Add(colSign, rowSign, rs)) { dblEntry = true; } } } } return(!dblEntry); }
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); }
private void DeleteStartRule(RuleStart rs) { BNFRule bnfRl = rs.GetRuleConnect(); RuleStart AltNext = rs.GetAlternate(); RuleStart AltBack = rs.GetAlternateBack(); if (AltBack != null) { AltBack.SetAlternate(AltNext); } if (AltNext != null) { AltNext.SetAlternateBack(AltBack); } int index = bnfRl.Index; m_Rules.RemoveAt(index); }