Re: [Pyparsing] Using both Forward and operatorPrecedence()
Brought to you by:
ptmcg
From: spir ☣ <den...@gm...> - 2010-03-25 10:21:45
|
On Wed, 24 Mar 2010 15:57:34 -0600 "ThanhVu (Vu) Nguyen" <ngu...@gm...> wrote: > Hi, I tried to generate this simple recursive rule that involves both > Forward and operatorPrecedence() and get errors about maximum > recursion depth exceeded . > > Thanks, > > > > def getRule_test(): > #rule exp = name | num | name[exp] | exp + exp | exp * exp | > name = Word(alphas) > num = Word(nums) > > exp = Forward() > idx=name + '[' + exp + ']' > > arith = operatorPrecedence( > exp,[('*',2,opAssoc.LEFT), > ('+',2,opAssoc.RIGHT)],) > > exp << (arith|idx|name|num) #works ok if take out arith > return exp > > VN - Yes, the recursive term "exp" in your format appears on the left side of arith, and thus finally on the left side of itself. This cannot cannot be matched since it lauches an infinite recursive loop of call to exp.match(). More generally, you cannot write a pattern such as: p1 : p1 whatever But it can always be reformulated into something like: p2 : whatever p2 Here, you need to distinguish between the levels of a non-recursive operand (inside arith) and of a whole exp. Operator precedence already involves the inherent recursivity of operations. Exp only needs be recursive because it appears inside idx. (I guess.) Something like, maybe (untested!): #rule exp = name | num | name[exp] | exp + exp | exp * exp | name = Word(alphas) num = Word(nums) exp = Forward() idx=name + '[' + exp + ']' operand = (idx|name|num) arith = operatorPrecedence( operand,[('*',2,opAssoc.LEFT), ('+',2,opAssoc.RIGHT)],) exp << (arith|operand) #works ok if take out arith return exp Denis ________________________________ vit esse estrany ☣ spir.wikidot.com |