Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Shift-Reduce解析器的上下文无关语法_Python_Parsing_Context Free Grammar_Shift Reduce - Fatal编程技术网

Python 使用Shift-Reduce解析器的上下文无关语法

Python 使用Shift-Reduce解析器的上下文无关语法,python,parsing,context-free-grammar,shift-reduce,Python,Parsing,Context Free Grammar,Shift Reduce,我正在尝试对以下句子使用Shift-Reduce解析器: 他在餐馆里吃意大利面和一些凤尾鱼 我已经创建了一些代码和语法,但语法仅适用于: 他吃意大利面和一些凤尾鱼 import nltk from nltk.parse import ShiftReduceParser grammar = nltk.CFG.fromstring(""" S -> NP VP NP -> PropN | N PP | Det N VP -> V NP | V S | VP PP | NP P

我正在尝试对以下句子使用Shift-Reduce解析器:

他在餐馆里吃意大利面和一些凤尾鱼

我已经创建了一些代码和语法,但语法仅适用于:

他吃意大利面和一些凤尾鱼

import nltk
from nltk.parse import ShiftReduceParser


grammar = nltk.CFG.fromstring("""

S -> NP VP
NP -> PropN | N PP | Det N
VP -> V NP | V S | VP PP | NP PP
PP -> P NP 
PropN -> 'He'
Det -> 'some' | 'the'
N -> 'pasta' | 'anchovies' | 'restaurant'
V -> 'eats'
P -> 'with' | 'in'

""")


sr = ShiftReduceParser(grammar)

sentence1 = 'He eats pasta with some anchovies'
# He eats pasta with some anchovies in the restaurant

tokens = nltk.word_tokenize(sentence1)



print("--------------------------- Sentence 1 ---------------------------")

for x in sr.parse(tokens):
    print(x)
现在我尝试将
Det NP
添加到
NP->

但是显然,
Det-NP
是不正确的语法。哪一方面是我的错误?我将如何让移位解析器完全解析我的句子