The standard algorithm to eliminate indirect left recursion takes a preventative approach, rewriting a grammar's rules so that indirect left recursion is no longer possible, rather than eliminating it only as and when it occurs. This approach results in many of the rules being lost, so that the parse trees that result are often devoid of the detail that the BNF was supposed to capture in the first place. Furthermore, the standard algorithm results in exponential blow-up as the BNF is rewritten, making it wholly unworkable in practice. To avoid these pitfalls, we revise the standard algorithm to eliminate direct left recursion and then take a graph-theoretic approach to eliminating indirect left recursion. We also extend the algorithm to rewrite the resultant parse trees in order to recover the parse trees that would have resulted if left recursion had not had to be eliminated in the first place. Therefore, aside from a couple of caveats, our algorithm works not just in theory but also in practice.
翻译:消除间接左递归的标准算法采用预防性方法,通过重写文法规则使间接左递归不再可能发生,而非仅在出现时予以消除。这种方法导致许多规则丢失,使得最终生成的语法树往往缺乏巴科斯范式最初意图捕获的细节。此外,标准算法在重写巴科斯范式时会导致指数级膨胀,使其在实践中完全不可行。为避免这些缺陷,我们改进了消除直接左递归的标准算法,并采用图论方法消除间接左递归。我们还扩展了该算法,通过重写生成的语法树来恢复若未消除左递归时本应得到的语法树。因此,除少数特殊情况外,我们的算法不仅在理论上可行,在实践中也表现良好。