Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parsimonious parser - error trying to parse assignment grammar

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?

like image 981
rafiki_rafi Avatar asked Oct 21 '22 00:10

rafiki_rafi


1 Answers

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.

like image 137
rafiki_rafi Avatar answered Oct 29 '22 12:10

rafiki_rafi