xref: /dragonfly/contrib/byacc/graph.c (revision 5fb3968e)
1 /* $Id: graph.c,v 1.9 2020/09/10 17:22:51 tom Exp $ */
2 
3 #include "defs.h"
4 
5 static void graph_state(int stateno);
6 static void graph_LA(int ruleno);
7 
8 static unsigned int larno;
9 
10 void
11 graph(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 
51 static void
52 graph_state(int stateno)
53 {
54     Value_t *isp;
55     Value_t *sp;
56 
57     larno = (unsigned)lookaheads[stateno];
58     fprintf(graph_file, "\n\tq%d [label=\"%d:\\l", stateno, stateno);
59 
60     for (isp = itemset; isp < itemsetend; isp++)
61     {
62 	Value_t *sp1;
63 	int rule;
64 
65 	sp1 = sp = ritem + *isp;
66 
67 	while (*sp >= 0)
68 	    ++sp;
69 	rule = -(*sp);
70 	fprintf(graph_file, "  %s -> ", symbol_pname[rlhs[rule]]);
71 
72 	for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
73 	    fprintf(graph_file, "%s ", symbol_pname[*sp]);
74 
75 	putc('.', graph_file);
76 
77 	while (*sp >= 0)
78 	{
79 	    fprintf(graph_file, " %s", symbol_pname[*sp]);
80 	    sp++;
81 	}
82 
83 	if (*sp1 < 0)
84 	    graph_LA(-*sp1);
85 
86 	fprintf(graph_file, "\\l");
87     }
88     fprintf(graph_file, "\"];");
89 }
90 
91 static void
92 graph_LA(int ruleno)
93 {
94     unsigned tokensetsize;
95 
96     tokensetsize = (unsigned)WORDSIZE(ntokens);
97 
98     if (ruleno == LAruleno[larno])
99     {
100 	int i;
101 	unsigned *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