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