source: trunk/gsdl/packages/mg-1.3d/src/text/bool_parser.c@ 26

Last change on this file since 26 was 26, checked in by rjmcnab, 25 years ago

Fixed a few problems with the windows build

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 17.8 KB
Line 
1#ifndef lint
2static char yysccsid[] = "@(#)yaccpar 1.9 (Berkeley) 02/21/93";
3#endif
4#define YYBYACC 1
5#define YYMAJOR 1
6#define YYMINOR 9
7#define yyclearin (yychar=(-1))
8#define yyerrok (yyerrflag=0)
9#define YYRECOVERING (yyerrflag!=0)
10#define YYPREFIX "yy"
11#line 25 "bool_parser.y"
12
13#include "sysfuncs.h"
14
15#include "messages.h"
16
17#include "memlib.h"
18#include "words.h"
19#include "stemmer.h"
20#include "term_lists.h"
21#include "bool_tree.h"
22/* [RPAP - Jan 97: Stem Index Change] */
23#include "backend.h" /* for stemmed_dict def */
24#include "stem_search.h"
25
26#include "query_term_list.h" /* [RPAP - Feb 97: Term Frequency] */
27
28/* --- routines --- */
29static int query_lex();
30static int yyerror(char *);
31#define yylex() query_lex(&ch_buf, end_buf)
32
33/* --- module variables --- */
34static char *ch_buf; /* ptr to the character query line buffer */
35static char *end_buf; /* ptr to the last character of the line buffer */
36static bool_tree_node *tree_base = NULL;
37static TermList **term_list;
38static int stem_method;
39/* [RPAP - Jan 97: Stem Index Change] */
40stemmed_dict *p__sd;
41static int indexed;
42/* [RPAP - Feb 97: Term Frequency] */
43static QueryTermList **query_term_list;
44static int word_num;
45static u_long count;
46static u_long doc_count;
47static u_long invf_ptr;
48static u_long invf_len;
49#line 65 "bool_parser.y"
50typedef union {
51 char *text;
52 bool_tree_node *node;
53} YYSTYPE;
54#line 55 "y.tab.c"
55#define TERM 257
56#define YYERRCODE 256
57short yylhs[] = { -1,
58 0, 1, 1, 1, 1, 2, 2, 3, 3, 3,
59 4, 4,
60};
61short yylen[] = { 2,
62 1, 1, 3, 1, 1, 1, 2, 3, 2, 1,
63 3, 1,
64};
65short yydefred[] = { 0,
66 2, 0, 4, 5, 0, 0, 6, 10, 0, 0,
67 0, 7, 0, 9, 0, 3, 8, 0,
68};
69short yydgoto[] = { 6,
70 7, 8, 9, 10,
71};
72short yysindex[] = { -32,
73 0, -32, 0, 0, -32, 0, 0, 0, -33, -118,
74 -37, 0, -32, 0, -32, 0, 0, -33,
75};
76short yyrindex[] = { 0,
77 0, 0, 0, 0, 0, 0, 0, 0, 2, 12,
78 0, 0, 0, 0, 0, 0, 0, 3,
79};
80short yygindex[] = { 0,
81 0, 6, -2, 14,
82};
83#define YYTABLESIZE 225
84short yytable[] = { 5,
85 5, 12, 11, 16, 13, 15, 2, 2, 3, 3,
86 12, 1, 18, 0, 14, 11, 0, 0, 17, 0,
87 0, 0, 0, 14, 0, 0, 0, 0, 0, 0,
88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
89 0, 0, 12, 11, 0, 0, 0, 0, 0, 0,
90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91 0, 4, 4, 0, 0, 0, 0, 0, 0, 0,
92 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93 0, 0, 0, 0, 0, 0, 15, 0, 0, 0,
94 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
97 0, 0, 0, 0, 0, 12, 11, 0, 0, 0,
98 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107 0, 0, 0, 1, 1,
108};
109short yycheck[] = { 33,
110 33, 0, 0, 41, 38, 124, 40, 40, 42, 42,
111 5, 0, 15, -1, 9, 2, -1, -1, 13, -1,
112 -1, -1, -1, 18, -1, -1, -1, -1, -1, -1,
113 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
114 -1, -1, 41, 41, -1, -1, -1, -1, -1, -1,
115 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
116 -1, 95, 95, -1, -1, -1, -1, -1, -1, -1,
117 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
118 -1, -1, -1, -1, -1, -1, 124, -1, -1, -1,
119 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
120 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
121 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
122 -1, -1, -1, -1, -1, 124, 124, -1, -1, -1,
123 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
124 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
125 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
126 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
127 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
128 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
129 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
130 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
131 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
132 -1, -1, -1, 257, 257,
133};
134#define YYFINAL 6
135#ifndef YYDEBUG
136#define YYDEBUG 0
137#endif
138#define YYMAXTOKEN 257
139#if YYDEBUG
140char *yyname[] = {
141"end-of-file",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
142"'!'",0,0,0,0,"'&'",0,"'('","')'","'*'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1430,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"'_'",0,0,0,0,0,
1440,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"'|'",0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1450,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1460,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1470,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
148"TERM",
149};
150char *yyrule[] = {
151"$accept : query",
152"query : or",
153"term : TERM",
154"term : '(' or ')'",
155"term : '*'",
156"term : '_'",
157"not : term",
158"not : '!' not",
159"and : and '&' not",
160"and : and not",
161"and : not",
162"or : or '|' and",
163"or : and",
164};
165#endif
166#ifdef YYSTACKSIZE
167#undef YYMAXDEPTH
168#define YYMAXDEPTH YYSTACKSIZE
169#else
170#ifdef YYMAXDEPTH
171#define YYSTACKSIZE YYMAXDEPTH
172#else
173#define YYSTACKSIZE 500
174#define YYMAXDEPTH 500
175#endif
176#endif
177int yydebug;
178int yynerrs;
179int yyerrflag;
180int yychar;
181short *yyssp;
182YYSTYPE *yyvsp;
183YYSTYPE yyval;
184YYSTYPE yylval;
185short yyss[YYSTACKSIZE];
186YYSTYPE yyvs[YYSTACKSIZE];
187#define yystacksize YYSTACKSIZE
188#line 99 "bool_parser.y"
189
190/* Bison on one mips machine defined "const" to be nothing but
191 then did not undef it */
192#ifdef const
193#undef const
194#endif
195
196/**************************************************************************/
197
198
199/* =========================================================================
200 * Function: query_lex
201 * Description:
202 * Hand written lexical analyser for the parser.
203 * Input:
204 * ptr = ptr to a ptr into character query-line buffer
205 * end = ptr to last char in buffer
206 * Output:
207 * yylval.text = the token's text
208 * Notes:
209 * does NOT produce WILD tokens at the moment
210 * ========================================================================= */
211
212/* [RPAP - Jan 97: Stem Index Change]
213 state mode:
214 0 = Read next token
215 1 = Output word
216 2 = Output '|' or ')'
217 */
218static int query_lex(char **ptr, const char *end)
219{
220 char *buf_ptr = *ptr;
221 static int mode = 0;
222 static int termnum = 0;
223 static TermList *Terms = NULL;
224
225 if (mode == 0)
226 {
227 /* jump over whitespace */
228 while (isspace(*buf_ptr))
229 buf_ptr++;
230
231 if (INAWORD(*buf_ptr))
232 {
233 static char word[MAXSTEMLEN + 1]; /* [RJM 07/98: Memory Leak] */
234 char *sWord = Xmalloc(MAXSTEMLEN + 1);
235 int stem_to_apply, method_using = -1;
236
237 PARSE_STEM_WORD(word, buf_ptr, end);
238
239 /* Extract any parameters */
240 stem_to_apply = stem_method;
241 while (buf_ptr <= end)
242 {
243 int stem_param, param_type;
244 char param[MAXPARAMLEN + 1];
245
246 param_type = 0;
247 PARSE_OPT_TERM_PARAM (param, param_type, buf_ptr, end);
248 if (!param_type)
249 break;
250
251 if (param_type == STEMPARAM)
252 {
253 stem_param = atoi (param);
254 if (errno != ERANGE && indexed && stem_param >= 0 && stem_param <= 3)
255 method_using = stem_to_apply = stem_param;
256 }
257 }
258
259 bcopy ((char *) word, (char *) sWord, *word + 1);
260 stemmer (stem_to_apply, sWord);
261
262 if (stem_to_apply == 0 || !indexed || p__sd == NULL)
263 {
264 /* [RPAP - Feb 97: Term Frequency] */
265 word_num = FindWord (p__sd, sWord, &count, &doc_count, &invf_ptr, &invf_len);
266 if (word_num == -1)
267 count = doc_count = invf_ptr = invf_len = 0;
268 AddQueryTerm (query_term_list, (u_char *) word, count, method_using);
269
270 yylval.text = word;
271 *ptr = buf_ptr; /* fix up ptr */
272 Xfree (sWord);
273 return TERM;
274 }
275 else
276 {
277 *ptr = buf_ptr; /* fix up ptr */
278 termnum = 0;
279 ResetTermList (&Terms);
280 if (FindWords (p__sd, (u_char *) sWord, stem_to_apply, &Terms) > 0)
281 {
282 /* [RPAP - Feb 97: Term Frequency] */
283 int i, freq = 0;
284 for (i = 0; i < Terms->num; i++)
285 freq += Terms->TE[i].WE.count;
286 AddQueryTerm (query_term_list, word, freq, method_using);
287
288 Xfree (sWord);
289 mode = 1;
290 return '(';
291 }
292 else
293 {
294 /* Word does not exists - include in tree anyway */
295 Xfree (sWord);
296
297 /* [RPAP - Feb 97: Term Frequency] */
298 word_num = -1;
299 count = doc_count = invf_ptr = invf_len = 0;
300 AddQueryTerm (query_term_list, (u_char *) word, count, method_using);
301
302 yylval.text = word;
303 return TERM;
304 }
305 }
306 }
307 else /* NON-WORD */
308 {
309 if (*buf_ptr == '\0')
310 {
311 /* return null-char if it is one */
312 *ptr = buf_ptr; /* fix up ptr */
313 return 0;
314 }
315 else
316 {
317 /* return 1st char, and delete from buffer */
318 char c = *buf_ptr++;
319 *ptr = buf_ptr; /* fix up ptr */
320 return c;
321 }
322 }
323 }
324 else if (mode == 1)
325 {
326 yylval.text = Terms->TE[termnum].Word;
327
328 /* [RPAP - Feb 97: Term Frequency] */
329 word_num = Terms->TE[termnum].WE.word_num;
330 count = Terms->TE[termnum].WE.count;
331 doc_count = Terms->TE[termnum].WE.doc_count;
332 invf_ptr = Terms->TE[termnum].WE.invf_ptr;
333 invf_len = Terms->TE[termnum].WE.invf_len;
334
335 termnum++;
336 mode = 2;
337 return TERM;
338 }
339 else /* mode == 2 */
340 {
341 if (termnum >= Terms->num)
342 {
343 mode = 0;
344 return ')';
345 }
346 else
347 {
348 mode = 1;
349 return '|';
350 }
351 }
352}/*query_lex*/
353
354/* =========================================================================
355 * Function: yyerror
356 * Description:
357 * Input:
358 * Output:
359 * ========================================================================= */
360static int yyerror(char *s)
361{
362 Message("%s", s);
363 return(1);
364}
365
366
367/* =========================================================================
368 * Function: ParseBool
369 * Description:
370 * Parse a boolean query string into a term-list and a boolean parse tree
371 * Input:
372 * query_line = query line string
373 * query_len = query line length
374 * the_stem_method = stem method id used for stemming
375 * Output:
376 * the_term_list = the list of terms
377 * res = parser result code
378 * ========================================================================= */
379
380bool_tree_node *
381ParseBool(char *query_line, int query_len,
382 TermList **the_term_list, int the_stem_method, int *res,
383 stemmed_dict * the_sd, int is_indexed, /* [RPAP - Jan 97: Stem Index Change] */
384 QueryTermList **the_query_term_list) /* [RPAP - Feb 97: Term Frequency] */
385{
386 /* global variables to be accessed by bison/yacc created parser */
387 term_list = the_term_list;
388 stem_method = the_stem_method;
389 ch_buf = query_line;
390 end_buf = query_line + query_len;
391 p__sd = the_sd; /* [RPAP - Jan 97: Stem Index Change] */
392 indexed = is_indexed; /* [RPAP - Jan 97: Stem Index Change] */
393 query_term_list = the_query_term_list; /* [RPAP - Feb 97: Term Frequency] */
394
395 FreeBoolTree(&(tree_base));
396
397 ResetTermList(term_list);
398 ResetQueryTermList(query_term_list); /* [RPAP - Feb 97: Term Frequency] */
399
400 *res = yyparse();
401
402 return tree_base;
403}
404
405
406#line 407 "y.tab.c"
407#define YYABORT goto yyabort
408#define YYREJECT goto yyabort
409#define YYACCEPT goto yyaccept
410#define YYERROR goto yyerrlab
411int
412yyparse()
413{
414 register int yym, yyn, yystate;
415#if YYDEBUG
416 register char *yys;
417 extern char *getenv();
418
419 if (yys = getenv("YYDEBUG"))
420 {
421 yyn = *yys;
422 if (yyn >= '0' && yyn <= '9')
423 yydebug = yyn - '0';
424 }
425#endif
426
427 yynerrs = 0;
428 yyerrflag = 0;
429 yychar = (-1);
430
431 yyssp = yyss;
432 yyvsp = yyvs;
433 *yyssp = yystate = 0;
434
435yyloop:
436 if (yyn = yydefred[yystate]) goto yyreduce;
437 if (yychar < 0)
438 {
439 if ((yychar = yylex()) < 0) yychar = 0;
440#if YYDEBUG
441 if (yydebug)
442 {
443 yys = 0;
444 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
445 if (!yys) yys = "illegal-symbol";
446 printf("%sdebug: state %d, reading %d (%s)\n",
447 YYPREFIX, yystate, yychar, yys);
448 }
449#endif
450 }
451 if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&
452 yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
453 {
454#if YYDEBUG
455 if (yydebug)
456 printf("%sdebug: state %d, shifting to state %d\n",
457 YYPREFIX, yystate, yytable[yyn]);
458#endif
459 if (yyssp >= yyss + yystacksize - 1)
460 {
461 goto yyoverflow;
462 }
463 *++yyssp = yystate = yytable[yyn];
464 *++yyvsp = yylval;
465 yychar = (-1);
466 if (yyerrflag > 0) --yyerrflag;
467 goto yyloop;
468 }
469 if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&
470 yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
471 {
472 yyn = yytable[yyn];
473 goto yyreduce;
474 }
475 if (yyerrflag) goto yyinrecovery;
476#ifdef lint
477 goto yynewerror;
478#endif
479yynewerror:
480 yyerror("syntax error");
481#ifdef lint
482 goto yyerrlab;
483#endif
484yyerrlab:
485 ++yynerrs;
486yyinrecovery:
487 if (yyerrflag < 3)
488 {
489 yyerrflag = 3;
490 for (;;)
491 {
492 if ((yyn = yysindex[*yyssp]) && (yyn += YYERRCODE) >= 0 &&
493 yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)
494 {
495#if YYDEBUG
496 if (yydebug)
497 printf("%sdebug: state %d, error recovery shifting\
498 to state %d\n", YYPREFIX, *yyssp, yytable[yyn]);
499#endif
500 if (yyssp >= yyss + yystacksize - 1)
501 {
502 goto yyoverflow;
503 }
504 *++yyssp = yystate = yytable[yyn];
505 *++yyvsp = yylval;
506 goto yyloop;
507 }
508 else
509 {
510#if YYDEBUG
511 if (yydebug)
512 printf("%sdebug: error recovery discarding state %d\n",
513 YYPREFIX, *yyssp);
514#endif
515 if (yyssp <= yyss) goto yyabort;
516 --yyssp;
517 --yyvsp;
518 }
519 }
520 }
521 else
522 {
523 if (yychar == 0) goto yyabort;
524#if YYDEBUG
525 if (yydebug)
526 {
527 yys = 0;
528 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
529 if (!yys) yys = "illegal-symbol";
530 printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
531 YYPREFIX, yystate, yychar, yys);
532 }
533#endif
534 yychar = (-1);
535 goto yyloop;
536 }
537yyreduce:
538#if YYDEBUG
539 if (yydebug)
540 printf("%sdebug: state %d, reducing by rule %d (%s)\n",
541 YYPREFIX, yystate, yyn, yyrule[yyn]);
542#endif
543 yym = yylen[yyn];
544 yyval = yyvsp[1-yym];
545 switch (yyn)
546 {
547case 1:
548#line 75 "bool_parser.y"
549{ tree_base = yyvsp[0].node;}
550break;
551case 2:
552#line 79 "bool_parser.y"
553{ yyval.node = CreateBoolTermNode(term_list, yyvsp[0].text, 1, word_num, count, doc_count, invf_ptr, invf_len); }
554break;
555case 3:
556#line 80 "bool_parser.y"
557{ yyval.node = yyvsp[-1].node; }
558break;
559case 4:
560#line 81 "bool_parser.y"
561{ yyval.node = CreateBoolTreeNode(N_all, NULL, NULL); }
562break;
563case 5:
564#line 82 "bool_parser.y"
565{ yyval.node = CreateBoolTreeNode(N_none, NULL, NULL); }
566break;
567case 7:
568#line 86 "bool_parser.y"
569{ yyval.node = CreateBoolTreeNode(N_not, yyvsp[0].node, NULL); }
570break;
571case 8:
572#line 89 "bool_parser.y"
573{ yyval.node = CreateBoolTreeNode(N_and, yyvsp[-2].node, yyvsp[0].node); }
574break;
575case 9:
576#line 90 "bool_parser.y"
577{ yyval.node = CreateBoolTreeNode(N_and, yyvsp[-1].node, yyvsp[0].node); }
578break;
579case 11:
580#line 94 "bool_parser.y"
581{ yyval.node = CreateBoolTreeNode(N_or, yyvsp[-2].node, yyvsp[0].node); }
582break;
583#line 584 "y.tab.c"
584 }
585 yyssp -= yym;
586 yystate = *yyssp;
587 yyvsp -= yym;
588 yym = yylhs[yyn];
589 if (yystate == 0 && yym == 0)
590 {
591#if YYDEBUG
592 if (yydebug)
593 printf("%sdebug: after reduction, shifting from state 0 to\
594 state %d\n", YYPREFIX, YYFINAL);
595#endif
596 yystate = YYFINAL;
597 *++yyssp = YYFINAL;
598 *++yyvsp = yyval;
599 if (yychar < 0)
600 {
601 if ((yychar = yylex()) < 0) yychar = 0;
602#if YYDEBUG
603 if (yydebug)
604 {
605 yys = 0;
606 if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
607 if (!yys) yys = "illegal-symbol";
608 printf("%sdebug: state %d, reading %d (%s)\n",
609 YYPREFIX, YYFINAL, yychar, yys);
610 }
611#endif
612 }
613 if (yychar == 0) goto yyaccept;
614 goto yyloop;
615 }
616 if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
617 yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
618 yystate = yytable[yyn];
619 else
620 yystate = yydgoto[yym];
621#if YYDEBUG
622 if (yydebug)
623 printf("%sdebug: after reduction, shifting from state %d \
624to state %d\n", YYPREFIX, *yyssp, yystate);
625#endif
626 if (yyssp >= yyss + yystacksize - 1)
627 {
628 goto yyoverflow;
629 }
630 *++yyssp = yystate;
631 *++yyvsp = yyval;
632 goto yyloop;
633yyoverflow:
634 yyerror("yacc stack overflow");
635yyabort:
636 return (1);
637yyaccept:
638 return (0);
639}
Note: See TracBrowser for help on using the repository browser.