5

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

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?

4

1 回答 1

3

当我试图解析SET a, 7时,我的lvalue规则没有考虑到 theSET和 lvalue之间的空格a。这是因为我将我的lvalue规则定义为'lvalue = ~"[A-Za-z]+" _'不考虑名称前的空格。我重新定义了我的分配规则,以说明 theGET和 name 之间的空格:

'setvar = "SETVAR" _ lvalue _ "," _ expr'

Parsimonious 似乎更喜欢这样。

于 2013-09-23T21:14:00.707 回答