Re: [Pyparsing] A newbie w/nested structures
Brought to you by:
ptmcg
From: Paul M. <pa...@al...> - 2007-09-09 18:29:30
|
Tim - Sorry, I must have mis-pasted the sample, here it is again. It parses okay on my system (and I tested versions back to 1.4.2). from pyparsing import * LT,GT,EQ,LPAR,RPAR,LBRK,RBRK,BAR,QUOT,SEMI = map(Suppress,"<>=()[]|';") upper = srange("[A-Z]") lower = upper.lower() attrName = Word(lower,alphanums+"_") key = attrName | (LBRK+quotedString+RBRK) quotedString.setParseAction(removeQuotes) valueDef = Forward() valueDef << ( key + EQ + LT + ZeroOrMore( Group(valueDef | quotedString )) + GT ) ontologySection = "ontology" + valueDef comment = "--" + restOfLine ontologySection.ignore(comment) sample = """ ontology term_definitions = < ["en"] = < items = < ["at0000"] = < description = <"Generic reporting composition in response to a request for information or testing"> text = <"Report"> > ["at0001"] = < description = <"@ internal @"> text = <"Tree"> > ["at0002"] = < description = <"Information about the request"> text = <"Request details"> > ["at0003"] = < description = <"Identification of the request"> text = <"Request identifier"> > > > > """ res = ontologySection.parseString(sample) from pprint import pprint pprint( res.asList() ) This prints: ['ontology', 'term_definitions', ['en', ['items', ['at0000', ['description', ['Generic reporting composition in response to a request for information or testing']], ['text', ['Report']]], ['at0001', ['description', ['@ internal @']], ['text', ['Tree']]], ['at0002', ['description', ['Information about the request']], ['text', ['Request details']]], ['at0003', ['description', ['Identification of the request']], ['text', ['Request identifier']]]]]] Your description of key and valueDef are completely correct. If you want another example of Forward, I just posted this on comp.lang.python - http://groups.google.com/group/comp.lang.python/browse_frm/thread/4f128e9df5 4d6962/# - it is a basic nested example with words enclosed in nested braces. -- Paul -----Original Message----- From: pyp...@li... [mailto:pyp...@li...] On Behalf Of Tim Cook Sent: Sunday, September 09, 2007 5:41 AM To: PyParsing List Subject: Re: [Pyparsing] A newbie w/nested structures Hi Paul, On Sun, 2007-09-09 at 04:30 -0500, Paul McGuire wrote: > Here are some suggestions on getting started: > - pick a part of the sample ADL (I would suggest working section by > section) > - develop a simple BNF for this grammar > > Here is a sample parser for the ontology section. This is exactly what I started with. Well, it's what I dropped back to when I realized how difficult it would be. :-) > It is a recursive > example, defining a valueDef that is defined in terms of component > valueDefs. It also shows the comment format, and the mechanism for > skipping comments. I hope this sample gives you a jump start on a > more complete ADL parser. Thank you so very much for your prompt and informative reply. > valueDef = Forward() > valueDef << ( key + EQ + LT + ZeroOrMore( Group(valueDef | > quotedString )) + GT ) I am certain that my problem is that I still do not have a good grasp of Forward(). Is this the meaning of the valueDef assignment? "valueDefs are composed of a key followed by an = followed by < and then zero or more embedded valueDefs or quoted strings. The key is composed of an attribute name or a bracketed and quoted string" BTW: The example you sent raises ParseException; pyparsing.ParseException: Expected ">" (at char 55), (line:4, col:17) Thanks. Tim -- Timothy Cook, MSc Health Informatics Research & Development Services http://timothywayne.cook.googlepages.com/home 01-904-322-8582 |