Re: [Pyparsing] left-recursion again: expression of attribute
Brought to you by:
ptmcg
From: Eike W. <eik...@gm...> - 2010-03-30 20:43:20
|
Hello Denis! On Tuesday March 30 2010 21:31:58 spir ☣ wrote: > Hello, > > This time, that's me having a left-recursion issue. I'm trying to parse var > names that can possibly refer to attributes, like "a.b.c". I can parse it > as simpleName + ZeroOrMore(extension) > but then I need to reformat the result resursively, to get the real > semantics of: getattr(getattr(container, name), name) > i.e. in the case of "a.b.c": > ((a).b).c It is impossible to do what you want in an elegant way, you must reformat the parse result. I had a similar question some time ago, this is the simplified summary of Paul's answer. For code that gives you the right operator precedence look at Pyparsing's operatorPrecedence(...) or at the calculator example. It also gives you a parse result that can be relatively easily reformatted. By the way, if you really implement a programming language with Pyparsing use operatorPrecedence(...) it can parse nearly all of Python's expressions including function calls. The only exception are the intertwined unary minus and exponentiation operators. You'll need hand crafted code for them. operatorPrecedence(...) has significantly reduced the size of my parser (I have 12 levels of precedence). If you are interested I can talk you though my still very big parser implementation, which can be found here (look at line 979): http://tinyurl.com/yc3gyqc Original URL, but Kmail messes this URL up: http://bazaar.launchpad.net/~vcs- imports/freeode/trunk/annotate/head:/freeode_py/freeode/simlparser.py Eike. |