Re: [Pyparsing] A PyParsing hack
Brought to you by:
ptmcg
From: Paul M. <pa...@al...> - 2008-03-07 20:27:20
|
Greg - Bwa-ha-ha! Another minion in my evil domain!!! :) I'm very happy that you find pyparsing so useful AND enjoyable. Your concept of passing a class instead of a function to setParseAction looks pretty good. Your point is dead-on - why bother returning just a nested list of strings when you can use parse actions to construct and return executable objects? It is an excellent combination of both lexing and parsing. And extra credit for using parse actions as part of an operatorPrecedence expression. Please don't be disappointed, but yes, pyparsing *can* do this already. In fact, the code you posted/pasted, if you just change PP.NoStringBase to object as the base class for your various parse action/classes, will run otherwise unchanged with the unpatched pyparsing running on my system. I get this result: [<Assignment <Token 'X'> = <Math [<Token 'Y'>, '+', <Math [5, '*', <Token 'Z'>] >] > >] For another example of this technique, see: http://pyparsing.wikispaces.com/space/showimage/simpleBool.py. In addition to being online at the pyparsing wiki, this script is included in the examples directory of the source and doc distibutions. In "Getting Started With Pyparsing" (http://www.oreilly.com/catalog/9780596514235), the final chapter shows how to convert query string expressions into objects that generate Python set expressions, which can then be intersect'ed and union'ed to correspond to the and's and or's of the original query string. And I have a new magazine article coming out soon that uses this method to compile an esoteric language to executable objects. Congratulations on arriving at this powerful concept on your own, it is most definitely an advanced technique! And keep proposing patches! There are now many more brains than just my own thinking about this module and how it could be improved, and I would be foolish to limit new work to only the stuff I come up with myself. Many of the recent enhancements came as a result of discussions and patches submitted over discussion lists and forums. I'm glad that you are not afraid to get your fingernails dirty, and get right into the pyparsing code to see what it is doing. -- Paul (I'm sorry about unitTests - some of the files that I parse in my testing are proprietary to some of my business customers, so I can't post them. I'll try to update the SVN with what is publicly shareable, and conditionalize the unitTests.py script to step over the private tests.) |