1 /* $Id: cql.y,v 1.7 2004-10-15 00:19:00 adam Exp $
2 Copyright (C) 2002-2004
5 This file is part of the YAZ toolkit.
9 bison parser for CQL grammar.
14 * \brief Implements CQL parser.
16 * This is a YACC parser, but since it must be reentrant, Bison is required.
17 * The original source file is cql.y.
23 #include <yaz/xmalloc.h>
27 /** Node in the LALR parse tree. */
29 /** Inhereted attribute: relation */
31 /** Synthesized attribute: CQL node */
33 /** string buffer with token */
35 /** length of token */
37 /** size of buffer (len <= size) */
42 int (*getbyte)(void *client_data);
43 void (*ungetbyte)(int b, void *client_data);
53 #define YYPARSE_PARAM parm
54 #define YYLEX_PARAM parm
56 int yylex(YYSTYPE *lval, void *vp);
61 %token TERM AND OR NOT PROX GE LE NE
67 $$.rel = cql_node_mk_sc("cql.serverChoice", "scr", 0);
68 ((CQL_parser) parm)->top = 0;
70 cql_node_destroy($$.rel);
71 ((CQL_parser) parm)->top = $2.cql;
77 cql_node_destroy($1.cql);
85 cqlQuery boolean modifiers {
88 struct cql_node *cn = cql_node_mk_boolean($2.buf);
90 cn->u.boolean.modifiers = $3.cql;
91 cn->u.boolean.left = $1.cql;
92 cn->u.boolean.right = $5.cql;
107 struct cql_node *st = cql_node_dup ($0.rel);
108 st->u.st.term = xstrdup($1.buf);
112 index relation modifiers {
113 $$.rel = cql_node_mk_sc($1.buf, $2.buf, 0);
114 $$.rel->u.st.modifiers = $3.cql;
117 cql_node_destroy($4.rel);
119 | '>' searchTerm '=' searchTerm {
122 $$.cql = cql_apply_prefix($6.cql, $2.buf, $4.buf);
127 $$.cql = cql_apply_prefix($4.cql, 0, $2.buf);
131 /* unary NOT search TERM here .. */
134 AND | OR | NOT | PROX
137 modifiers: modifiers '/' searchTerm
139 struct cql_node *mod = cql_node_mk_sc($3.buf, "=", 0);
141 mod->u.st.modifiers = $1.cql;
145 modifiers '/' searchTerm mrelation searchTerm
147 struct cql_node *mod = cql_node_mk_sc($3.buf, $4.buf, $5.buf);
149 mod->u.st.modifiers = $1.cql;
196 * putb is a utility that puts one character to the string
197 * in current lexical token. This routine deallocates as
198 * necessary using NMEM.
201 static void putb(YYSTYPE *lval, CQL_parser cp, int c)
203 if (lval->len+1 >= lval->size)
206 nmem_malloc(cp->nmem, (lval->size = lval->len * 2 + 20));
207 memcpy (nb, lval->buf, lval->len);
211 lval->buf[lval->len++] = c;
212 lval->buf[lval->len] = '\0';
217 * yylex returns next token for Bison to be read. In this
218 * case one of the CQL terminals are returned.
220 int yylex(YYSTYPE *lval, void *vp)
222 CQL_parser cp = (CQL_parser) vp;
228 lval->buf = (char *) nmem_malloc(cp->nmem, lval->size);
232 c = cp->getbyte(cp->client_data);
237 } while (isspace(c));
238 if (strchr("()=></", c))
244 c1 = cp->getbyte(cp->client_data);
251 cp->ungetbyte(c1, cp->client_data);
255 c1 = cp->getbyte(cp->client_data);
267 cp->ungetbyte(c1, cp->client_data);
273 while ((c = cp->getbyte(cp->client_data)) != 0 && c != '"')
276 c = cp->getbyte(cp->client_data);
284 while ((c = cp->getbyte(cp->client_data)) != 0 &&
285 !strchr(" \n()=<>/", c))
288 c = cp->getbyte(cp->client_data);
292 printf ("got %s\n", lval->buf);
295 cp->ungetbyte(c, cp->client_data);
296 if (!strcmp(lval->buf, "and"))
298 if (!strcmp(lval->buf, "or"))
300 if (!strcmp(lval->buf, "not"))
302 if (!strncmp(lval->buf, "prox", 4))
309 int cql_parser_stream(CQL_parser cp,
310 int (*getbyte)(void *client_data),
311 void (*ungetbyte)(int b, void *client_data),
314 nmem_reset(cp->nmem);
315 cp->getbyte = getbyte;
316 cp->ungetbyte = ungetbyte;
317 cp->client_data = client_data;
319 cql_node_destroy(cp->top);
326 CQL_parser cql_parser_create(void)
328 CQL_parser cp = (CQL_parser) xmalloc (sizeof(*cp));
336 cp->nmem = nmem_create();
340 void cql_parser_destroy(CQL_parser cp)
342 cql_node_destroy(cp->top);
343 nmem_destroy(cp->nmem);
347 struct cql_node *cql_parser_result(CQL_parser cp)