当我尝试运行下面的代码时,我不断收到递归错误
import nltk
from nltk import Nonterminal, nonterminals, Production, CFG
from nltk.parse import RecursiveDescentParser
from nltk.parse import ShiftReduceParser
from nltk.parse import ChartParser
from nltk.parse.generate import generate, demo_grammar
from nltk import CFG
grammar = CFG.fromstring("""
S -> NP VP
NP -> Det Nom | PropN | NP PP
Nom -> Adj Nom | N
VP -> V NP | V S | VP PP
PP -> P NP
PropN -> 'Bill' | 'Bob' | 'He'
Det -> 'the' | 'a' | 'an' | 'some'
N -> 'bear' | 'squirrel' | 'park' | 'block' | 'table' | 'river' | 'dog' | 'dogs' | 'pasta'| 'anchovies' | 'restaurant' | 'fork'
Adj -> 'angry' | 'frightened' | 'furry'
V -> 'chased' | 'saw' | 'Put' | 'eats' | 'eat' | 'chase'
P -> 'on' | 'in' | 'along' | 'with'
""")
rd = RecursiveDescentParser(grammar)
sr = ShiftReduceParser(grammar)
cp = ChartParser(grammar)
s1 = 'Bill Put the block on the table'.split()
s2 = 'Bob chased a bear in the park along the river'.split()
s3 = 'Bill saw Bob chase the angry furry dog'.split()
s4 = 'an bear eat an squirrel'.split()
s5 = 'the dogs eats'.split()
s6 = 'He eats pasta with some anchovies in the restaurant'.split()
s7 = 'He eats pasta with a fork in the restaurant'.split()
for t in rd.parse(s1):
print(t)
到目前为止,只有图表解析器可以工作,但是我也希望其他两个解析器也可以工作。我感觉到错误与我的语法变量有关,但是我无法找到解决方法。