Barry Warsaw | 5947af5 | 2001-11-28 21:34:34 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Grammar subroutines needed by parser */ |
| 3 | |
Tim Peters | 1ca1296 | 2001-12-04 03:18:48 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | #include "grammar.h" |
| 7 | #include "token.h" |
| 8 | |
| 9 | /* Return the DFA for the given type */ |
| 10 | |
| 11 | dfa * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 12 | PyGrammar_FindDFA(grammar *g, register int type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14 | register dfa *d; |
Guido van Rossum | 6522eb0 | 1994-09-09 11:11:39 +0000 | [diff] [blame] | 15 | #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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 23 | |
| 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 Rossum | 6522eb0 | 1994-09-09 11:11:39 +0000 | [diff] [blame] | 30 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | char * |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 34 | PyGrammar_LabelRepr(label *lb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | { |
| 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 Peters | 88e138c | 2001-12-04 03:36:01 +0000 | [diff] [blame] | 42 | PyOS_snprintf(buf, sizeof(buf), "NT%d", lb->lb_type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 43 | return buf; |
| 44 | } |
| 45 | else |
| 46 | return lb->lb_str; |
| 47 | } |
| 48 | else { |
| 49 | if (lb->lb_str == NULL) |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 50 | return _PyParser_TokenNames[lb->lb_type]; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 51 | else { |
Tim Peters | 88e138c | 2001-12-04 03:36:01 +0000 | [diff] [blame] | 52 | PyOS_snprintf(buf, sizeof(buf), "%.32s(%.32s)", |
Barry Warsaw | 5947af5 | 2001-11-28 21:34:34 +0000 | [diff] [blame] | 53 | _PyParser_TokenNames[lb->lb_type], lb->lb_str); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | return buf; |
| 55 | } |
| 56 | } |
| 57 | } |