Re: [Pyparsing] Recursive grammar
Brought to you by:
ptmcg
From: thomas_h <th...@go...> - 2007-11-09 13:56:24
|
Thanks for the fast answers! > and your recursion wont recurse properly. Instead, you must use the '<<' > operator. In your example, you just need to replace: Ah, silly me. I had the '<<' operator in my original code, but forgot it when I tried to stip it down to a minimal example ... Yes, now it works :-). The reason I tried to build a minimal example in the first place was that I had double recursion in my initial version, something like aexp << Or([..., aexp + '+' + aexp, ...]) which results in infinite recursion. I felt this might be the right way to do it, but maybe I'm wrong. Is this generally possible in pyparsing? > While this all works, I'm curious why you are not using the operators > defined for creating compound constructs such as Or. The form you have > feels very tedious to me, compared to: > > aexp << ( number ^ number + '+' + aexp ) I tried the operators superficially and got error messages from Python, but that was probably my fault (e.g. not importing enough). I will improve on that :-). > And I would also suggest that you try to use the MatchFirst construct > instead of Or, especially with recursive expressions: > > aexp << ( number + '+' + aexp | number ) I presume this will gain me some run-time efficiency. > Note that I had to reorder the terms in order to try the more restrictive > test first. But MatchFirst will stop at the first matching alternative, > while Or will evaluate all alternatives and select the longest. In > recursive expressions, Or can descend down a neverending sequence of > self-referencing alternatives. I will keep this in mind, although I might stick with Or for the beginning and use MatchFirst as an optimization later on (my target is parsing JavaScript). > > Welcome to pyparsing! Thanks :-). Apart from my beginner's problems I've got the impression that it is really good stuff. Thanks for making the effort! =Thomas |