Re: [Pyparsing] How can I print syntax tree for debug?
Brought to you by:
ptmcg
From: Paul M. <pa...@al...> - 2007-06-24 05:16:53
|
Have you looked at the dump() method on ParseResults? dump() lists out a nested list of the ParseResults (the same as you get from the asList() method), then lists a hierarchical tree of the named results. Here is how your program's parsed tokens look using dump(): import pyparsing as pp Name = pp.Word(pp.alphas).setResultsName('Name') S = pp.delimitedList(Name, delim=pp.White()) results = S.parseString("a b c") print results.dump() Prints: ['a', 'b', 'c'] - Name: c You're probably wondering why 'Name' only gives 'c' as the value. In the default use of setResultsName, results names work like a Python dict. While parsing progresses, 'Name' gets successively set to 'a', then 'b', and then finally 'c'. If you want to see all the 'Name' results, then use a different form of setResultsName, passing listAllResults as True: Name = pp.Word(pp.alphas).setResultsName('Name',listAllMatches=True) S = pp.delimitedList(Name, delim=pp.White()) results = S.parseString("a b c") print results.dump() Prints: ['a', 'b', 'c'] - Name: ['a', 'b', 'c'] A couple of other comments: - Pyparsing skips over whitespace by default, so there is no need to define a sequence of Name words as a delimitedList(Name,delim=White()). You can just OneOrMore(Name) to get this same result. - setResultsName can be used in the manner you have done, and you should use listAllMatches=True so that you don't just get the latest Name. When I first added setResultsName, my intention was that a single expression might be used several times within a larger grammar, with different results names depending on where in the grammar the expression occurred. For instance, here is an expression for a two-digit number: num2d = pp.Word(pp.nums,exact=2) I could label these with: num2d.setResultsName("twoDigitNumber") But if I were parsing a timestamp, this would be much more meaningful: timestamp = num2d.setResultsName("year") + "/" + \ num2d.setResultsName("month") + "/" + \ num2d.setResultsName("day") + \ num2d.setResultsName("hours") + ":" + \ num2d.setResultsName("minutes") + ":" + \ num2d.setResultsName("seconds") Parsing the string "07/06/23 12:34:56" and calling dump() gives this output: ['07', '/', '06', '/', '23', '12', ':', '34', ':', '56'] - day: 23 - hours: 12 - minutes: 34 - month: 06 - seconds: 56 - year: 07 I am glad you're using results names, I think they are an important feature for all but the most trivial parsers. In the next release of pyparsing, this syntax will be simplfied to: timestamp = num2d("year") + "/" + \ num2d("month") + "/" + \ num2d("day") + \ num2d("hours") + ":" + \ num2d("minutes") + ":" + \ num2d("seconds") Probably more than you were bargaining for, but I hope this helps. -- Paul > -----Original Message----- > From: pyp...@li... > [mailto:pyp...@li...] On > Behalf Of sam lee > Sent: Saturday, June 23, 2007 11:24 PM > To: pyp...@li... > Subject: [Pyparsing] How can I print syntax tree for debug? > > Hi. > > I want to print a syntax tree or something similar for > debugging purpose. > > import pyparsing as pp > Name = pp.Word(pp.alphas).setResultsName('Name') > S = pp.delimitedList(Name, delim=pp.White()) > print S.parseString("a b c") > ['a', 'b', 'c'] > > I want to print something like: > ['Name:a', 'Name:b', 'Name:c'] #prefixing each token with > results name of Name. > > What I am currently doing is: > Name.setParseAction(lambda s,loc,toks: ['Name:'+x for x in toks]) > > But, I have to write 'Name:' part manually. Probably what I want is: > def display( self, s, loc, toks ): > return [self.getName() + x for x in toks] > pp.ParseResults.debugDisplay = display > Name.setParseAction(pp.ParseResults.debugDisplay) > > Above doesn't work. > > I think I am going in the wrong direction. Is there a nice way of > printing debugging information? Printing side-way syntax tree would be > superb!! > > Something like: > Expr: > Name: 'apple' > Op: '+' > Literal: > Number: 233 > > > Thank you. > > -------------------------------------------------------------- > ----------- > This SF.net email is sponsored by DB2 Express > Download DB2 Express C - the FREE version of DB2 express and take > control of your XML. No limits. Just data. Click to get it now. > http://sourceforge.net/powerbar/db2/ > _______________________________________________ > Pyparsing-users mailing list > Pyp...@li... > https://lists.sourceforge.net/lists/listinfo/pyparsing-users > > |