1/* $Id: graph.c,v 1.8 2014/02/19 00:46:57 Tom.Shields Exp $ */
2
3#include "defs.h"
4
5static void graph_state(int stateno);
6static void graph_LA(int ruleno);
7
8static unsigned int larno;
9
10void
11graph(void)
12{
13    int i;
14    int j;
15    shifts *sp;
16    int sn;
17    int as;
18
19    if (!gflag)
20	return;
21
22    for (i = 0; i < nstates; ++i)
23    {
24	closure(state_table[i]->items, state_table[i]->nitems);
25	graph_state(i);
26    }
27
28    fprintf(graph_file, "\n\n");
29    for (i = 0; i < nstates; ++i)
30    {
31
32	sp = shift_table[i];
33	if (sp)
34	    for (j = 0; j < sp->nshifts; ++j)
35	    {
36		sn = sp->shift[j];
37		as = accessing_symbol[sn];
38		fprintf(graph_file,
39			"\tq%d -> q%d [label=\"%s\"];\n",
40			i, sn, symbol_pname[as]);
41	    }
42    }
43
44    fprintf(graph_file, "}\n");
45
46    for (i = 0; i < nsyms; ++i)
47	FREE(symbol_pname[i]);
48    FREE(symbol_pname);
49}
50
51static void
52graph_state(int stateno)
53{
54    Value_t *isp;
55    int rule;
56    Value_t *sp;
57    Value_t *sp1;
58
59    larno = (unsigned)lookaheads[stateno];
60    fprintf(graph_file, "\n\tq%d [label=\"%d:\\l", stateno, stateno);
61
62    for (isp = itemset; isp < itemsetend; isp++)
63    {
64	sp1 = sp = ritem + *isp;
65
66	while (*sp >= 0)
67	    ++sp;
68	rule = -(*sp);
69	fprintf(graph_file, "  %s -> ", symbol_pname[rlhs[rule]]);
70
71	for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
72	    fprintf(graph_file, "%s ", symbol_pname[*sp]);
73
74	putc('.', graph_file);
75
76	while (*sp >= 0)
77	{
78	    fprintf(graph_file, " %s", symbol_pname[*sp]);
79	    sp++;
80	}
81
82	if (*sp1 < 0)
83	    graph_LA(-*sp1);
84
85	fprintf(graph_file, "\\l");
86    }
87    fprintf(graph_file, "\"];");
88}
89
90static void
91graph_LA(int ruleno)
92{
93    int i;
94    unsigned tokensetsize;
95    unsigned *rowp;
96
97    tokensetsize = (unsigned)WORDSIZE(ntokens);
98
99    if (ruleno == LAruleno[larno])
100    {
101	rowp = LA + larno * tokensetsize;
102
103	fprintf(graph_file, " { ");
104	for (i = ntokens - 1; i >= 0; i--)
105	{
106	    if (BIT(rowp, i))
107		fprintf(graph_file, "%s ", symbol_pname[i]);
108	}
109	fprintf(graph_file, "}");
110	++larno;
111    }
112}
113