Пример #1
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);
        }
Пример #2
0
        public override 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++)
                        {
                            LR1Element LrElm = (LR1Element)Huelle[j];
                            BNFRule    rl    = (BNFRule)m_Rules[LrElm.RulePos];
                            res += "(" + LrElm.RulePos + "," + LrElm.PosInRule + ")" + "\t" + GetRuleString(rl.GetStart(), LrElm.PosInRule) + "\t" + ParseHelper.PrintRules(LrElm.FirstSet) + "\n";
                        }
                    }
                    res += "\n\n";
                }
            }
            return(res);
        }