[Pyparsing] Unordered set of expressions
Brought to you by:
ptmcg
From: Russell D. <Rus...@as...> - 2011-09-29 18:25:23
|
I have to parse something with an unordered set of expressions. In the simplest case, some expressions are required, some are optional. For this, parsing is easy: language = expr1 & expr2 & Optional(expr3) However, there are cases where an expression can occur zero or more times, one or more times, or two or more times. But since they can still occur in any order, I'm not sure how to handle it. If I try: language = expr1 & expr2 & Optional(expr3) & ZeroOrMore(expr4) & ZeroOrMore(expr5) Then parsing fails if all expr4's are not adjacent. My current idea is to do the following: multiples = ZeroOrMore(expr4 | expr5) language = (multiples + expr1) & (multiples + expr2) & Optional(multiples + expr3) & multiples While its ugly, I can't think of any other way to do it. Additionally, it doesn't get me any closer to implementing OneOrMore in an unsorted set of expressions. |