问题描述
我正在使用 Python Parsimonious Parser 尝试为一种简单的语言构建解释器, m设计.我观看了教程视频,它非常有用,现在我正在慢慢修改代码符合我自己的规则.我陷入了最初定义为的分配规则:
I'm using the Python Parsimonious Parser to try to build an interpreter for a simple language I'm designing. I watched this tutorial video which was very helpful, and now I'm slowly modifying the code to match my own rules. I'm stuck on an assignment rule originally defined as:
def assignment(self, node, children):
'assignment = lvalue "=" expr'
lvalue, _, expr = children
self.env[lvalue] = expr
return expr
我使用以下语法对规则进行了一些修改:
I modified the rule slightly with the following grammar:
def assignment(self, node, children):
'assignment = "SET" lvalue "," expr'
_, lvalue, _, expr = children
self.env[lvalue] = expr
return expr
例如,我希望解析器对SET a, 7
求值,与a = 7
相同,并将值7
绑定到名称a
.但是,当我尝试解析它时,我从Parsimonious库中得到了这个错误:
I'd like the parser to evaluate SET a, 7
for example, the same as a = 7
and bind the value 7
to the name a
. However, when I attempt to parse it, I get this error from the Parsimonious library:
parsimonious.exceptions.IncompleteParseError: Rule 'program' matched in its
entirety, but it didn't consume all the text. The non-matching portion of
the text begins with 'SET a, 7' (line 1, column 1).
我是解析/词法分析的新手,不确定我是否正确定义了规则.希望有人具有更多的解析/词法处理经验,可以帮助我正确地定义规则并解释我哪里出错了.也许还向我解释了简约错误?
I'm fairly new to parsing/lexing and am not entirely sure if I defined the rule correctly. Was hoping someone with more parsing/lexing experience can help me properly define the rule and explain where I went wrong. Also perhaps explain the Parsimonious error to me?
推荐答案
当我尝试解析SET a, 7
时,我的lvalue
规则未考虑SET
和左值a
之间的空格. .这是因为我将lvalue
规则定义为'lvalue = ~"[A-Za-z]+" _'
,该规则未考虑名称前的空格.我重新定义了分配规则,以说明GET
和名称之间的空格:
When I was trying to parse SET a, 7
, my lvalue
rule did not take into account the whitespace between the SET
and the lvalue a
. This is because I defined my lvalue
rule as 'lvalue = ~"[A-Za-z]+" _'
which doesn't take into account whitespace before the name. I redefined my assignment rule as follows to account for whitespace between the GET
and the name:
'setvar = "SETVAR" _ lvalue _ "," _ expr'
节俭似乎要好得多.
这篇关于简约解析器-尝试解析分配语法时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!