Re: [Pyparsing] Using both Forward and operatorPrecedence()
Brought to you by:
ptmcg
From: ThanhVu (V. N. <ngu...@gm...> - 2010-03-25 22:07:46
|
thanks for the explanation -- I think your code works pretty well VN - 2010/3/25 spir ☣ <den...@gm...>: > 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 > > ------------------------------------------------------------------------------ > Download Intel® Parallel Studio Eval > Try the new software tools for yourself. Speed compiling, find bugs > proactively, and fine-tune applications for parallel performance. > See why Intel Parallel Studio got high marks during beta. > http://p.sf.net/sfu/intel-sw-dev > _______________________________________________ > Pyparsing-users mailing list > Pyp...@li... > https://lists.sourceforge.net/lists/listinfo/pyparsing-users > |