删除左递归语法

时间:2015-07-12 16:49:19

标签: recursion compiler-construction grammar

如何删除以下语法中的左递归?

S -> “url” “(“ STRING “)” // basic services
| S “?” S // sequential execution
| S “|” S // concurrent execution
| “timeout” “(“ REAL “,” S “)” // timeout combinator
| “repeat” “(“ S “)” // repetition
| “stall” // nontermination
| “fail” // failure

1 个答案:

答案 0 :(得分:0)

诀窍是创建一个正确递归的父作品。

T -> S
T -> S "?" T // sequential execution
T -> S "|" T // concurrent execution

S -> “url” “(“ STRING “)” // basic services
| “timeout” “(“ REAL “,” S “)” // timeout combinator
| “repeat” “(“ S “)” // repetition
| “stall” // nontermination
| “fail” // failure

这将识别同一组输入,但要注意T现在是右关联的,而不是左关联的。