How can I solve this left recursion? I have used long time to find a solution but I struggle with it. sentence: Bob gives Max cars. The main point I wanted to show here is the left recursion which occurs. Which is NP -> NP, how can I solve this problem? If I run this code right now in python3 it will crash.
import nltk.grammar
grammar = nltk.CFG.fromstring("""
S -> NP VP
VP -> V NP
NP -> "cars" | "Bob" | NP | "Max"
V -> "gives"
""")
sent = "Bob gives Max cars".split()
rd_parser = nltk.RecursiveDescentParser(grammar)
for tree in rd_parser.parse(sent):
print(tree)