Re: [Pyparsing] Dealing with blocks in different order
Brought to you by:
ptmcg
From: Paul M. <pt...@au...> - 2015-04-21 07:14:35
|
I agree with John's suggestion - define an overall grammar that is simply OneOrMore(video_block | audio_block), and then use that to parse the mixed listing of blocks. The other way in pyparsing to treat a grammar like "A, B, C and D, in any order" is to use the Each construct in pyparsing, created using the '&' operator. So "A & B & C & D" will match the 4 elements in any order, but all 4 must be present. If some are optional, then indicate them so using Optional, as in "A & B & Optional(C) & D". Finally, if some items might appear more than once, use ZeroOrMore or OneOrMore, as in "OneOrMore(A) & B & ZeroOrMore(C) & D". This last expression will match the repeated elements even if they are not all together, so AABADBACC would match, as would DABAAA. ABC would *not* match, as both B and D elements are required. But for your particular case, I think just OneOrMore(A | B) should be sufficient for any combination of A's and B's. I'm glad to see some of the other pyparsing folks stepping up to answer some questions, here and on StackOverflow. And thanks, John, for your kind comments on the help you get on the wikispaces site. As it turns out, between work and family activities for the next month or so, my participation on these lists will be limited, so I appreciate other experienced pyparsing users helping the new folks. Cheers, -- Paul -----Original Message----- From: john grant [mailto:joh...@ya...] Sent: Tuesday, April 21, 2015 12:41 AM To: mlist @dslextreme.com; pyp...@li... Subject: Re: [Pyparsing] Dealing with blocks in different order I've been stuck on the same thing. I think I know the answer, but I have not had time to verify it. If you find one of the examples for parsing a C structure, I think it must contain the secret because the parser works no matter the order of the structure members. If my memory is correct, that example has parsers defined for each type of member (e.g. single variable, array, pointer, etc), and then there is a single parser that ORs each of the other parsers together, and the matches get inserted into a container (i.e. OneOrMore). Let me know if you find a fix! FYI: I've had great support on the wikispaces site. Paul has answered many of my questions. However, finding that support channel was insanely difficult. You have to go to the home page, then click Getting Help in the left side menu, then in the body of the page that appears, click the text that is underlined/hyperlinked saying "pyparsing home page" (which is poorly named). -John --- This email has been checked for viruses by Avast antivirus software. http://www.avast.com |