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

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

* empty log message *

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