コード例 #1
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;
                }
            }
        }
コード例 #2
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;
                    }
                }
            }
        }
コード例 #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);
        }
コード例 #4
0
        public SLR1ParseTable(MyArrayList Rules, string StartSign) : base(Rules, StartSign)
        {
            m_StartSign = FindUnusedToken(m_StartSign);
            BNFRule bnfRl = new BNFRule(m_Rules.Count);

            bnfRl.SetStart(m_StartSign);
            bnfRl.GetStart().SetNext(new RuleToken(StartSign, bnfRl.GetStart()));
            m_Rules.Add(bnfRl);
        }
コード例 #5
0
        protected MyArrayList GetAllTerminals()
        {
            MyArrayList Terminals = new MyArrayList();

            for (int i = 0; i < m_Rules.Count; i++)
            {
                BNFRule rl = (BNFRule)m_Rules[i];
                Terminals = ParseHelper.UnionSet(Terminals, rl.GetAllTerminals());
            }
            return(Terminals);
        }
コード例 #6
0
        public string Print()
        {
            string res = "";

            for (int i = 0; i < m_Rules.Count; i++)
            {
                BNFRule rl = (BNFRule)m_Rules[i];
                res += i + ")\t" + rl.GetAllInfos() + "\n";
            }
            return(res);
        }
コード例 #7
0
        private MyArrayList GetAllFollows()
        {
            MyArrayList Terminals = new MyArrayList();

            for (int i = 0; i < m_Rules.Count; i++)
            {
                BNFRule rl = (BNFRule)m_Rules[i];
                Terminals = ParseHelper.UnionSet(Terminals, rl.FollowSet);
            }
            return(Terminals);
        }
コード例 #8
0
 private bool IsTokenInStartRule(string Token)
 {
     for (int i = 0; i < m_Rules.Count; i++)
     {
         BNFRule bnfRl = (BNFRule)m_Rules[i];
         if (Token.Equals(bnfRl.GetStart().GetToken()))
         {
             return(true);
         }
     }
     return(false);
 }
コード例 #9
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);
            }
        }
コード例 #10
0
        private MyArrayList GetNonTermsFrom(string NonTerm)
        {
            MyArrayList AllNonTerms = null;

            for (int i = 0; i < m_Rules.Count; i++)
            {
                BNFRule     rule    = (BNFRule)m_Rules[i];
                MyArrayList NonTrms = rule.FindToken(NonTerm);
                AllNonTerms = ParseHelper.AddList(AllNonTerms, NonTrms);
            }
            return(AllNonTerms);
        }
コード例 #11
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);
 }
コード例 #12
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);
        }
コード例 #13
0
        protected MyArrayList GetStartRulesOneToken(string Token)
        {
            MyArrayList StartRuleList = new MyArrayList();

            for (int i = 0; i < m_Rules.Count; i++)
            {
                BNFRule rule = (BNFRule)m_Rules[i];
                if (Token.Equals(rule.GetStart().GetToken()))
                {
                    StartRuleList.Add(rule.GetStart());
                }
            }
            return(StartRuleList);
        }
コード例 #14
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);
        }
コード例 #15
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);
        }
コード例 #16
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);
        }
コード例 #17
0
        protected RuleElement GetRuleElement(LRElement LrElm)
        {
            RuleElement re = null;

            if (LrElm != null)
            {
                int RulePos = LrElm.RulePos;
                if (RulePos >= 0 && RulePos < m_Rules.Count)
                {
                    BNFRule rl = (BNFRule)m_Rules[RulePos];
                    if (rl != null)
                    {
                        re = rl[LrElm.PosInRule];
                    }
                }
            }
            return(re);
        }
コード例 #18
0
        private RuleElement GetRuleElement(LRElement LrElm, int LookAHead)
        {
            RuleElement re = null;

            if (LrElm != null)
            {
                int RulePos = LrElm.RulePos;
                if (RulePos >= 0 && RulePos < m_Rules.Count)
                {
                    BNFRule rl = (BNFRule)m_Rules[RulePos];
                    if (rl != null)
                    {
                        re = rl[LrElm.PosInRule + LookAHead];
                    }
                }
            }
            return(re);
        }
コード例 #19
0
        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();
            }
        }
コード例 #20
0
        public virtual string PrintHuellen()
        {
            string res = "";

            if (m_Huellen != null)
            {
                for (int i = 0; i < m_Huellen.Count; i++)
                {
                    res += "I" + i + ":\n";
                    MyArrayList Huelle = (MyArrayList)m_Huellen[i];
                    if (Huelle != null)
                    {
                        for (int j = 0; j < Huelle.Count; j++)
                        {
                            LRElement LrElm = (LRElement)Huelle[j];
                            BNFRule   rl    = (BNFRule)m_Rules[LrElm.RulePos];
                            res += "(" + LrElm.RulePos + "," + LrElm.PosInRule + ")" + "\t" + GetRuleString(rl.GetStart(), LrElm.PosInRule) + "\n";
                        }
                    }
                    res += "\n\n";
                }
            }
            return(res);
        }
コード例 #21
0
ファイル: RuleStart.cs プロジェクト: sky4walk/WhileCompiler
 public RuleStart(String Token, RuleStart re, BNFRule back) : base(Token, re)
 {
     AlternateRule     = null;
     AlternateRuleBack = null;
     RuleConnect       = back;
 }
コード例 #22
0
        public ASTElement Parse(string Input)
        {
            m_ParseVarlauf = "";

            Input += m_EofSign;
            TerminalMatcher tm = new TerminalMatcher(Input, m_pt.Terminals);

            RuleTerminal ReadTerminal = tm.GetNextTerminal();
            bool         bAccept      = false;

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

                ButtomUpParseStack.buStackElement stkElm = m_ps.Look();

                if (stkElm != null)
                {
                    if (stkElm.IsRuleElement)
                    {
                        tmpVerlauf += "err";
                    }
                    else
                    {
                        int State = stkElm.GetState;
                        ButtomUpParseTabelle.ActionEntry ae = m_pt.Get(ReadTerminal, State);
                        if (ae != null)
                        {
                            if (ae.GetAction == ButtomUpParseTabelle.Actions.SHIFT)
                            {
                                tmpVerlauf += "; s " + ae.NextState.ToString();
                                m_ps.PushShift(ReadTerminal, ae.NextState);
                                ReadTerminal = tm.GetNextTerminal();
                            }
                            else if (ae.GetAction == ButtomUpParseTabelle.Actions.REDUCE)
                            {
                                int sm = ae.NextState;
                                tmpVerlauf += "; r " + ae.NextState.ToString();

                                if (sm < m_ParseTable.AllRules.Count)
                                {
                                    BNFRule rl    = (BNFRule)m_ParseTable.AllRules[sm];
                                    int     RlLen = rl.RuleLength();
                                    for (int i = 0; i < RlLen; i++)
                                    {
                                        m_ps.Pop2();
                                    }
                                    ButtomUpParseStack.buStackElement stkLook = m_ps.Look();
                                    if (stkLook != null)
                                    {
                                        int nxtState = m_ParseTable.GetGotoState(rl.GetStart(), stkLook.GetState);
                                        m_ps.PushReduce(rl.GetStart(), nxtState);
                                        tmpVerlauf += "; " + rl.GetRuleString();
                                    }
                                }
                            }
                            else if (ae.GetAction == ButtomUpParseTabelle.Actions.JUMP)
                            {
                                tmpVerlauf += "; err";
                            }
                            else if (ae.GetAction == ButtomUpParseTabelle.Actions.ACCEPT)
                            {
                                tmpVerlauf += "; acc";
                                bAccept     = true;
                            }
                        }
                    }
                }
                m_ParseVarlauf += tmpVerlauf + "\n";
            }

            return(m_ps.ASTRoot);
        }
コード例 #23
0
        private bool ParseBNF(string BNF)
        {
            bool bTerminal   = false;
            bool bDelNoSigns = true;
            int  automstate  = 0;
            int  len         = BNF.Length;
            int  spos        = 0;
            char sign;
            int  failureState = 100;

            char[]  DelSigns = new char[] { ' ', '\n', '\r', '\t' };
            BNFRule actRule  = null;

            while (spos < len)
            {
                if (bDelNoSigns)
                {
                    spos = ParseHelper.DelNoSigns(BNF, spos, DelSigns);
                }
                if (spos < len)
                {
                    sign = BNF.ToCharArray()[spos];
                    switch (automstate)
                    {
                    case 0:
                    {
                        actRule = null;
                        if (sign == '#')
                        {
                            spos = ParseHelper.DelComment(BNF, spos);
                        }
                        else
                        {
                            automstate = 1;
                        }
                        break;
                    }

                    case 1:
                    {
                        char[]     EndSigns = new char[] { '-' };
                        ScannerErg scnErg   = ParseHelper.GetWord(BNF, spos, EndSigns, DelSigns);
                        if (scnErg.getTermSignNr() == 0)
                        {
                            actRule = new BNFRule(m_Rules.Count);
                            actRule.SetStart(scnErg.getWord());
                            m_Rules.Add(actRule);
                            automstate = 2;
                        }
                        else
                        {
                            automstate = failureState;
                        }
                        spos = scnErg.getSpos();
                        break;
                    }

                    case 2:
                    {
                        if (spos < len && BNF.ToCharArray()[spos] == '>')
                        {
                            automstate = 3;
                            spos++;
                        }
                        else
                        {
                            automstate = failureState;
                        }
                        break;
                    }

                    case 3:
                    {
                        char[]     EndSigns = new char[] { ',', '\'', '|', '.', '$' };
                        ScannerErg scnErg   = ParseHelper.GetWord(BNF, spos, EndSigns, DelSigns);
                        if (scnErg.getTermSignNr() == 0)
                        {
                            actRule.SetNextToken(scnErg.getWord(), bTerminal);
                            automstate = 3;
                            bTerminal  = false;
                        }
                        else if (scnErg.getTermSignNr() == 1)
                        {
                            bTerminal   = true;
                            bDelNoSigns = false;
                            automstate  = 4;
                        }
                        else if (scnErg.getTermSignNr() == 2)
                        {
                            if (scnErg.getWord().Length != 0)
                            {
                                actRule.SetNextToken(scnErg.getWord(), bTerminal);
                            }
                            else
                            {
                                actRule.SetNextToken(scnErg.getWord(), true);
                            }
                            string StartSign = actRule.GetStart().GetToken();
                            actRule = new BNFRule(m_Rules.Count);
                            actRule.SetStart(StartSign);
                            m_Rules.Add(actRule);
                            automstate = 3;
                            bTerminal  = false;
                        }
                        else if (scnErg.getTermSignNr() == 3)
                        {
                            if (scnErg.getWord().Length != 0)
                            {
                                actRule.SetNextToken(scnErg.getWord(), bTerminal);
                            }
                            else
                            {
                                actRule.SetNextToken(scnErg.getWord(), true);
                            }
                            automstate = 0;
                            bTerminal  = false;
                        }
                        else if (scnErg.getTermSignNr() == 4)
                        {
                            automstate = 3;
                            bTerminal  = true;
                        }
                        else
                        {
                            automstate = failureState;
                        }
                        spos = scnErg.getSpos();
                        break;
                    }

                    case 4:
                    {
                        char[]     EndSigns = new char[] { '\'' };
                        ScannerErg scnErg   = ParseHelper.GetWord(BNF, spos, EndSigns, null);
                        if (scnErg.getTermSignNr() == 0)
                        {
                            actRule.SetNextToken(scnErg.getWord(), bTerminal);
                            automstate = 5;
                            bTerminal  = false;
                        }
                        else
                        {
                            automstate = failureState;
                        }
                        bDelNoSigns = true;
                        spos        = scnErg.getSpos();
                        break;
                    }

                    case 5:
                    {
                        char[]     EndSigns = new char[] { ',', '|', '.' };
                        ScannerErg scnErg   = ParseHelper.GetWord(BNF, spos, EndSigns, DelSigns);
                        if (scnErg.getTermSignNr() == 0)
                        {
                            automstate = 3;
                            bTerminal  = false;
                        }
                        else if (scnErg.getTermSignNr() == 1)
                        {
                            string StartSign = actRule.GetStart().GetToken();
                            actRule = new BNFRule(m_Rules.Count);
                            actRule.SetStart(StartSign);
                            m_Rules.Add(actRule);
                            automstate = 3;
                            bTerminal  = false;
                        }
                        else if (scnErg.getTermSignNr() == 2)
                        {
                            automstate = 0;
                            bTerminal  = false;
                        }
                        else
                        {
                            automstate = failureState;
                        }
                        spos = scnErg.getSpos();
                        break;
                    }

                    default:
                    {
                        return(false);
                    }
                    }
                }
            }
            return(true);
        }
コード例 #24
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);
        }