Exemplo n.º 1
0
 public TopDownParser(TopDownParseTabelle pt, RuleStart rs, string EOFSign)
 {
     m_pt = pt;
     m_rs = rs;
     m_ParseVerlaufArray = new MyArrayList();
     m_EofSign           = EOFSign;
 }
Exemplo n.º 2
0
        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;
                }
            }
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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;
                    }
                }
            }
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
 public BNFRule(int index)
 {
     Start         = null;
     act           = null;
     m_Index       = index;
     m_EpsilonFree = true;
 }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        public bool Add(string col, string row, RuleStart Rule)
        {
            int iCol = GetColPos(col);
            int iRow = GetRowPos(row);

            return(base.Add(iCol, iRow, Rule));
        }
Exemplo n.º 10
0
 public void SetAlternate(RuleStart alternate)
 {
     AlternateRule = alternate;
     if (alternate != null)
     {
         alternate.SetAlternateBack(this);
     }
 }
Exemplo n.º 11
0
 protected RuleStart GoToFirstStartRule(RuleStart rs)
 {
     if (rs != null)
     {
         while (rs.GetAlternateBack() != null)
         {
             rs = rs.GetAlternateBack();
         }
     }
     return(rs);
 }
Exemplo n.º 12
0
        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);
            }
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
 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);
     }
 }
Exemplo n.º 15
0
 private bool IsLeftRecursionInStartToken(RuleStart rs)
 {
     while (rs != null)
     {
         if (IsRuleLeftRekursiv(rs))
         {
             return(true);
         }
         rs = rs.GetAlternate();
     }
     return(false);
 }
Exemplo n.º 16
0
        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);
            }
        }
Exemplo n.º 17
0
//		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);
        }
Exemplo n.º 18
0
        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);
            }
        }
Exemplo n.º 19
0
 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);
         }
     }
 }
Exemplo n.º 20
0
 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);
 }
Exemplo n.º 21
0
        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);
        }
Exemplo n.º 22
0
        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);
        }
Exemplo n.º 23
0
        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)));
        }
Exemplo n.º 24
0
        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);
        }
Exemplo n.º 25
0
        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();
        }
Exemplo n.º 26
0
        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);
        }
Exemplo n.º 27
0
        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);
        }
Exemplo n.º 28
0
        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);
        }
Exemplo n.º 29
0
        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);
        }
Exemplo n.º 30
0
        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);
        }