grammar1.c revision 23c9e0024af99379ae517b016b874d57127e9a97
1/***********************************************************
2Copyright (c) 2000, BeOpen.com.
3Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5All rights reserved.
6
7See the file "Misc/COPYRIGHT" for information on usage and
8redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9******************************************************************/
10
11/* Grammar subroutines needed by parser */
12
13#include "pgenheaders.h"
14#include "assert.h"
15#include "grammar.h"
16#include "token.h"
17
18/* Return the DFA for the given type */
19
20dfa *
21PyGrammar_FindDFA(grammar *g, register int type)
22{
23	register dfa *d;
24#if 1
25	/* Massive speed-up */
26	d = &g->g_dfa[type - NT_OFFSET];
27	assert(d->d_type == type);
28	return d;
29#else
30	/* Old, slow version */
31	register int i;
32
33	for (i = g->g_ndfas, d = g->g_dfa; --i >= 0; d++) {
34		if (d->d_type == type)
35			return d;
36	}
37	assert(0);
38	/* NOTREACHED */
39#endif
40}
41
42char *
43PyGrammar_LabelRepr(label *lb)
44{
45	static char buf[100];
46
47	if (lb->lb_type == ENDMARKER)
48		return "EMPTY";
49	else if (ISNONTERMINAL(lb->lb_type)) {
50		if (lb->lb_str == NULL) {
51			sprintf(buf, "NT%d", lb->lb_type);
52			return buf;
53		}
54		else
55			return lb->lb_str;
56	}
57	else {
58		if (lb->lb_str == NULL)
59			return _PyParser_TokenNames[lb->lb_type];
60		else {
61			sprintf(buf, "%.32s(%.32s)",
62				_PyParser_TokenNames[lb->lb_type], lb->lb_str);
63			return buf;
64		}
65	}
66}
67