Python-2.7.3/Parser/grammar1.c

No issues found

 1 /* Grammar subroutines needed by parser */
 2 
 3 #include "Python.h"
 4 #include "pgenheaders.h"
 5 #include "grammar.h"
 6 #include "token.h"
 7 
 8 /* Return the DFA for the given type */
 9 
10 dfa *
11 PyGrammar_FindDFA(grammar *g, register int type)
12 {
13     register dfa *d;
14 #if 1
15     /* Massive speed-up */
16     d = &g->g_dfa[type - NT_OFFSET];
17     assert(d->d_type == type);
18     return d;
19 #else
20     /* Old, slow version */
21     register int i;
22 
23     for (i = g->g_ndfas, d = g->g_dfa; --i >= 0; d++) {
24         if (d->d_type == type)
25             return d;
26     }
27     assert(0);
28     /* NOTREACHED */
29 #endif
30 }
31 
32 char *
33 PyGrammar_LabelRepr(label *lb)
34 {
35     static char buf[100];
36 
37     if (lb->lb_type == ENDMARKER)
38         return "EMPTY";
39     else if (ISNONTERMINAL(lb->lb_type)) {
40         if (lb->lb_str == NULL) {
41             PyOS_snprintf(buf, sizeof(buf), "NT%d", lb->lb_type);
42             return buf;
43         }
44         else
45             return lb->lb_str;
46     }
47     else {
48         if (lb->lb_str == NULL)
49             return _PyParser_TokenNames[lb->lb_type];
50         else {
51             PyOS_snprintf(buf, sizeof(buf), "%.32s(%.32s)",
52                 _PyParser_TokenNames[lb->lb_type], lb->lb_str);
53             return buf;
54         }
55     }
56 }