Skip to content

Instantly share code, notes, and snippets.

@mdciotti
Last active October 16, 2015 06:30
Show Gist options
  • Save mdciotti/673ca375de06d08c36ae to your computer and use it in GitHub Desktop.
Save mdciotti/673ca375de06d08c36ae to your computer and use it in GitHub Desktop.
/* SEARCH QUERY BISON SOURCE */
%left '|'
%left '&' '!'
%left '(' ')'
%start file
%%
file
: exp EOF
{ return $1; }
;
exp
: '(' exp ')'
{ $$ = $2; }
| exp '|' exp
{ $$ = ["union", $1, $3]; }
| exp '&' exp
{ $$ = ["intersect", $1, $3]; }
| exp '!' exp
{ $$ = ["difference", $1, $3]; }
| term
{ $$ = ["term", $1]; }
;
term
: TERM
{ $$ = yytext; }
;
/* SEARCH QUERY FLEX SOURCE */
/* TERM [^()|&!\s]+ */
TERM [[:alphanum:]]+
%%
\s+ { /* skip whitespace */ }
/* {TERM}(\s+{TERM})* { return 'T'; } */
{TERM} { return 'T'; }
"(" { return '('; }
")" { return ')'; }
"|" { return '|'; }
"&" { return '&'; }
"!" { return '!'; }
<<EOF>> { return EOF; }
/* SEARCH QUERY */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
enum Operator { // Actions
TERM,
UNION,
INTERSECTION,
EXCLUSION
};
// ARRAY OUTPUT SYNTAX
// ["union", ["exclusion", ["term", "cat"], ["term", "food"]], ["term", "dog"]]
// ["union", ["term", "cat"], ["term", "dog"]]
// ["term", "cat"]
// PREFERRED OUTPUT SYNTAX
// (intersection (exclusion (term cat) (term food)) (term dog))
// (union (term cat) (term dog))
// (term cat)
// (2 (0 dog) (3 (0 cat) (0 food)))
// (1 (0 cat) (0 dog))
// (0 cat)
// 20dog30cat0food
// (1 (0 cat) (0 dog))
// (0 cat)
struct Term {
char *value;
};
struct Operation {
union Expression *expr1;
union Expression *expr2;
};
union Expression {
// enum Operator op;
struct Term *term;
struct Operation *operation;
};
struct Result {
union Expression *value;
};
int indent(char *sDest, int iLevel)
{
int i;
for (i = 0; i < iLevel; ++i)
{
strcat(sDest, " ");
}
return 0;
}
int dump(union Expression *uExpr)
{
unsigned int uiOperator = uExpr->op;
// char *sOps[4] = {"TERM", "UNION", "INTERSECTION", "EXCLUSION"};
int iLevel = 0;
char *sExprname = "Expression:";
// WHY DOESN'T OPERATOR EVALUATE TO ITS VALUE
if (uiOperator != TERM)
{
indent(sExprname, iLevel++);
printf(" %s (%d)\n", sExprname, uiOperator);
// dump(uExpr->operation->expr1);
// dump(uExpr->operation->expr2);
}
return 0;
}
int make_term(union Expression *puExpr, char *pcTerm)
{
// struct Term sTerm;
// struct Operation sOperation;
// sTerm.value = pcTerm;
// sOperation
return 0;
}
int make_union(union Expression *puExpr, union Expression *puArg1, union Expression *puArg2)
{
struct Operation sOperation;
sOperation.expr1 = puArg1;
sOperation.expr2 = puArg2;
puExpr->op = UNION;
puExpr->operation = &sOperation;
return 0;
}
int main(int argc, char const *argv[])
{
static char *pcBuf = "hello";
static char *pcBuf2 = "world";
// int *buf3;
struct Term sT1, sT2;
union Expression uE1, uE2, uE3;
struct Operation sO1;
struct Result sResult;
enum Operator eTerm = TERM;
enum Operator eUnion = UNION;
enum Operator eIntersection = INTERSECTION;
enum Operator eExclusion = EXCLUSION;
// Set Term
sT1.value = pcBuf;
// Set Expression
uE1.op = eTerm;
uE1.term = &sT1;
// Set Term
sT2.value = pcBuf2;
// Set Expression
uE2.op = eTerm;
uE2.term = &sT2;
// Set Operation
sO1.expr1 = &uE1;
sO1.expr2 = &uE2;
// Set Expression
uE3.op = eUnion;
uE3.operation = &sO1;
// Set Result
sResult.value = &uE3;
printf("===============\n");
printf("TERM: %d, UNION: %d, INTERSECTION: %d, EXCLUSION: %d\n", TERM, UNION, INTERSECTION, EXCLUSION);
printf("eTerm: %d, eUnion: %d, eIntersection: %d, eExclusion: %d\n", eTerm, eUnion, eIntersection, eExclusion);
printf("uE3.op: %d\n", uE3.op);
printf("sResult.value->op: %d\n", sResult.value->op);
printf("===============\n");
puts("Result:");
dump(sResult.value);
// printf("%s\n", sResult.value->operation->expr1->term->value);
// printf("%s %s!\n", buf, buf2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment