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