Back to index

python3.2  3.2.2
Classes | Defines | Functions
grammar.h File Reference
#include "bitset.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  label
struct  labellist
struct  arc
struct  state
struct  dfa
struct  grammar

Defines

#define EMPTY   0 /* Label number 0 is by definition the empty label */

Functions

grammarnewgrammar (int start)
dfaadddfa (grammar *g, int type, char *name)
int addstate (dfa *d)
void addarc (dfa *d, int from, int to, int lbl)
dfaPyGrammar_FindDFA (grammar *g, int type)
int addlabel (labellist *ll, int type, char *str)
int findlabel (labellist *ll, int type, char *str)
char * PyGrammar_LabelRepr (label *lb)
void translatelabels (grammar *g)
void addfirstsets (grammar *g)
void PyGrammar_AddAccelerators (grammar *g)
void PyGrammar_RemoveAccelerators (grammar *)
void printgrammar (grammar *g, FILE *fp)
void printnonterminals (grammar *g, FILE *fp)

Class Documentation

struct label

Definition at line 14 of file grammar.h.

Class Members
char * lb_str
int lb_type
struct labellist

Definition at line 23 of file grammar.h.

Collaboration diagram for labellist:
Class Members
label * ll_label
int ll_nlabels
struct arc

Definition at line 30 of file grammar.h.

Class Members
short a_arrow
short a_lbl
struct state

Definition at line 37 of file grammar.h.

Collaboration diagram for state:
Class Members
int * s_accel
int s_accept
arc * s_arc
int s_lower
int s_narcs
int s_upper
struct dfa

Definition at line 50 of file grammar.h.

Collaboration diagram for dfa:
Class Members
bitset d_first
int d_initial
char * d_name
int d_nstates
state * d_state
int d_type
struct grammar

Definition at line 61 of file grammar.h.

Collaboration diagram for grammar:
Class Members
int g_accel
dfa * g_dfa
labellist g_ll
int g_ndfas
int g_start

Define Documentation

#define EMPTY   0 /* Label number 0 is by definition the empty label */

Definition at line 19 of file grammar.h.


Function Documentation

void addarc ( dfa d,
int  from,
int  to,
int  lbl 
)

Definition at line 70 of file grammar.c.

{
    state *s;
    arc *a;

    assert(0 <= from && from < d->d_nstates);
    assert(0 <= to && to < d->d_nstates);

    s = &d->d_state[from];
    s->s_arc = (arc *)PyObject_REALLOC(s->s_arc, sizeof(arc) * (s->s_narcs + 1));
    if (s->s_arc == NULL)
        Py_FatalError("no mem to resize arc list in addarc");
    a = &s->s_arc[s->s_narcs++];
    a->a_lbl = lbl;
    a->a_arrow = to;
}

Here is the call graph for this function:

dfa* adddfa ( grammar g,
int  type,
char *  name 
)

Definition at line 32 of file grammar.c.

{
    dfa *d;

    g->g_dfa = (dfa *)PyObject_REALLOC(g->g_dfa,
                                        sizeof(dfa) * (g->g_ndfas + 1));
    if (g->g_dfa == NULL)
        Py_FatalError("no mem to resize dfa in adddfa");
    d = &g->g_dfa[g->g_ndfas++];
    d->d_type = type;
    d->d_name = strdup(name);
    d->d_nstates = 0;
    d->d_state = NULL;
    d->d_initial = -1;
    d->d_first = NULL;
    return d; /* Only use while fresh! */
}

Here is the call graph for this function:

Definition at line 14 of file firstsets.c.

{
    int i;
    dfa *d;

    if (Py_DebugFlag)
        printf("Adding FIRST sets ...\n");
    for (i = 0; i < g->g_ndfas; i++) {
        d = &g->g_dfa[i];
        if (d->d_first == NULL)
            calcfirstset(g, d);
    }
}

Here is the call graph for this function:

int addlabel ( labellist ll,
int  type,
char *  str 
)

Definition at line 88 of file grammar.c.

{
    int i;
    label *lb;

    for (i = 0; i < ll->ll_nlabels; i++) {
        if (ll->ll_label[i].lb_type == type &&
            strcmp(ll->ll_label[i].lb_str, str) == 0)
            return i;
    }
    ll->ll_label = (label *)PyObject_REALLOC(ll->ll_label,
                                    sizeof(label) * (ll->ll_nlabels + 1));
    if (ll->ll_label == NULL)
        Py_FatalError("no mem to resize labellist in addlabel");
    lb = &ll->ll_label[ll->ll_nlabels++];
    lb->lb_type = type;
    lb->lb_str = strdup(str);
    if (Py_DebugFlag)
        printf("Label @ %8p, %d: %s\n", ll, ll->ll_nlabels,
               PyGrammar_LabelRepr(lb));
    return lb - ll->ll_label;
}

Here is the call graph for this function:

int addstate ( dfa d)

Definition at line 51 of file grammar.c.

{
    state *s;

    d->d_state = (state *)PyObject_REALLOC(d->d_state,
                                  sizeof(state) * (d->d_nstates + 1));
    if (d->d_state == NULL)
        Py_FatalError("no mem to resize state in addstate");
    s = &d->d_state[d->d_nstates++];
    s->s_narcs = 0;
    s->s_arc = NULL;
    s->s_lower = 0;
    s->s_upper = 0;
    s->s_accel = NULL;
    s->s_accept = 0;
    return s - d->d_state;
}

Here is the call graph for this function:

int findlabel ( labellist ll,
int  type,
char *  str 
)

Definition at line 114 of file grammar.c.

{
    int i;

    for (i = 0; i < ll->ll_nlabels; i++) {
        if (ll->ll_label[i].lb_type == type /*&&
            strcmp(ll->ll_label[i].lb_str, str) == 0*/)
            return i;
    }
    fprintf(stderr, "Label %d/'%s' not found\n", type, str);
    Py_FatalError("grammar.c:findlabel()");
    return 0; /* Make gcc -Wall happy */
}

Here is the call graph for this function:

grammar* newgrammar ( int  start)

Definition at line 15 of file grammar.c.

{
    grammar *g;

    g = (grammar *)PyObject_MALLOC(sizeof(grammar));
    if (g == NULL)
        Py_FatalError("no mem for new grammar");
    g->g_ndfas = 0;
    g->g_dfa = NULL;
    g->g_start = start;
    g->g_ll.ll_nlabels = 0;
    g->g_ll.ll_label = NULL;
    g->g_accel = 0;
    return g;
}

Here is the call graph for this function:

void printgrammar ( grammar g,
FILE *  fp 
)

Definition at line 16 of file printgrammar.c.

{
    fprintf(fp, "/* Generated by Parser/pgen */\n\n");
    fprintf(fp, "#include \"pgenheaders.h\"\n");
    fprintf(fp, "#include \"grammar.h\"\n");
    fprintf(fp, "PyAPI_DATA(grammar) _PyParser_Grammar;\n");
    printdfas(g, fp);
    printlabels(g, fp);
    fprintf(fp, "grammar _PyParser_Grammar = {\n");
    fprintf(fp, "    %d,\n", g->g_ndfas);
    fprintf(fp, "    dfas,\n");
    fprintf(fp, "    {%d, labels},\n", g->g_ll.ll_nlabels);
    fprintf(fp, "    %d\n", g->g_start);
    fprintf(fp, "};\n");
}

Here is the call graph for this function:

void printnonterminals ( grammar g,
FILE *  fp 
)

Definition at line 33 of file printgrammar.c.

{
    dfa *d;
    int i;

    fprintf(fp, "/* Generated by Parser/pgen */\n\n");

    d = g->g_dfa;
    for (i = g->g_ndfas; --i >= 0; d++)
        fprintf(fp, "#define %s %d\n", d->d_name, d->d_type);
}

Definition at line 24 of file acceler.c.

{
    dfa *d;
    int i;
    d = g->g_dfa;
    for (i = g->g_ndfas; --i >= 0; d++)
        fixdfa(g, d);
    g->g_accel = 1;
}

Here is the caller graph for this function:

dfa* PyGrammar_FindDFA ( grammar g,
int  type 
)

Here is the caller graph for this function:

char* PyGrammar_LabelRepr ( label lb)

Definition at line 34 of file grammar1.c.

{
    static char buf[100];

    if (lb->lb_type == ENDMARKER)
        return "EMPTY";
    else if (ISNONTERMINAL(lb->lb_type)) {
        if (lb->lb_str == NULL) {
            PyOS_snprintf(buf, sizeof(buf), "NT%d", lb->lb_type);
            return buf;
        }
        else
            return lb->lb_str;
    }
    else {
        if (lb->lb_str == NULL)
            return _PyParser_TokenNames[lb->lb_type];
        else {
            PyOS_snprintf(buf, sizeof(buf), "%.32s(%.32s)",
                _PyParser_TokenNames[lb->lb_type], lb->lb_str);
            return buf;
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 35 of file acceler.c.

{
    dfa *d;
    int i;
    g->g_accel = 0;
    d = g->g_dfa;
    for (i = g->g_ndfas; --i >= 0; d++) {
        state *s;
        int j;
        s = d->d_state;
        for (j = 0; j < d->d_nstates; j++, s++) {
            if (s->s_accel)
                PyObject_FREE(s->s_accel);
            s->s_accel = NULL;
        }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 132 of file grammar.c.

{
    int i;

#ifdef Py_DEBUG
    printf("Translating labels ...\n");
#endif
    /* Don't translate EMPTY */
    for (i = EMPTY+1; i < g->g_ll.ll_nlabels; i++)
        translabel(g, &g->g_ll.ll_label[i]);
}

Here is the call graph for this function: