source: trunk/gsdl/packages/mg/src/text/bool_parser.c@ 531

Last change on this file since 531 was 531, checked in by sjboddie, 25 years ago

* empty log message *

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