PageRenderTime 45ms CodeModel.GetById 35ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Parser/printgrammar.c

http://unladen-swallow.googlecode.com/
C | 117 lines | 100 code | 15 blank | 2 comment | 10 complexity | e628e1a016663bdd1735fbf51739fb83 MD5 | raw file
  1
  2/* Print a bunch of C initializers that represent a grammar */
  3
  4#include "pgenheaders.h"
  5#include "grammar.h"
  6
  7/* Forward */
  8static void printarcs(int, dfa *, FILE *);
  9static void printstates(grammar *, FILE *);
 10static void printdfas(grammar *, FILE *);
 11static void printlabels(grammar *, FILE *);
 12
 13void
 14printgrammar(grammar *g, FILE *fp)
 15{
 16	fprintf(fp, "/* Generated by Parser/pgen */\n\n");
 17	fprintf(fp, "#include \"pgenheaders.h\"\n");
 18	fprintf(fp, "#include \"grammar.h\"\n");
 19	fprintf(fp, "PyAPI_DATA(grammar) _PyParser_Grammar;\n");
 20	printdfas(g, fp);
 21	printlabels(g, fp);
 22	fprintf(fp, "grammar _PyParser_Grammar = {\n");
 23	fprintf(fp, "\t%d,\n", g->g_ndfas);
 24	fprintf(fp, "\tdfas,\n");
 25	fprintf(fp, "\t{%d, labels},\n", g->g_ll.ll_nlabels);
 26	fprintf(fp, "\t%d\n", g->g_start);
 27	fprintf(fp, "};\n");
 28}
 29
 30void
 31printnonterminals(grammar *g, FILE *fp)
 32{
 33	dfa *d;
 34	int i;
 35	
 36	fprintf(fp, "/* Generated by Parser/pgen */\n\n");
 37	
 38	d = g->g_dfa;
 39	for (i = g->g_ndfas; --i >= 0; d++)
 40		fprintf(fp, "#define %s %d\n", d->d_name, d->d_type);
 41}
 42
 43static void
 44printarcs(int i, dfa *d, FILE *fp)
 45{
 46	arc *a;
 47	state *s;
 48	int j, k;
 49	
 50	s = d->d_state;
 51	for (j = 0; j < d->d_nstates; j++, s++) {
 52		fprintf(fp, "static arc arcs_%d_%d[%d] = {\n",
 53			i, j, s->s_narcs);
 54		a = s->s_arc;
 55		for (k = 0; k < s->s_narcs; k++, a++)
 56			fprintf(fp, "\t{%d, %d},\n", a->a_lbl, a->a_arrow);
 57		fprintf(fp, "};\n");
 58	}
 59}
 60
 61static void
 62printstates(grammar *g, FILE *fp)
 63{
 64	state *s;
 65	dfa *d;
 66	int i, j;
 67	
 68	d = g->g_dfa;
 69	for (i = 0; i < g->g_ndfas; i++, d++) {
 70		printarcs(i, d, fp);
 71		fprintf(fp, "static state states_%d[%d] = {\n",
 72			i, d->d_nstates);
 73		s = d->d_state;
 74		for (j = 0; j < d->d_nstates; j++, s++)
 75			fprintf(fp, "\t{%d, arcs_%d_%d},\n",
 76				s->s_narcs, i, j);
 77		fprintf(fp, "};\n");
 78	}
 79}
 80
 81static void
 82printdfas(grammar *g, FILE *fp)
 83{
 84	dfa *d;
 85	int i, j;
 86	
 87	printstates(g, fp);
 88	fprintf(fp, "static dfa dfas[%d] = {\n", g->g_ndfas);
 89	d = g->g_dfa;
 90	for (i = 0; i < g->g_ndfas; i++, d++) {
 91		fprintf(fp, "\t{%d, \"%s\", %d, %d, states_%d,\n",
 92			d->d_type, d->d_name, d->d_initial, d->d_nstates, i);
 93		fprintf(fp, "\t \"");
 94		for (j = 0; j < NBYTES(g->g_ll.ll_nlabels); j++)
 95			fprintf(fp, "\\%03o", d->d_first[j] & 0xff);
 96		fprintf(fp, "\"},\n");
 97	}
 98	fprintf(fp, "};\n");
 99}
100
101static void
102printlabels(grammar *g, FILE *fp)
103{
104	label *l;
105	int i;
106	
107	fprintf(fp, "static label labels[%d] = {\n", g->g_ll.ll_nlabels);
108	l = g->g_ll.ll_label;
109	for (i = g->g_ll.ll_nlabels; --i >= 0; l++) {
110		if (l->lb_str == NULL)
111			fprintf(fp, "\t{%d, 0},\n", l->lb_type);
112		else
113			fprintf(fp, "\t{%d, \"%s\"},\n",
114				l->lb_type, l->lb_str);
115	}
116	fprintf(fp, "};\n");
117}