简约解析器-尝试解析分配语法时出错 [英] parsimonious parser - error trying to parse assignment grammar

查看:139
本文介绍了简约解析器-尝试解析分配语法时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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'

节俭似乎要好得多.

这篇关于简约解析器-尝试解析分配语法时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆