Re: [Pyparsing] parse algebra like expressions
Brought to you by:
ptmcg
From: Paul M. <pt...@au...> - 2011-08-04 14:08:59
|
Rita - There are really two parts to your question - how to parse the expression you've given, and then how to construct a useful data structure from it. >From your example, I haven't gone too far into creating a true parent-child graph, I'm just implementing the problem as you have stated it. Here is a little annotated script to try: from pyparsing import Suppress, Word, alphas, alphanums, Literal, Group, delimitedList # define basic punctuation, operators, and our parentage item LPAR,RPAR = map(Suppress, '()') parentOp = Literal("->") item = Word(alphas, alphanums) # define a group of possible items, as in '(b|c)' altItems = Group(LPAR + delimitedList(item,'|') + RPAR) # define a hierarchy expression as a list of items delimited by parentOps hierarchyExpr = delimitedList(item | altItems, parentOp) # parse the test string testStr = "a -> (b|c) -> d" gens = hierarchyExpr.parseString(testStr) # use zip to convert the list of hierarchy entries into parent-child pairs for parent,child in zip(gens,gens[1:]): print 'parent', ' '.join(parent), 'child', ' '.join(child) See if that gets you further along. -- Paul -----Original Message----- From: Rita [mailto:rmo...@gm...] Sent: Wednesday, August 03, 2011 6:18 PM To: pyp...@li... Subject: [Pyparsing] parse algebra like expressions Hello, I would like to parse a string into something like this str = "a -> (b|c) -> d" This should generate parent a child b c parent b c child d Any ideas how I can do this with pyparse? -- --- Get your facts first, then you can distort them as you please.-- ---------------------------------------------------------------------------- -- BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA The must-attend event for mobile developers. Connect with experts. Get tools for creating Super Apps. See the latest technologies. Sessions, hands-on labs, demos & much more. Register early & save! http://p.sf.net/sfu/rim-blackberry-1 _______________________________________________ Pyparsing-users mailing list Pyp...@li... https://lists.sourceforge.net/lists/listinfo/pyparsing-users |