GrammarOk() public method

public GrammarOk ( ) : bool
return bool
Exemplo n.º 1
0
        void Coco()
        {
            Symbol sym; Graph g, g1, g2; string gramName; CharSet s; int beg, line;

            if (StartOf(1))
            {
                Get();
                beg = t.pos; line = t.line;
                while (StartOf(1))
                {
                    Get();
                }
                pgen.usingPos = new Position(beg, la.pos, 0, line);
            }
            Expect(6);
            genScanner  = true;
            tab.ignored = new CharSet();
            Expect(1);
            gramName = t.val;
            beg      = la.pos; line = la.line;

            while (StartOf(2))
            {
                Get();
            }
            tab.semDeclPos = new Position(beg, la.pos, 0, line);
            if (la.kind == 7)
            {
                Get();
                dfa.ignoreCase = true;
            }
            if (la.kind == 8)
            {
                Get();
                while (la.kind == 1)
                {
                    SetDecl();
                }
            }
            if (la.kind == 9)
            {
                Get();
                while (la.kind == 1 || la.kind == 3 || la.kind == 5)
                {
                    TokenDecl(Node.t);
                }
            }
            if (la.kind == 10)
            {
                Get();
                while (la.kind == 1 || la.kind == 3 || la.kind == 5)
                {
                    TokenDecl(Node.pr);
                }
            }
            while (la.kind == 11)
            {
                Get();
                bool nested = false;
                Expect(12);
                TokenExpr(out g1);
                Expect(13);
                TokenExpr(out g2);
                if (la.kind == 14)
                {
                    Get();
                    nested = true;
                }
                dfa.NewComment(g1.l, g2.l, nested);
            }
            while (la.kind == 15)
            {
                Get();
                Set(out s);
                tab.ignored.Or(s);
            }
            while (!(la.kind == 0 || la.kind == 16))
            {
                SynErr(42); Get();
            }
            Expect(16);
            if (genScanner)
            {
                dfa.MakeDeterministic();
            }
            tab.DeleteNodes();

            while (la.kind == 1)
            {
                Get();
                sym = tab.FindSym(t.val);
                bool undef = sym == null;
                if (undef)
                {
                    sym = tab.NewSym(Node.nt, t.val, t.line);
                }
                else
                {
                    if (sym.typ == Node.nt)
                    {
                        if (sym.graph != null)
                        {
                            SemErr("name declared twice");
                        }
                    }
                    else
                    {
                        SemErr("this symbol kind not allowed on left side of production");
                    }
                    sym.line = t.line;
                }
                bool noAttrs = sym.attrPos == null;
                sym.attrPos = null;

                if (la.kind == 24 || la.kind == 26)
                {
                    AttrDecl(sym);
                }
                if (!undef)
                {
                    if (noAttrs != (sym.attrPos == null))
                    {
                        SemErr("attribute mismatch between declaration and use of this symbol");
                    }
                }

                if (la.kind == 39)
                {
                    SemText(out sym.semPos);
                }
                ExpectWeak(17, 3);
                Expression(out g);
                sym.graph = g.l;
                tab.Finish(g);

                ExpectWeak(18, 4);
            }
            Expect(19);
            Expect(1);
            if (gramName != t.val)
            {
                SemErr("name does not match grammar name");
            }
            tab.gramSy = tab.FindSym(gramName);
            if (tab.gramSy == null)
            {
                SemErr("missing production for grammar name");
            }
            else
            {
                sym = tab.gramSy;
                if (sym.attrPos != null)
                {
                    SemErr("grammar symbol must not have attributes");
                }
            }
            tab.noSym = tab.NewSym(Node.t, "???", 0);     // noSym gets highest number
            tab.SetupAnys();
            tab.RenumberPragmas();
            if (tab.ddt[2])
            {
                tab.PrintNodes();
            }
            if (errors.count == 0)
            {
                Console.WriteLine("checking");
                tab.CompSymbolSets();
                if (tab.ddt[7])
                {
                    tab.XRef();
                }
                if (tab.GrammarOk())
                {
                    Console.Write("parser");
                    pgen.WriteParser();
                    if (genScanner)
                    {
                        Console.Write(" + scanner");
                        dfa.WriteScanner();
                        if (tab.ddt[0])
                        {
                            dfa.PrintStates();
                        }
                    }
                    Console.WriteLine(" generated");
                    if (tab.ddt[8])
                    {
                        pgen.WriteStatistics();
                    }
                }
            }
            if (tab.ddt[6])
            {
                tab.PrintSymbolTable();
            }

            Expect(18);
        }
Exemplo n.º 2
0
        static void Coco()
        {
            Symbol sym; Graph g; string gramName;

            if (la.kind == 40)
            {
                UsingDecl(out ParserGen.usingPos);
            }
            Expect(6);
            int gramLine = t.line;

            genScanner = true;
            bool ok = true;

            Tab.ignored = null;

            Expect(1);
            gramName = t.val;
            int beg = la.pos;

            while (StartOf(1))
            {
                Get();
            }
            Tab.semDeclPos = new Position(beg, la.pos - beg, 0);
            while (StartOf(2))
            {
                Declaration();
            }
            while (!(la.kind == 0 || la.kind == 7))
            {
                SynErr(43); Get();
            }
            Expect(7);
            if (genScanner)
            {
                DFA.MakeDeterministic();
            }
            Graph.DeleteNodes();

            while (la.kind == 1)
            {
                Get();
                sym = Symbol.Find(t.val);
                bool undef = sym == null;
                if (undef)
                {
                    sym = new Symbol(Node.nt, t.val, t.line);
                }
                else
                {
                    if (sym.typ == Node.nt)
                    {
                        if (sym.graph != null)
                        {
                            SemErr("name declared twice");
                        }
                    }
                    else
                    {
                        SemErr("this symbol kind not allowed on left side of production");
                    }
                    sym.line = t.line;
                }
                bool noAttrs = sym.attrPos == null;
                sym.attrPos = null;

                if (la.kind == 24)
                {
                    AttrDecl(sym);
                }
                if (!undef)
                {
                    if (noAttrs != (sym.attrPos == null))
                    {
                        SemErr("attribute mismatch between declaration and use of this symbol");
                    }
                }

                if (la.kind == 38)
                {
                    SemText(out sym.semPos);
                }
                ExpectWeak(8, 3);
                Expression(out g);
                sym.graph = g.l;
                Graph.Finish(g);

                ExpectWeak(9, 4);
            }
            Expect(10);
            Expect(1);
            if (gramName != t.val)
            {
                SemErr("name does not match grammar name");
            }
            Tab.gramSy = Symbol.Find(gramName);
            if (Tab.gramSy == null)
            {
                SemErr("missing production for grammar name");
            }
            else
            {
                sym = Tab.gramSy;
                if (sym.attrPos != null)
                {
                    SemErr("grammar symbol must not have attributes");
                }
            }
            Tab.noSym = new Symbol(Node.t, "???", 0);             // noSym gets highest number
            Tab.SetupAnys();
            Tab.RenumberPragmas();
            if (Tab.ddt[2])
            {
                Node.PrintNodes();
            }
            if (Errors.count == 0)
            {
                Console.WriteLine("checking");
                Tab.CompSymbolSets();
                ok = ok && Tab.GrammarOk();
                if (Tab.ddt[7])
                {
                    Tab.XRef();
                }
                if (ok)
                {
                    Console.Write("parser");
                    ParserGen.WriteParser();
                    if (genScanner)
                    {
                        Console.Write(" + scanner");
                        DFA.WriteScanner();
                        if (Tab.ddt[0])
                        {
                            DFA.PrintStates();
                        }
                    }
                    Console.WriteLine(" generated");
                    if (Tab.ddt[8])
                    {
                        ParserGen.WriteStatistics();
                    }
                }
            }
            if (Tab.ddt[6])
            {
                Tab.PrintSymbolTable();
            }

            Expect(9);
        }