%{ /* $Id: jqp.y,v 1.22 2005/09/04 01:08:35 joshr Exp $ */ /* THIS is where we put the headers for the yaccer */ #include "jqplib.h" #include "bintree.h" #include /* for malloc */ %} %union { struct bintree *tree; } /* new way */ /* This is called 'yylval.tree' in lexer jqp.l, */ /* and is 'struct bintree*' is the type of $$, $1, $2, $3 below */ %token AND OR NOT OPAREN CPAREN WORD EQUALS QSTRING /* * our tokens are: * binary operators, unary operators, words, equals, quoted strings, and parentheses */ %type query mterm qterm /* set the type for these to 'tree *' also */ /* precedence started from the working example at */ /* http://cvs.freedesktop.org/xorg/xc/programs/xkbevd/cfgparse.y? */ /* and on the example in K&P p. 243 */ /* NOTE: no need for precedence for CPAREN */ /* NOTE: EQUALS is NON-associative: a=b=c not allowed */ /* JUXT-aposition precedence added at suggestion of IRC folks. */ %left OR /* lowest prec */ %left AND JUXT %left NOT %nonassoc EQUALS /* non associative. prop=a=term is not allowed */ /* %right CPAREN */ /* not needed */ %left OPAREN /* highest precedence */ /* this grammar was once upon a time was based on the example */ /* in K&P, p. 243 */ /* $$ is the 'result' of the rule matching. */ /* $1 ... $n are the yllval.tree tokens that matched */ /* setTree just sets the global gTree to the top of the current parse tree representation ($$) */ %% /* a query, built up of one or more mterms */ /* highest precedence last */ /* for JUXT: since no AND node was created for JUXT, we make an AND node (so JUXT == AND) */ query: mterm { $$ = $1; setTree($$); } | query OR query { $$ = $2; $$->left = $1; $$->right = $3; setTree($$); } | query AND query { $$ = $2; $$->left = $1; $$->right = $3; setTree($$); } | query query %prec JUXT { $$ = init_new_node("AND", AND, $1, $2); setTree($$); } | NOT query { $$ = $1; $$->right = $2; setTree($$); } | OPAREN query CPAREN { $$ = $2; setTree($$); } ; /* meta-ized queryterm: with 0 or one meta names. Not more. */ mterm: qterm { $$ = $1; setTree($$); } | WORD EQUALS qterm { $$ = $2; $$->left = $1; $$->right = $3; setTree($$); } /* qterm is a single sequence of chars to be searched on, without a metaname, optionally quoted */ qterm: WORD { $$ = $1; setTree($$); } | QSTRING { $$ = $1; setTree($$); } ; %% /* grammar end */