xref: /minix3/external/bsd/byacc/dist/graph.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: graph.c,v 1.5 2015/01/03 23:22:52 christos Exp $	*/
24a17663cSThomas Veerman 
34a17663cSThomas Veerman #include "defs.h"
4*0a6a1f1dSLionel Sambuc /* Id: graph.c,v 1.8 2014/02/19 00:46:57 Tom.Shields Exp  */
54a17663cSThomas Veerman 
64a17663cSThomas Veerman #include <sys/cdefs.h>
7*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: graph.c,v 1.5 2015/01/03 23:22:52 christos Exp $");
84a17663cSThomas Veerman 
94a17663cSThomas Veerman static void graph_state(int stateno);
104a17663cSThomas Veerman static void graph_LA(int ruleno);
114a17663cSThomas Veerman 
124a17663cSThomas Veerman static unsigned int larno;
134a17663cSThomas Veerman 
144a17663cSThomas Veerman void
graph(void)154a17663cSThomas Veerman graph(void)
164a17663cSThomas Veerman {
174a17663cSThomas Veerman     int i;
184a17663cSThomas Veerman     int j;
194a17663cSThomas Veerman     shifts *sp;
204a17663cSThomas Veerman     int sn;
214a17663cSThomas Veerman     int as;
224a17663cSThomas Veerman 
234a17663cSThomas Veerman     if (!gflag)
244a17663cSThomas Veerman 	return;
254a17663cSThomas Veerman 
264a17663cSThomas Veerman     for (i = 0; i < nstates; ++i)
274a17663cSThomas Veerman     {
284a17663cSThomas Veerman 	closure(state_table[i]->items, state_table[i]->nitems);
294a17663cSThomas Veerman 	graph_state(i);
304a17663cSThomas Veerman     }
314a17663cSThomas Veerman 
324a17663cSThomas Veerman     fprintf(graph_file, "\n\n");
334a17663cSThomas Veerman     for (i = 0; i < nstates; ++i)
344a17663cSThomas Veerman     {
354a17663cSThomas Veerman 
364a17663cSThomas Veerman 	sp = shift_table[i];
374a17663cSThomas Veerman 	if (sp)
384a17663cSThomas Veerman 	    for (j = 0; j < sp->nshifts; ++j)
394a17663cSThomas Veerman 	    {
404a17663cSThomas Veerman 		sn = sp->shift[j];
414a17663cSThomas Veerman 		as = accessing_symbol[sn];
424a17663cSThomas Veerman 		fprintf(graph_file,
434a17663cSThomas Veerman 			"\tq%d -> q%d [label=\"%s\"];\n",
444a17663cSThomas Veerman 			i, sn, symbol_pname[as]);
454a17663cSThomas Veerman 	    }
464a17663cSThomas Veerman     }
474a17663cSThomas Veerman 
484a17663cSThomas Veerman     fprintf(graph_file, "}\n");
494a17663cSThomas Veerman 
504a17663cSThomas Veerman     for (i = 0; i < nsyms; ++i)
514a17663cSThomas Veerman 	FREE(symbol_pname[i]);
524a17663cSThomas Veerman     FREE(symbol_pname);
534a17663cSThomas Veerman }
544a17663cSThomas Veerman 
554a17663cSThomas Veerman static void
graph_state(int stateno)564a17663cSThomas Veerman graph_state(int stateno)
574a17663cSThomas Veerman {
58*0a6a1f1dSLionel Sambuc     Value_t *isp;
594a17663cSThomas Veerman     int rule;
60*0a6a1f1dSLionel Sambuc     Value_t *sp;
61*0a6a1f1dSLionel Sambuc     Value_t *sp1;
624a17663cSThomas Veerman 
634a17663cSThomas Veerman     larno = (unsigned)lookaheads[stateno];
644a17663cSThomas Veerman     fprintf(graph_file, "\n\tq%d [label=\"%d:\\l", stateno, stateno);
654a17663cSThomas Veerman 
664a17663cSThomas Veerman     for (isp = itemset; isp < itemsetend; isp++)
674a17663cSThomas Veerman     {
684a17663cSThomas Veerman 	sp1 = sp = ritem + *isp;
694a17663cSThomas Veerman 
704a17663cSThomas Veerman 	while (*sp >= 0)
714a17663cSThomas Veerman 	    ++sp;
724a17663cSThomas Veerman 	rule = -(*sp);
734a17663cSThomas Veerman 	fprintf(graph_file, "  %s -> ", symbol_pname[rlhs[rule]]);
744a17663cSThomas Veerman 
754a17663cSThomas Veerman 	for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
764a17663cSThomas Veerman 	    fprintf(graph_file, "%s ", symbol_pname[*sp]);
774a17663cSThomas Veerman 
784a17663cSThomas Veerman 	putc('.', graph_file);
794a17663cSThomas Veerman 
804a17663cSThomas Veerman 	while (*sp >= 0)
814a17663cSThomas Veerman 	{
824a17663cSThomas Veerman 	    fprintf(graph_file, " %s", symbol_pname[*sp]);
834a17663cSThomas Veerman 	    sp++;
844a17663cSThomas Veerman 	}
854a17663cSThomas Veerman 
864a17663cSThomas Veerman 	if (*sp1 < 0)
874a17663cSThomas Veerman 	    graph_LA(-*sp1);
884a17663cSThomas Veerman 
894a17663cSThomas Veerman 	fprintf(graph_file, "\\l");
904a17663cSThomas Veerman     }
914a17663cSThomas Veerman     fprintf(graph_file, "\"];");
924a17663cSThomas Veerman }
934a17663cSThomas Veerman 
944a17663cSThomas Veerman static void
graph_LA(int ruleno)954a17663cSThomas Veerman graph_LA(int ruleno)
964a17663cSThomas Veerman {
974a17663cSThomas Veerman     int i;
984a17663cSThomas Veerman     unsigned tokensetsize;
994a17663cSThomas Veerman     unsigned *rowp;
1004a17663cSThomas Veerman 
1014a17663cSThomas Veerman     tokensetsize = (unsigned)WORDSIZE(ntokens);
1024a17663cSThomas Veerman 
1034a17663cSThomas Veerman     if (ruleno == LAruleno[larno])
1044a17663cSThomas Veerman     {
1054a17663cSThomas Veerman 	rowp = LA + larno * tokensetsize;
1064a17663cSThomas Veerman 
1074a17663cSThomas Veerman 	fprintf(graph_file, " { ");
1084a17663cSThomas Veerman 	for (i = ntokens - 1; i >= 0; i--)
1094a17663cSThomas Veerman 	{
1104a17663cSThomas Veerman 	    if (BIT(rowp, i))
1114a17663cSThomas Veerman 		fprintf(graph_file, "%s ", symbol_pname[i]);
1124a17663cSThomas Veerman 	}
1134a17663cSThomas Veerman 	fprintf(graph_file, "}");
1144a17663cSThomas Veerman 	++larno;
1154a17663cSThomas Veerman     }
1164a17663cSThomas Veerman }
117