parsimonious parser - error trying to parse assignment grammar

混江龙づ霸主 提交于 2019-12-07 04:54:53

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/18966779/parsimonious-parser-error-trying-to-parse-assignment-grammar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!