Ejemplo n.º 1
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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
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);
 }
Ejemplo n.º 4
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);
        }
Ejemplo 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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
 private int GetColPos(string col)
 {
     if (col != null)
     {
         for (int i = 0; i < m_Signs.Count; i++)
         {
             RuleElement re = (RuleElement)m_Signs[i];
             if (re != null)
             {
                 if (re.GetToken().Equals(col) && re.IsTerminal())
                 {
                     return(i);
                 }
             }
         }
     }
     return(-1);
 }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
        public ASTElement Parse(string Input)
        {
            Input += m_EofSign;
            TerminalMatcher tm = new TerminalMatcher(Input, m_pt.Terminals);

            RuleTerminal ReadTerminal = tm.GetNextTerminal();

            while (ReadTerminal != null && !m_ps.IsEmpty && !ReadTerminal.Equals(m_EofSign))
            {
                string tmpVerlauf = ReadTerminal.GetToken() + " : " + m_ps.StackValues + "\n";
                m_ParseVerlaufArray.Add(m_ps.StackArray);
                m_ParseVarlauf += tmpVerlauf;

                RuleElement re = m_ps.Pop();
                if (re != null)
                {
                    if (re.IsTerminal())
                    {
                        if (ReadTerminal.GetToken().Equals(re.GetToken()))
                        {
                        }
                        else
                        {
                            return(null);
                        }
                        ReadTerminal = tm.GetNextTerminal();
                    }
                    else if (re.GetToken().Length > 0 && ReadTerminal != null && ReadTerminal.GetToken().Length > 0)
                    {
                        RuleStart rs = m_pt.Get(ReadTerminal.GetToken(), re.GetToken());
                        if (rs == null)
                        {
                            return(null);
                        }
                        m_ps.Push(rs);
                    }
                }
                else
                {
                    break;
                }
            }
            return(m_ps.ASTRoot);
        }
Ejemplo n.º 13
0
        private void TraverseTree(ASTElement Node)
        {
            if (Node != null)
            {
                RuleElement re       = Node.rlElement;
                string      Token    = re.GetToken();
                bool        Terminal = re.IsTerminal();
                ExecuteMethod(Token, Terminal);

                ASTElement tmpAst = null;
                int        NodeNr = 0;
                do
                {
                    tmpAst = Node.GetNextNode(NodeNr);
                    TraverseTree(tmpAst);
                    NodeNr++;
                }while(tmpAst != null);
            }
        }
Ejemplo n.º 14
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++)
                        {
                            ButtomUpParseStack.buStackElement stkElm = (ButtomUpParseStack.buStackElement)actStack[j];
                            if (stkElm.IsRuleElement)
                            {
                                RuleElement rl = stkElm.GetRule;
                                if (rl.IsTerminal())
                                {
                                    parseverlaufStr += "'" + rl.GetToken() + "'";
                                }
                                else
                                {
                                    parseverlaufStr += rl.GetToken();
                                }
                                parseverlaufStr += " ";
                            }
                            else
                            {
                                // parseverlaufStr += stkElm.GetState.ToString();
                                // parseverlaufStr+=" ";
                            }
                        }
                        parseverlaufStr += "\n";
                    }
                }
            }
            return(parseverlaufStr);
        }
Ejemplo n.º 15
0
        protected override void Closure(MyArrayList Huelle)
        {
            bool bChanged = true;

            if (Huelle != null)
            {
                while (bChanged)
                {
                    bChanged = false;
                    for (int i = 0; i < Huelle.Count; i++)
                    {
                        LR1Element  LrElm = (LR1Element)Huelle[i];
                        RuleElement re    = GetRuleElement(LrElm);
                        if (re != null && !re.IsTerminal() && re.GetToken().Length > 0)
                        {
                            RuleElement reLH    = GetRuleElement(LrElm, m_LH);
                            MyArrayList FrstSet = null;
                            if (reLH == null)
                            {
                                FrstSet = LrElm.FirstSet;
                            }
                            else
                            {
                                FrstSet = BetaTotalRuleFirstSet(reLH);
                                if (!BetaTotalRuleEpsFree(reLH))
                                {
                                    FrstSet = ParseHelper.UnionSet(FrstSet, LrElm.FirstSet);
                                }
                            }
                            if (FirstClosureFromRule(Huelle, re.GetToken(), FrstSet))
                            {
                                bChanged = true;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 16
0
        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);
        }
Ejemplo n.º 17
0
        protected virtual void Closure(MyArrayList Huelle)
        {
            bool bChanged = true;

            if (Huelle != null)
            {
                while (bChanged)
                {
                    bChanged = false;
                    for (int i = 0; i < Huelle.Count; i++)
                    {
                        LRElement   LrElm = (LRElement)Huelle[i];
                        RuleElement re    = GetRuleElement(LrElm);
                        if (re != null && !re.IsTerminal() && re.GetToken().Length > 0)
                        {
                            if (FirstClosureFromRule(Huelle, re.GetToken()))
                            {
                                bChanged = true;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 18
0
        protected override bool GenerateParseTable()
        {
            MyArrayList Terminals = GetAllTerminals();

            Terminals.Add(new RuleTerminal(m_EofTerminal, null));
            MyArrayList Tokens = GetDifStartTokens();
            MyArrayList column = new MyArrayList();

            for (int i = 0; i < Terminals.Count; i++)
            {
                RuleElement re = (RuleElement)Terminals[i];
                column.Add(re);
            }
            for (int i = 0; i < Tokens.Count; i++)
            {
                RuleStart rs = (RuleStart)Tokens[i];
                column.Add(rs);
            }

            m_bupt = new ButtomUpParseTabelle(column, Terminals, m_Huellen.Count);

            for (int i = 0; i < m_GotoTable.Count; i++)
            {
                GotoEntry   ge = (GotoEntry)m_GotoTable[i];
                RuleElement re = ge.TokenSymbol;
                if (re.IsTerminal())
                {
                    ButtomUpParseTabelle.ActionEntry buptAE = new ButtomUpParseTabelle.ActionEntry(ButtomUpParseTabelle.Actions.SHIFT, ge.JumpToState);
                    m_bupt.Add(re, ge.ThisState, buptAE);
                }
                else
                {
                    ButtomUpParseTabelle.ActionEntry buptAE = new ButtomUpParseTabelle.ActionEntry(ButtomUpParseTabelle.Actions.JUMP, ge.JumpToState);
                    m_bupt.Add(re, ge.ThisState, buptAE);
                }
            }

            bool dblEntry = false;

            for (int i = 0; i < m_Huellen.Count; i++)
            {
                MyArrayList ActHuelle = (MyArrayList)m_Huellen[i];
                for (int j = 0; j < ActHuelle.Count; j++)
                {
                    LRElement   lrm = (LRElement)ActHuelle[j];
                    RuleElement re  = GetRuleElement(lrm);
                    if (re == null)
                    {
                        BNFRule     rl   = (BNFRule)m_Rules[lrm.RulePos];
                        MyArrayList flws = rl.FollowSet;
                        if (flws != null)
                        {
                            for (int z = 0; z < flws.Count; z++)
                            {
                                RuleElement flw_re = (RuleElement)flws[z];
                                ButtomUpParseTabelle.ActionEntry buptAE = new ButtomUpParseTabelle.ActionEntry(ButtomUpParseTabelle.Actions.REDUCE, lrm.RulePos);
                                ButtomUpParseTabelle.ActionEntry ae     = m_bupt.Get(flw_re, i);
                                if (ae == null)
                                {
                                    m_bupt.Add(flw_re, i, buptAE);
                                }
                                else
                                {
                                    if (ae.GetAction != ButtomUpParseTabelle.Actions.ACCEPT)
                                    {
                                        dblEntry = true;
                                    }
                                }
                            }
                            if (rl.GetStart().GetToken().Equals(m_StartSign))
                            {
                                ButtomUpParseTabelle.ActionEntry buptAE = new ButtomUpParseTabelle.ActionEntry(ButtomUpParseTabelle.Actions.ACCEPT, lrm.RulePos);
                                m_bupt.Add(m_EofTerminal, i, buptAE);
                            }
                        }
                    }
                }
            }

            return(!dblEntry);
        }