| 
									
										
										
										
											1991-02-19 12:39:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | /* Parser accelerator module */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | /* The parser as originally conceived had disappointing performance.
 | 
					
						
							|  |  |  |    This module does some precomputation that speeds up the selection | 
					
						
							|  |  |  |    of a DFA based upon a token, turning a search through an array | 
					
						
							|  |  |  |    into a simple indexing operation.  The parser now cannot work | 
					
						
							|  |  |  |    without the accelerators installed.  Note that the accelerators | 
					
						
							|  |  |  |    are installed dynamically when the parser is initialized, they | 
					
						
							|  |  |  |    are not part of the static data structure written on graminit.[ch] | 
					
						
							|  |  |  |    by the parser generator. */ | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | #include "pgenheaders.h"
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | #include "grammar.h"
 | 
					
						
							| 
									
										
										
										
											1994-08-30 08:27:36 +00:00
										 |  |  | #include "node.h"
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | #include "token.h"
 | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | #include "parser.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Forward references */ | 
					
						
							| 
									
										
										
										
											2000-07-09 03:09:57 +00:00
										 |  |  | static void fixdfa(grammar *, dfa *); | 
					
						
							|  |  |  | static void fixstate(grammar *, state *); | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2000-07-22 19:20:54 +00:00
										 |  |  | PyGrammar_AddAccelerators(grammar *g) | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	dfa *d; | 
					
						
							|  |  |  | 	int i; | 
					
						
							| 
									
										
										
										
											1996-12-30 16:17:54 +00:00
										 |  |  | #ifdef Py_DEBUG
 | 
					
						
							| 
									
										
										
										
											1992-09-03 20:45:24 +00:00
										 |  |  | 	fprintf(stderr, "Adding parser accelerators ...\n"); | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 	d = g->g_dfa; | 
					
						
							|  |  |  | 	for (i = g->g_ndfas; --i >= 0; d++) | 
					
						
							|  |  |  | 		fixdfa(g, d); | 
					
						
							|  |  |  | 	g->g_accel = 1; | 
					
						
							| 
									
										
										
										
											1996-12-30 16:17:54 +00:00
										 |  |  | #ifdef Py_DEBUG
 | 
					
						
							| 
									
										
										
										
											1992-09-03 20:45:24 +00:00
										 |  |  | 	fprintf(stderr, "Done.\n"); | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-08-02 03:02:27 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2000-07-22 19:20:54 +00:00
										 |  |  | PyGrammar_RemoveAccelerators(grammar *g) | 
					
						
							| 
									
										
										
										
											1997-08-02 03:02:27 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	dfa *d; | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	g->g_accel = 0; | 
					
						
							|  |  |  | 	d = g->g_dfa; | 
					
						
							|  |  |  | 	for (i = g->g_ndfas; --i >= 0; d++) { | 
					
						
							|  |  |  | 		state *s; | 
					
						
							|  |  |  | 		int j; | 
					
						
							|  |  |  | 		s = d->d_state; | 
					
						
							|  |  |  | 		for (j = 0; j < d->d_nstates; j++, s++) { | 
					
						
							|  |  |  | 			if (s->s_accel) | 
					
						
							|  |  |  | 				PyMem_DEL(s->s_accel); | 
					
						
							|  |  |  | 			s->s_accel = NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-22 19:20:54 +00:00
										 |  |  | fixdfa(grammar *g, dfa *d) | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	state *s; | 
					
						
							|  |  |  | 	int j; | 
					
						
							|  |  |  | 	s = d->d_state; | 
					
						
							|  |  |  | 	for (j = 0; j < d->d_nstates; j++, s++) | 
					
						
							| 
									
										
										
										
											1992-03-27 17:24:37 +00:00
										 |  |  | 		fixstate(g, s); | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-22 19:20:54 +00:00
										 |  |  | fixstate(grammar *g, state *s) | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	arc *a; | 
					
						
							|  |  |  | 	int k; | 
					
						
							|  |  |  | 	int *accel; | 
					
						
							|  |  |  | 	int nl = g->g_ll.ll_nlabels; | 
					
						
							|  |  |  | 	s->s_accept = 0; | 
					
						
							| 
									
										
										
										
											1997-04-29 21:03:06 +00:00
										 |  |  | 	accel = PyMem_NEW(int, nl); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 	for (k = 0; k < nl; k++) | 
					
						
							|  |  |  | 		accel[k] = -1; | 
					
						
							|  |  |  | 	a = s->s_arc; | 
					
						
							|  |  |  | 	for (k = s->s_narcs; --k >= 0; a++) { | 
					
						
							|  |  |  | 		int lbl = a->a_lbl; | 
					
						
							|  |  |  | 		label *l = &g->g_ll.ll_label[lbl]; | 
					
						
							|  |  |  | 		int type = l->lb_type; | 
					
						
							|  |  |  | 		if (a->a_arrow >= (1 << 7)) { | 
					
						
							|  |  |  | 			printf("XXX too many states!\n"); | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (ISNONTERMINAL(type)) { | 
					
						
							| 
									
										
										
										
											1997-04-29 21:03:06 +00:00
										 |  |  | 			dfa *d1 = PyGrammar_FindDFA(g, type); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 			int ibit; | 
					
						
							|  |  |  | 			if (type - NT_OFFSET >= (1 << 7)) { | 
					
						
							|  |  |  | 				printf("XXX too high nonterminal number!\n"); | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			for (ibit = 0; ibit < g->g_ll.ll_nlabels; ibit++) { | 
					
						
							|  |  |  | 				if (testbit(d1->d_first, ibit)) { | 
					
						
							| 
									
										
										
										
											1991-09-10 14:53:39 +00:00
										 |  |  | #ifdef applec
 | 
					
						
							| 
									
										
										
										
											1994-08-30 08:27:36 +00:00
										 |  |  | #define MPW_881_BUG			/* Undefine if bug below is fixed */
 | 
					
						
							| 
									
										
										
										
											1991-09-10 14:53:39 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #ifdef MPW_881_BUG
 | 
					
						
							|  |  |  | 					/* In 881 mode MPW 3.1 has a code
 | 
					
						
							|  |  |  | 					   generation bug which seems to | 
					
						
							|  |  |  | 					   set the upper bits; fix this by | 
					
						
							|  |  |  | 					   explicitly masking them off */ | 
					
						
							|  |  |  | 					int temp; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 					if (accel[ibit] != -1) | 
					
						
							|  |  |  | 						printf("XXX ambiguity!\n"); | 
					
						
							| 
									
										
										
										
											1991-09-10 14:53:39 +00:00
										 |  |  | #ifdef MPW_881_BUG
 | 
					
						
							|  |  |  | 					temp = 0xFFFF & | 
					
						
							|  |  |  | 						(a->a_arrow | (1 << 7) | | 
					
						
							|  |  |  | 						 ((type - NT_OFFSET) << 8)); | 
					
						
							|  |  |  | 					accel[ibit] = temp; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 					accel[ibit] = a->a_arrow | (1 << 7) | | 
					
						
							|  |  |  | 						((type - NT_OFFSET) << 8); | 
					
						
							| 
									
										
										
										
											1991-09-10 14:53:39 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else if (lbl == EMPTY) | 
					
						
							|  |  |  | 			s->s_accept = 1; | 
					
						
							|  |  |  | 		else if (lbl >= 0 && lbl < nl) | 
					
						
							|  |  |  | 			accel[lbl] = a->a_arrow; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	while (nl > 0 && accel[nl-1] == -1) | 
					
						
							|  |  |  | 		nl--; | 
					
						
							|  |  |  | 	for (k = 0; k < nl && accel[k] == -1;) | 
					
						
							|  |  |  | 		k++; | 
					
						
							|  |  |  | 	if (k < nl) { | 
					
						
							|  |  |  | 		int i; | 
					
						
							| 
									
										
										
										
											1997-04-29 21:03:06 +00:00
										 |  |  | 		s->s_accel = PyMem_NEW(int, nl-k); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 		if (s->s_accel == NULL) { | 
					
						
							|  |  |  | 			fprintf(stderr, "no mem to add parser accelerators\n"); | 
					
						
							|  |  |  | 			exit(1); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		s->s_lower = k; | 
					
						
							|  |  |  | 		s->s_upper = nl; | 
					
						
							|  |  |  | 		for (i = 0; k < nl; i++, k++) | 
					
						
							|  |  |  | 			s->s_accel[i] = accel[k]; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-04-29 21:03:06 +00:00
										 |  |  | 	PyMem_DEL(accel); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } |