c - Which kind of recursive parsing is this algorithm? Bottom-up or top-down? -
i have found , used algorithm solve problem had. current problem not sure if bottom-up or top-down.
i have following grammar:
query ::= andterm | andterm "andnot" query andterm ::= orterm | orterm "and" andterm orterm ::= term | term "or" orterm term ::= "(" query ")" | <word>
and have 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 current algorithm this: sample input of "blue , html".
when ran through parsequery fed through process: parsequery->parseand->parseor->parseterm.
in parseterm found in hasmap. parseterm return set "blue". parseterm iterate query list 1 step further using qnext.
in parseor we'll have new word being tested, "and", isn't strcmp therefore parseor returns term.
in parseand strcmp == 0 taken loop. qnext ran, return intersection of orterm set ("blue") , recursive parseand(index, iter).
the word html found in hashmap , set returned upto parseand. parseand return set_intersection of "blue" , "html".
i don't know call parsing, really. i've read book , book down, pdf after pdf on parsing, not sure. start on top, feed in word, design of algorithm send word bottom, parseterm , work it's way again.
sorry if long post. tried explain problem best could.
in code, there procedure each non-terminal symbol recursively calls procedures parsing other non-terminals according rules of grammar. recursive descent parser. constructs parse tree(implicitly) top bottom, makes top down parser. doesn't matter how additional information propagated.
Comments
Post a Comment