0

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)
  • You asked the same question earlier today. It's better with the code but why did you delete it and repost it instead of editing your existing question? This is seriously against the rules of the site. Don't do it in the future. – alexis Apr 30 '17 at 17:12
  • I think you need to reread whatever textbook you are using for the meaning of `|`. – rici Apr 30 '17 at 17:26
  • As you say, "Bob give Max car" is not a grammatical English sentence. However, "Bob gives Max cars" is okay. Using a grammatical sentence would make the point of your question a bit clearer. – Michael Dyck May 01 '17 at 00:26

2 Answers2

0

Solve the problem by not writing a recursive rule. If you can't even come up with a sentence where you need it, you don't need it.

Recursive rules are legal in CFGs; that's how they can generate an infinite number of sentences. And with a left-recursive grammar, a recursive descent parser will run forever with some inputs. That's what it does, and that's why there are smarter parsing algorithms.

alexis
  • 48,685
  • 16
  • 101
  • 161
0

It's true that NP -> NP is a left-recursive rule. However, it's also a pointless rule, because it doesn't change the language generated by the grammar, it just makes the grammar ambiguous.

So if you were thinking that adding NP -> NP to the grammar would allow you to parse "Bob give Max car", it does not. And this has nothing to do with the parser, but just the fact that that sentence is not in the language generated by the grammar.

Try to draw the parse tree that you were hoping to get for that sentence. You'll find that there isn't one that conforms to that grammar. But if you find one that doesn't conform to the grammar, that should tell you how to change the grammar to handle such a sentence. (Note that there may be various such trees, reflecting various different grammars that can generate that sentence. Your job then will be to pick one that your parser can deal with.)

Michael Dyck
  • 2,153
  • 1
  • 14
  • 18
  • @aom97: In that diagram, the rightmost NP seems to be growing out of thin air. In fact, its parent should be the VP; it is the object of the verb. (Max is the indirect object.) But it is not clear whether you are asking about English grammar or how to use NLTK. If you are asking about English grammar, this is probably not the correct forum. – rici May 01 '17 at 15:08
  • In that parse tree, it looks like the rightmost NP (above 'car') is a child of the next-rightmost NP (above 'Max'). It's important to understand that any given node in a parse tree can use at most one production from the grammar. So the second-from-the-right NP can have a line down to 'Max' (using the production `NP -> Max`) OR it can have a line down to another 'NP' (using the production `NP -> NP`), but it can't have *both* (unless you change the grammar). – Michael Dyck May 01 '17 at 17:38