I found and used an algorithm to solve the problem I had. The current problem is that I'm not sure if this is from bottom to top or top to bottom.
I have the following grammar:
query ::= andterm | andterm "ANDNOT" query andterm ::= orterm | orterm "AND" andterm orterm ::= term | term "OR" orterm term ::= "(" query ")" | <word>
And therefore, I have the following code:
struct index { hashmap *map; char *qword; } void querynext(iter, index) { if (list_hasnext(iter)) { index->qword = list_next(iter); } else index->qword = ""; } set_t *parsequery(index, iter) { set_t *andterm; andterm = parseand(index, iter); if(strcmp(index->qword, "ANDNOT") == 0) { qnext(iter, index); return set_different(andterm, parsequery(index, iter)): } else return andterm; } set_t *parseand(index, iter) { set_t *orterm; orterm = parseor(index, iter); if(strcmp(index->qword, "AND") == 0) { qnext(iter, index); return set_intersection(orterm, parseand(index, iter)); } else return orterm; } set_t *parseor(index, iter) { set_t *term; term = parseterm(index, iter); if(strcmp(index->qword, "OR") == 0) { qnext(iter, index); return set_difference(term, parseor(index, iter)); } else return term; } set_t *parseterm(index, iter) { if(strcmp(index->qword, "(") == 0) { qnext(iter, index); set_t *parseset = parsequery(index, iter); if(strcmp(index->qword, ")") == 0) { qnext(iter, index); return perseset; } } if(map_haskey(index->map, index->qword)) { set_t *parsekey; parsekey = map_get(index->map, index->qword); qnext(iter, index); return parsekey; } else { set_t emptyset; emptyset = set_create; return empty; } }
The flow of the current algorithm will be as follows: Example input "blue AND html".
When it passes through the parsequery, it will be passed through this process: parsequery-> parseand-> parseor-> parseterm.
In parseterm it will be discovered that it is in the hasmap. Parseterm will return the set with "blue". Parseterm will also go through the query list one step further using qnext.
The parser will now have a new word that is being tested, "AND", this is not strcmp, so the parser returns the term.
Parseand will have strcmp == 0, so it will be looped. qnext will be launched, and then the intersection of orterm set ("blue") and the recursive parseand (index, iter) will return.
The word html will also be found in hashmap, and the set will be returned before parseand. Parseand will then return set_intersection "blue" and "html".
I do not know what to call this parsing. I read the book and booked, pdf after publishing in pdf, but I'm not sure. We start from the top, feed the word, but the design of the algorithm will send the word below, to the parsmerm, and then it will start again.
Sorry if this is a long post. I tried to explain my problem as best as possible.