Пример #1
0
        static void TokenFactor(out Graph g)
        {
            string name;
            int    kind;

            g = new Graph();
            if (la.kind == 1 || la.kind == 3 || la.kind == 5)
            {
                Sym(out name, out kind);
                if (kind == id)
                {
                    CharClass c = CharClass.Find(name);
                    if (c == null)
                    {
                        SemErr("undefined name");
                        c = new CharClass(name, new BitArray(CharClass.charSetSize));
                    }
                    Node p = new Node(Node.clas, null, 0);
                    p.val = c.n;
                    g     = new Graph(p);
                }
                else
                {
                    g = Graph.StrToGraph(name); // str
                }
            }
            else if (la.kind == 28)
            {
                Get();
                TokenExpr(out g);
                Expect(29);
            }
            else if (la.kind == 30)
            {
                Get();
                TokenExpr(out g);
                Expect(31);
                Graph.MakeOption(g);
            }
            else if (la.kind == 32)
            {
                Get();
                TokenExpr(out g);
                Expect(33);
                Graph.MakeIteration(g);
            }
            else
            {
                SynErr(54);
            }
        }
Пример #2
0
        static void Factor(out Graph g)
        {
            string name; int kind; Position pos; bool weak = false;

            g = null;

            switch (la.kind)
            {
            case 1:
            case 3:
            case 5:
            case 27: {
                if (la.kind == 27)
                {
                    Get();
                    weak = true;
                }
                Sym(out name, out kind);
                Symbol sym   = Symbol.Find(name);
                bool   undef = sym == null;
                if (undef)
                {
                    if (kind == id)
                    {
                        sym = new Symbol(Node.nt, name, 0);                                          // forward nt
                    }
                    else if (genScanner)
                    {
                        sym = new Symbol(Node.t, name, t.line);
                        DFA.MatchLiteral(sym);
                    }
                    else                                          // undefined string in production
                    {
                        SemErr("undefined string in production");
                        sym = Tab.eofSy;                                          // dummy
                    }
                }
                int typ = sym.typ;
                if (typ != Node.t && typ != Node.nt && typ != Node.rslv)                                 /* ML */
                {
                    SemErr("this symbol kind is not allowed in a production");
                }
                if (weak)
                {
                    if (typ == Node.t)
                    {
                        typ = Node.wt;
                    }
                    else
                    {
                        SemErr("only terminals may be weak");
                    }
                }
                Node p = new Node(typ, sym, t.line);
                g = new Graph(p);

                if (la.kind == 24)
                {
                    Attribs(p);
                    if (kind != id)
                    {
                        SemErr("a literal must not have attributes");
                    }
                }
                if (undef)
                {
                    sym.attrPos = p.pos;                                      // dummy
                }
                else if ((p.pos == null) != (sym.attrPos == null))
                {
                    SemErr("attribute mismatch between declaration and use of this symbol");
                }

                break;
            }

            case 28: {
                Get();
                Expression(out g);
                Expect(29);
                break;
            }

            case 30: {
                Get();
                Expression(out g);
                Expect(31);
                Graph.MakeOption(g);
                break;
            }

            case 32: {
                Get();
                Expression(out g);
                Expect(33);
                Graph.MakeIteration(g);
                break;
            }

            case 38: {
                SemText(out pos);
                Node p = new Node(Node.sem, null, 0);
                p.pos = pos;
                g     = new Graph(p);

                break;
            }

            case 23: {
                Get();
                Node p = new Node(Node.any, null, 0);                                  // p.set is set in Tab.SetupAnys
                g = new Graph(p);

                break;
            }

            case 34: {
                Get();
                Node p = new Node(Node.sync, null, 0);
                g = new Graph(p);

                break;
            }

            default: SynErr(53); break;
            }
        }