示例#1
0
        public RuleElement Pop()
        {
            RuleElement re = (RuleElement)m_Stack.Pop();

            m_ActRoot = (ASTElement)m_TreeStack.Pop();
            return(re);
        }
示例#2
0
        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);
        }
示例#3
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;
                    }
                }
            }
        }
示例#4
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);
        }
示例#5
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);
        }
示例#6
0
 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);
 }
示例#7
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);
        }
示例#8
0
        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);
        }
示例#9
0
 public BNFRule(int index)
 {
     Start         = null;
     act           = null;
     m_Index       = index;
     m_EpsilonFree = true;
 }
示例#10
0
        /// <summary>
        /// Add a rulelement to the root
        /// </summary>
        /// <param name="re"></param>
        /// <returns>Added ASTElement</returns>
        public ASTElement AddNode(RuleElement re)
        {
            ASTElement next = new ASTElement(re, this);

            m_NodeList.Add(next);
            return(next);
        }
示例#11
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;
                }
            }
        }
示例#12
0
        public static string ParseVerlaufArray2String(MyArrayList parseList)
        {
            string parseverlaufStr = "";

            if (parseList != null)
            {
                for (int i = 0; i < parseList.Count; i++)
                {
                    MyArrayList actStack = (MyArrayList)parseList[i];
                    if (actStack != null)
                    {
                        for (int j = 0; j < actStack.Count; j++)
                        {
                            RuleElement rl = (RuleElement)actStack[j];
                            if (rl.IsTerminal())
                            {
                                parseverlaufStr += "'" + rl.GetToken() + "'";
                            }
                            else
                            {
                                parseverlaufStr += rl.GetToken();
                            }
                            parseverlaufStr += " ";
                        }
                    }
                    parseverlaufStr += "\n";
                }
            }
            return(parseverlaufStr);
        }
示例#13
0
 private RuleElement GotoLastRule(RuleElement re)
 {
     while (re.GetNext() != null)
     {
         re = re.GetNext();
     }
     return(re);
 }
示例#14
0
        public void PushReduce(RuleElement re, int State)
        {
            m_Stack.Push(new buStackElement(re));
            m_Stack.Push(new buStackElement(State));

            m_TreeRoot = new ASTElement(re, m_actPopList);
            m_TreeStack.Push(m_TreeRoot);

            m_actPopList.Clear();
        }
示例#15
0
        public void PushShift(RuleElement re, int State)
        {
            m_Stack.Push(new buStackElement(re));
            m_Stack.Push(new buStackElement(State));

            ASTElement neuElm = new ASTElement(re);

            m_TreeStack.Push(neuElm);

            m_actPopList.Clear();
        }
示例#16
0
        public int GetGotoState(RuleElement re, int HuellenNr)
        {
            int GotoPos = GetPosInGototable(re, HuellenNr);

            if (GotoPos >= 0)
            {
                GotoEntry ge = (GotoEntry)m_GotoTable[GotoPos];
                return(ge.JumpToState);
            }
            return(-1);
        }
示例#17
0
 internal bool IsSame(RuleElement ReElm, int ActState)
 {
     if (ReElm != null)
     {
         if (m_ReElm.GetToken().Equals(ReElm.GetToken()) && ActState == m_ActState)
         {
             return(true);
         }
     }
     return(false);
 }
示例#18
0
 private int GetRowPos(string row)
 {
     for (int i = 0; i < m_StartTerms.Count; i++)
     {
         RuleElement re = (RuleElement)m_StartTerms[i];
         if (re.GetToken().Equals(row))
         {
             return(i);
         }
     }
     return(-1);
 }
示例#19
0
        public int RuleLength()
        {
            int         rl_len   = 0;
            RuleElement iterator = Start.GetNext();

            while (iterator != null)
            {
                rl_len++;
                iterator = iterator.GetNext();
            }
            return(rl_len);
        }
示例#20
0
        public string Print()
        {
            string TableOut = ms_Seperator;

            for (int i = 0; i < m_Signs.Count; i++)
            {
                RuleElement re = (RuleElement)m_Signs[i];
                TableOut += re.GetToken() + ms_Seperator;
            }

            TableOut += "\n";

            for (int i = 0; i < m_rows; i++)
            {
                TableOut += i + ms_Seperator;
                for (int j = 0; j < m_Signs.Count; j++)
                {
                    RuleElement re = (RuleElement)m_Signs[j];

                    ActionEntry ae = Get(re, i);
                    if (ae == null)
                    {
                        TableOut += ms_Seperator;
                    }
                    else
                    {
                        if (ae.GetAction == Actions.SHIFT)
                        {
                            TableOut += "s " + ae.NextState + ms_Seperator;
                        }
                        else if (ae.GetAction == Actions.REDUCE)
                        {
                            TableOut += "r " + ae.NextState + ms_Seperator;
                        }
                        else if (ae.GetAction == Actions.JUMP)
                        {
                            TableOut += ae.NextState + ms_Seperator;
                        }
                        else if (ae.GetAction == Actions.ACCEPT)
                        {
                            TableOut += "acc" + ms_Seperator;
                        }
                        else
                        {
                        }
                    }
                }
                TableOut += "\n";
            }

            return(TableOut);
        }
示例#21
0
 /// <summary>
 /// constructor
 /// </summary>
 /// <param name="re">RuleElement</param>
 public ASTElement(RuleElement re, ASTElement root)
 {
     m_re   = re;
     m_Root = root;
     if (root != null)
     {
         m_Depth = root.TreeDepth + 1;
     }
     else
     {
         m_Depth = 0;
     }
 }
示例#22
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="re"></param>
 /// <param name="Nodes"></param>
 public ASTElement(RuleElement re, MyArrayList Nodes)
 {
     m_re       = re;
     m_Root     = null;
     m_NodeList = new MyArrayList();
     if (Nodes != null)
     {
         for (int i = 0; i < Nodes.Count; i++)
         {
             m_NodeList.Add(Nodes[i]);
         }
     }
 }
示例#23
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);
            }
        }
示例#24
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);
        }
示例#25
0
        public ActionEntry Get(RuleElement col, int State)
        {
            int iCol = GetColPos(col);

//			if(iCol>=0)
            {
                object obj = base.Get(iCol, State);
                if (obj.GetType() == typeof(ActionEntry))
                {
                    return((ActionEntry)obj);
                }
            }
            return(null);
        }
示例#26
0
        private RuleElement GetElementFromHuelle(LRElement elm)
        {
            RuleElement re = null;

            if (elm.RulePos < m_Rules.Count)
            {
                BNFRule rl = (BNFRule)m_Rules[elm.RulePos];
                if (rl != null)
                {
                    re = rl[elm.PosInRule];
                }
            }
            return(re);
        }
示例#27
0
        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);
        }
示例#28
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)));
        }