| 
									
										
										
										
											1991-02-19 12:39:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | /* Computation of FIRST stets */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | #include "pgenheaders.h"
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | #include "grammar.h"
 | 
					
						
							|  |  |  | #include "token.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 21:03:06 +00:00
										 |  |  | extern int Py_DebugFlag; | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | /* Forward */ | 
					
						
							| 
									
										
										
										
											2000-07-09 03:09:57 +00:00
										 |  |  | static void calcfirstset(grammar *, dfa *); | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2000-07-22 19:20:54 +00:00
										 |  |  | addfirstsets(grammar *g) | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	dfa *d; | 
					
						
							| 
									
										
										
										
											2001-09-11 16:43:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (Py_DebugFlag) | 
					
						
							|  |  |  | 		printf("Adding FIRST sets ...\n"); | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | 	for (i = 0; i < g->g_ndfas; i++) { | 
					
						
							|  |  |  | 		d = &g->g_dfa[i]; | 
					
						
							|  |  |  | 		if (d->d_first == NULL) | 
					
						
							|  |  |  | 			calcfirstset(g, d); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-22 19:20:54 +00:00
										 |  |  | calcfirstset(grammar *g, dfa *d) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int i, j; | 
					
						
							|  |  |  | 	state *s; | 
					
						
							|  |  |  | 	arc *a; | 
					
						
							|  |  |  | 	int nsyms; | 
					
						
							|  |  |  | 	int *sym; | 
					
						
							|  |  |  | 	int nbits; | 
					
						
							|  |  |  | 	static bitset dummy; | 
					
						
							|  |  |  | 	bitset result; | 
					
						
							|  |  |  | 	int type; | 
					
						
							|  |  |  | 	dfa *d1; | 
					
						
							|  |  |  | 	label *l0; | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1997-04-29 21:03:06 +00:00
										 |  |  | 	if (Py_DebugFlag) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 		printf("Calculate FIRST set for '%s'\n", d->d_name); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if (dummy == NULL) | 
					
						
							|  |  |  | 		dummy = newbitset(1); | 
					
						
							|  |  |  | 	if (d->d_first == dummy) { | 
					
						
							|  |  |  | 		fprintf(stderr, "Left-recursion for '%s'\n", d->d_name); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (d->d_first != NULL) { | 
					
						
							|  |  |  | 		fprintf(stderr, "Re-calculating FIRST set for '%s' ???\n", | 
					
						
							|  |  |  | 			d->d_name); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	d->d_first = dummy; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	l0 = g->g_ll.ll_label; | 
					
						
							|  |  |  | 	nbits = g->g_ll.ll_nlabels; | 
					
						
							|  |  |  | 	result = newbitset(nbits); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | 	sym = (int *)PyObject_MALLOC(sizeof(int)); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 	if (sym == NULL) | 
					
						
							| 
									
										
										
										
											1997-04-29 21:03:06 +00:00
										 |  |  | 		Py_FatalError("no mem for new sym in calcfirstset"); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 	nsyms = 1; | 
					
						
							|  |  |  | 	sym[0] = findlabel(&g->g_ll, d->d_type, (char *)NULL); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	s = &d->d_state[d->d_initial]; | 
					
						
							|  |  |  | 	for (i = 0; i < s->s_narcs; i++) { | 
					
						
							|  |  |  | 		a = &s->s_arc[i]; | 
					
						
							|  |  |  | 		for (j = 0; j < nsyms; j++) { | 
					
						
							|  |  |  | 			if (sym[j] == a->a_lbl) | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (j >= nsyms) { /* New label */ | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | 			sym = (int *)PyObject_REALLOC(sym,  | 
					
						
							|  |  |  |                                                 sizeof(int) * (nsyms + 1)); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 			if (sym == NULL) | 
					
						
							| 
									
										
										
										
											1997-04-29 21:03:06 +00:00
										 |  |  | 				Py_FatalError( | 
					
						
							|  |  |  | 				    "no mem to resize sym in calcfirstset"); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 			sym[nsyms++] = a->a_lbl; | 
					
						
							|  |  |  | 			type = l0[a->a_lbl].lb_type; | 
					
						
							|  |  |  | 			if (ISNONTERMINAL(type)) { | 
					
						
							| 
									
										
										
										
											1997-04-29 21:03:06 +00:00
										 |  |  | 				d1 = PyGrammar_FindDFA(g, type); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 				if (d1->d_first == dummy) { | 
					
						
							|  |  |  | 					fprintf(stderr, | 
					
						
							|  |  |  | 						"Left-recursion below '%s'\n", | 
					
						
							|  |  |  | 						d->d_name); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				else { | 
					
						
							|  |  |  | 					if (d1->d_first == NULL) | 
					
						
							|  |  |  | 						calcfirstset(g, d1); | 
					
						
							| 
									
										
										
										
											1997-04-29 21:03:06 +00:00
										 |  |  | 					mergebitset(result, | 
					
						
							|  |  |  | 						    d1->d_first, nbits); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else if (ISTERMINAL(type)) { | 
					
						
							|  |  |  | 				addbit(result, a->a_lbl); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	d->d_first = result; | 
					
						
							| 
									
										
										
										
											1997-04-29 21:03:06 +00:00
										 |  |  | 	if (Py_DebugFlag) { | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 		printf("FIRST set for '%s': {", d->d_name); | 
					
						
							|  |  |  | 		for (i = 0; i < nbits; i++) { | 
					
						
							|  |  |  | 			if (testbit(result, i)) | 
					
						
							| 
									
										
										
										
											1997-04-29 21:03:06 +00:00
										 |  |  | 				printf(" %s", PyGrammar_LabelRepr(&l0[i])); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		printf(" }\n"); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-03-07 15:39:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-04-21 10:40:58 +00:00
										 |  |  | 	PyObject_FREE(sym); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } |