Exemplo n.º 1
0
        private static void fixdfa(grammar g, dfa d)
        {
            statePtr s;
            int      j;

            s = new statePtr(d.d_state);
            for (j = 0; j < d.d_nstates; j++, s.inc())
            {
                fixstate(g, s[0]);
            }
        }
Exemplo n.º 2
0
        private static int s_push(stack s, dfa d, node parent)
        {
            stackentry top;

            if (stackentryPtr.equals(s.s_top, s.s_base))
            {
                fprintf(stderr, "s_push: parser stack overflow\n");
                return(E_NOMEM);
            }
            s.s_top.dec(); top = s.s_top[0];
            top.s_dfa          = d;
            top.s_parent       = parent;
            top.s_state        = 0;
            return(0);
        }
Exemplo n.º 3
0
        private static int push(stack s, int type, dfa d, int newstate, int lineno)
        {
            int  err;
            node n;

            n = s.s_top[0].s_parent;
            assert(!s_empty(s));
            err = PyNode_AddChild(n, type, (CharPtr)null, lineno);
            if (0 != err)
            {
                return(err);
            }
            s.s_top[0].s_state = newstate;
            return(s_push(s, d, CHILD(n, NCH(n) - 1)));
        }
Exemplo n.º 4
0
        public static int PyParser_AddToken(parser_state ps, int type, CharPtr str,
                                            int lineno, ref int expected_ret)
        {
            int ilabel;
            int err;

            if (D())
            {
                printf("Token %s/'%s' ... ", _PyParser_TokenNames[type], str);
            }

            ilabel = classify(ps, type, str);
            if (ilabel < 0)
            {
                return(E_SYNTAX);
            }

            for (;;)
            {
                dfa   d = ps.p_stack.s_top[0].s_dfa;
                state s = d.d_state[ps.p_stack.s_top[0].s_state];

                if (D())
                {
                    printf(" DFA '%s', state %d:",
                           d.d_name, ps.p_stack.s_top[0].s_state);
                }

                if (s.s_lower <= ilabel && ilabel < s.s_upper)
                {
                    int x = s.s_accel[ilabel - s.s_lower];
                    if (x != -1)
                    {
                        if (0 != (x & (1 << 7)))
                        {
                            int nt    = (x >> 8) + NT_OFFSET;
                            int arrow = x & ((1 << 7) - 1);
                            dfa d1    = PyGrammar_FindDFA(
                                ps.p_grammar, nt);
                            if ((err = push(ps.p_stack, nt, d1,
                                            arrow, lineno)) > 0)
                            {
                                if (D())
                                {
                                    printf(" MemError: push\n");
                                }
                                return(err);
                            }
                            if (D())
                            {
                                printf(" Push ...\n");
                            }
                            continue;
                        }
                        if ((err = shift(ps.p_stack, type, str,
                                         x, lineno)) > 0)
                        {
                            if (D())
                            {
                                printf(" MemError: shift.\n");
                            }
                            return(err);
                        }
                        if (D())
                        {
                            printf(" Shift.\n");
                        }
                        while (true)
                        {
                            s = d.d_state[ps.p_stack.s_top[0].s_state];
                            if (!(0 != s.s_accept && s.s_narcs == 1))
                            {
                                break;
                            }

                            if (D())
                            {
                                printf("  DFA '%s', state %d: " +
                                       "Direct pop.\n",
                                       d.d_name,
                                       ps.p_stack.s_top[0].s_state);
                            }
                            if (d.d_name[0] == 'i' &&
                                strcmp(d.d_name,
                                       "import_stmt") == 0)
                            {
                                future_hack(ps);
                            }
                            s_pop(ps.p_stack);
                            if (s_empty(ps.p_stack))
                            {
                                if (D())
                                {
                                    printf("  ACCEPT.\n");
                                }
                                return(E_DONE);
                            }
                            d = ps.p_stack.s_top[0].s_dfa;
                        }
                        return(E_OK);
                    }
                }

                if (0 != s.s_accept)
                {
                    if (d.d_name[0] == 'i' &&
                        strcmp(d.d_name, "import_stmt") == 0)
                    {
                        future_hack(ps);
                    }
                    s_pop(ps.p_stack);
                    if (D())
                    {
                        printf(" Pop ...\n");
                    }
                    if (s_empty(ps.p_stack))
                    {
                        if (D())
                        {
                            printf(" Error: bottom of stack.\n");
                        }
                        return(E_SYNTAX);
                    }
                    continue;
                }

                if (D())
                {
                    printf(" Error.\n");
                }
                if (true)                 //if (0!=expected_ret)
                {
                    if (s.s_lower == s.s_upper - 1)
                    {
                        expected_ret = ps.p_grammar.
                                       g_ll.ll_label[s.s_lower].lb_type;
                    }
                    else
                    {
                        expected_ret = -1;
                    }
                }
                return(E_SYNTAX);
            }
        }
Exemplo n.º 5
0
        private static void fixstate(grammar g, state s)
        {
            arcPtr a;
            int    k;
            intPtr accel;
            int    nl = g.g_ll.ll_nlabels;

            s.s_accept = 0;
            accel      = PyMem_NEW_int(nl);
            for (k = 0; k < nl; k++)
            {
                accel[k] = -1;
            }
            a = new arcPtr(s.s_arc);
            for (k = s.s_narcs; --k >= 0; a.inc())
            {
                int   lbl  = a[0].a_lbl;
                label l    = g.g_ll.ll_label[lbl];
                int   type = l.lb_type;
                if (a[0].a_arrow >= (1 << 7))
                {
                    printf("XXX too many states!\n");
                    continue;
                }
                if (ISNONTERMINAL(type))
                {
                    dfa d1 = PyGrammar_FindDFA(g, type);
                    int ibit;
                    if (type - NT_OFFSET >= (1 << 7))
                    {
                        printf("XXX too high nonterminal number!\n");
                        continue;
                    }
                    for (ibit = 0; ibit < g.g_ll.ll_nlabels; ibit++)
                    {
                        if (testbit(d1.d_first, ibit))
                        {
                            if (accel[ibit] != -1)
                            {
                                printf("XXX ambiguity!\n");
                            }
                            accel[ibit] = a[0].a_arrow | (1 << 7) |
                                          ((type - NT_OFFSET) << 8);
                        }
                    }
                }
                else if (lbl == EMPTY)
                {
                    s.s_accept = 1;
                }
                else if (lbl >= 0 && lbl < nl)
                {
                    accel[lbl] = a[0].a_arrow;
                }
            }
            while (nl > 0 && accel[nl - 1] == -1)
            {
                nl--;
            }
            for (k = 0; k < nl && accel[k] == -1;)
            {
                k++;
            }
            if (k < nl)
            {
                int i;
                s.s_accel = PyMem_NEW_int(nl - k);
                if (s.s_accel == null)
                {
                    fprintf(stderr, "no mem to add parser accelerators\n");
                    exit(1);
                }
                s.s_lower = k;
                s.s_upper = nl;
                for (i = 0; k < nl; i++, k++)
                {
                    s.s_accel[i] = accel[k];
                }
            }
            PyMem_DEL(ref accel);
        }