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?
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'
Parsimonious seems to like that a lot better.