blob: b76719a1d5ba2992f5843f919466ce597bcd2fda [file] [log] [blame]
Barry Warsaw5947af52001-11-28 21:34:34 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Grammar subroutines needed by parser */
3
Tim Peters1ca12962001-12-04 03:18:48 +00004#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006#include "grammar.h"
7#include "token.h"
8
9/* Return the DFA for the given type */
10
11dfa *
Thomas Wouters23c9e002000-07-22 19:20:54 +000012PyGrammar_FindDFA(grammar *g, register int type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014 register dfa *d;
Guido van Rossum6522eb01994-09-09 11:11:39 +000015#if 1
16 /* Massive speed-up */
17 d = &g->g_dfa[type - NT_OFFSET];
18 assert(d->d_type == type);
19 return d;
20#else
21 /* Old, slow version */
22 register int i;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000023
24 for (i = g->g_ndfas, d = g->g_dfa; --i >= 0; d++) {
25 if (d->d_type == type)
26 return d;
27 }
28 assert(0);
29 /* NOTREACHED */
Guido van Rossum6522eb01994-09-09 11:11:39 +000030#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031}
32
33char *
Thomas Wouters23c9e002000-07-22 19:20:54 +000034PyGrammar_LabelRepr(label *lb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035{
36 static char buf[100];
37
38 if (lb->lb_type == ENDMARKER)
39 return "EMPTY";
40 else if (ISNONTERMINAL(lb->lb_type)) {
41 if (lb->lb_str == NULL) {
Tim Peters88e138c2001-12-04 03:36:01 +000042 PyOS_snprintf(buf, sizeof(buf), "NT%d", lb->lb_type);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043 return buf;
44 }
45 else
46 return lb->lb_str;
47 }
48 else {
49 if (lb->lb_str == NULL)
Guido van Rossum86bea461997-04-29 21:03:06 +000050 return _PyParser_TokenNames[lb->lb_type];
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051 else {
Tim Peters88e138c2001-12-04 03:36:01 +000052 PyOS_snprintf(buf, sizeof(buf), "%.32s(%.32s)",
Barry Warsaw5947af52001-11-28 21:34:34 +000053 _PyParser_TokenNames[lb->lb_type], lb->lb_str);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054 return buf;
55 }
56 }
57}