示例#1
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);
                        }
                    }
                }
            }
        }
示例#2
0
 public ButtomUpParser(SLR1ParseTable ParseTable)
 {
     m_ParseTable = ParseTable;
     m_pt         = ParseTable.SLR1Table;
     m_EofSign    = ParseTable.EofTerminal;
 }
示例#3
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);
        }