简体   繁体   English

这种算法是哪种递归解析?自下而上还是自上而下?

[英]Which kind of recursive parsing is this algorithm? Bottom-up or top-down?

I have found and used an algorithm to solve a problem I had. 我找到并使用了一种算法来解决我遇到的问题。 The current problem is that I am not sure if this is bottom-up or top-down. 目前的问题是我不确定这是自下而上还是自上而下。

I have the following grammar: 我有以下语法:

query   ::= andterm 
        | andterm "ANDNOT" query
andterm ::= orterm
        | orterm "AND" andterm
orterm  ::= term
        | term "OR" orterm
term    ::= "(" query ")" 
        | <word>

And thus do 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 like this: Sample input of "blue AND html". 当前算法的流程如下:“blue AND html”的示例输入。

When it is ran through parsequery it will be fed through this process: parsequery->parseand->parseor->parseterm. 当它通过parsequery运行时,它将通过这个过程提供:parsequery-> parseand-> parseor-> parseterm。

In parseterm it will be found to be in the hasmap. 在parseterm中,它将被发现在hasmap中。 Parseterm will return a set with "blue". Parseterm将返回一个“蓝色”的集合。 Parseterm will also iterate the query list one step further using qnext. Parseterm还将使用qnext进一步迭代查询列表。

In parseor we'll now have a new word that is being tested, "AND", it isn't strcmp so therefore parseor returns term. 在parseor中,我们现在将有一个正在测试的新单词“AND”,它不是strcmp所以因此parseor返回term。

In parseand it will be strcmp == 0 so it will be taken into the loop. 在parseand中它将是strcmp == 0所以它将被带入循环。 qnext will be ran, then return the intersection of orterm set ("blue") and the recursive parseand(index, iter). 将运行qnext,然后返回orterm set(“blue”)和递归parseand(index,iter)的交集。

The word html will also be found in the hashmap and the set will be returned upto parseand. html这个词也可以在hashmap中找到,该set将返回到parseand。 Parseand will then return the set_intersection of "blue" and "html". 然后Parseand将返回“blue”和“html”的set_intersection。

I don't know what to call this parsing, really. 我真的不知道该怎么称呼这个解析。 I've read book up and book down, pdf after pdf on parsing, but I am not sure. 我已阅读预订并预订,pdf解析后的pdf,但我不确定。 We start on the top, feed in the word, but the design of the algorithm will send the word to the bottom, to parseterm and then work it's way up again. 我们从顶部开始,输入单词,但算法的设计会将单词发送到底部,然后再解析为parseterm,然后重新开始工作。

Sorry if this was a long post. 对不起,如果这是一篇很长的帖子。 I tried to explain my problem as best as I could. 我尽力解释我的问题。

In your code, there is a procedure for each non-terminal symbol which recursively calls procedures for parsing other non-terminals according to the rules of the grammar. 在您的代码中,每个非终端符号都有一个过程,它根据语法规则递归调用解析其他非终端的过程。 So it is a recursive descent parser . 所以它是一个递归下降解析器 It constructs the parse tree(implicitly) from top to bottom, which makes it a top down parser. 它从上到下构造解析树(隐式),这使它成为自上而下的解析器。 It doesn't really matter how the additional information is propagated. 附加信息的传播方式并不重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM