Difference between Left Factoring and Left Recursion

后端 未结 7 1157
长情又很酷
长情又很酷 2020-12-13 06:25

What is the difference between Left Factoring and Left Recursion ? I understand that Left factoring is a predictive top down parsing t

7条回答
  •  一整个雨季
    2020-12-13 07:06

    Left Recursion: A grammar is left recursive if it has a nonterminal A such that there is a derivation A -> Aα | β where α and β are sequences of terminals and nonterminals that do not start with A.

    While designing a top down-parser, if the left recursion exist in the grammar then the parser falls in an infinite loop, here because A is trying to match A itself, which is not possible. We can eliminate the above left recursion by rewriting the offending production. As-

    A -> βA'

    A' -> αA' | epsilon

    Left Factoring: Left factoring is required to eliminate non-determinism of a grammar. Suppose a grammar, S -> abS | aSb

    Here, S is deriving the same terminal a in the production rule(two alternative choices for S), which follows non-determinism. We can rewrite the production to defer the decision of S as-

    S -> aS'

    S' -> bS | Sb

    Thus, S' can be replaced for bS or Sb

提交回复
热议问题