我一生都无法弄清楚为什么Alternative 是递归的。它确实给我的解析器带来了麻烦。
Alternative :: [empty] Alternative Term
这是规范语义部分中的一个注释,但并不完全清楚。也许一旦我明白了这一点,推理就会揭晓?
NOTE Consecutive Terms try to simultaneously match consecutive portions of the input String. If the left Alternative, the right Term, and the sequel of the regular expression all have choice points, all choices in the sequel are tried before moving on to the next choice in the right Term, and all choices in the right Term are tried before moving on to the next choice in the left Alternative.
什么样的解析器可以正确处理左递归语法?
最佳答案
因为对于某些类型的解析器,左递归要好得多(例如,对于 yacc - 请参阅第 6.2 节 here 以获得解释)。
如果它给您的特定解析器带来麻烦,那么请务必将其交换 - 它不会以任何方式影响语言的定义。
关于javascript - 为什么 ECMAScript RegExp 语法的替代符号是左递归的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3118802/