parsimonious

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,

NodeVisitor class for PEG parser in Python

佐手、 提交于 2019-11-28 14:02:00
Imagine the following types of strings: if ((a1 and b) or (a2 and c)) or (c and d) or (e and f) Now, I'd like to get the expressions in parentheses, so I wrote a PEG parser with the following grammar: from parsimonious.grammar import Grammar grammar = Grammar( r""" program = if expr+ expr = term (operator term)* term = (factor operator factor) / factor factor = (lpar word operator word rpar) / (lpar expr rpar) if = "if" ws and = "and" or = "or" operator = ws? (and / or) ws? word = ~"\w+" lpar = "(" rpar = ")" ws = ~"\s*" """) which parses just fine with tree = grammar.parse(string) Now the

NodeVisitor class for PEG parser in Python

拥有回忆 提交于 2019-11-27 19:32:26
问题 Imagine the following types of strings: if ((a1 and b) or (a2 and c)) or (c and d) or (e and f) Now, I'd like to get the expressions in parentheses, so I wrote a PEG parser with the following grammar: from parsimonious.grammar import Grammar grammar = Grammar( r""" program = if expr+ expr = term (operator term)* term = (factor operator factor) / factor factor = (lpar word operator word rpar) / (lpar expr rpar) if = "if" ws and = "and" or = "or" operator = ws? (and / or) ws? word = ~"\w+" lpar