예제 #1
0
 public TopDownParser(TopDownParseTabelle pt, RuleStart rs, string EOFSign)
 {
     m_pt = pt;
     m_rs = rs;
     m_ParseVerlaufArray = new MyArrayList();
     m_EofSign           = EOFSign;
 }
예제 #2
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);
        }
예제 #3
0
        /// <summary>
        /// constructor
        /// </summary>
        public wc()
        {
            if (m_tr.m_Filename.Length > 0)
            {
                TextLoader BnfLd = new TextLoader(m_tr.m_Filename);
                m_tr.m_BNF = BnfLd.Load();
            }
            BNFReader bnfReader = new BNFReader(m_tr.m_BNF);

            if (bnfReader.Init())
            {
                if (m_tr.ParsingType == Translate.ParseType.LL1)
                {
                    LL1ParseTable ParseTable = new LL1ParseTable(bnfReader.Rules, m_tr.m_StartSign);
                    if (ParseTable.Init())
                    {
                        string     Sets   = bnfReader.Print();
                        TextLoader SetsSv = new TextLoader("Mengen.txt");
                        SetsSv.Save(Sets);

                        TopDownParseTabelle ptbl = ParseTable.LL1ParseTabelle;
                        if (ptbl != null)
                        {
                            string     strTbl = ptbl.Print();
                            TextLoader StrSv  = new TextLoader("Tabelle.csv");
                            StrSv.Save(strTbl);

                            m_tdParser = new TopDownParser(ptbl, bnfReader.GetStartRule(ParseTable.StartRule), ParseTable.EofTerminal);
                        }
                    }
                }
                else if (m_tr.ParsingType == Translate.ParseType.SLR1)
                {
                    SLR1ParseTable ParseTable = new SLR1ParseTable(bnfReader.Rules, m_tr.m_StartSign);
                    if (ParseTable.Init())
                    {
                        string     huellen = bnfReader.Print() + "\n" + ParseTable.PrintHuellen() + "\n" + ParseTable.PrintGotoTable();
                        TextLoader StrSv   = new TextLoader("Huellen.txt");
                        StrSv.Save(huellen);

                        ButtomUpParseTabelle ptbl = ParseTable.SLR1Table;
                        if (ptbl != null)
                        {
                            string     strTbl = ptbl.Print();
                            TextLoader TblSv  = new TextLoader("Tabelle.csv");
                            TblSv.Save(strTbl);

                            m_buParser = new ButtomUpParser(ParseTable);
                        }
                    }
                }
                else if (m_tr.ParsingType == Translate.ParseType.LR1)
                {
                    LR1ParseTable ParseTable = new LR1ParseTable(bnfReader.Rules, m_tr.m_StartSign);
                    if (ParseTable.Init())
                    {
                        string     huellen = bnfReader.Print() + "\n" + ParseTable.PrintHuellen() + "\n" + ParseTable.PrintGotoTable();
                        TextLoader StrSv   = new TextLoader("Huellen.txt");
                        StrSv.Save(huellen);

                        ButtomUpParseTabelle ptbl = ParseTable.LR1Table;
                        if (ptbl != null)
                        {
                            string     strTbl = ptbl.Print();
                            TextLoader TblSv  = new TextLoader("Tabelle.csv");
                            TblSv.Save(strTbl);

                            m_buParser = new ButtomUpParser(ParseTable);
                        }
                    }
                }
                else if (m_tr.ParsingType == Translate.ParseType.LALR1)
                {
                    LALR1ParseTable ParseTable = new LALR1ParseTable(bnfReader.Rules, m_tr.m_StartSign);
                    if (ParseTable.Init())
                    {
                        string     huellen = bnfReader.Print() + "\n" + ParseTable.PrintHuellen() + "\n" + ParseTable.PrintGotoTable();
                        TextLoader StrSv   = new TextLoader("Huellen.txt");
                        StrSv.Save(huellen);

                        ButtomUpParseTabelle ptbl = ParseTable.LR1Table;
                        if (ptbl != null)
                        {
                            string     strTbl = ptbl.Print();
                            TextLoader TblSv  = new TextLoader("Tabelle.csv");
                            TblSv.Save(strTbl);

                            m_buParser = new ButtomUpParser(ParseTable);
                        }
                    }
                }
            }
        }