Re: [Pyparsing] DSL using pyparsing
Brought to you by:
ptmcg
From: Asim M. <as...@ho...> - 2009-06-12 00:49:05
|
Thanks Paul, I think i have the idea now. I am using Imputil, so the problem i have now is that i am using imputil, so when say i am trying to compile a script, say: A = Color(100, 10, 50) I get a "NameError: name 'Color' is not defined". Any ideas? > From: pt...@au... > To: as...@ho...; pyp...@li... > Subject: RE: [Pyparsing] DSL using pyparsing > Date: Wed, 10 Jun 2009 14:17:50 -0500 > > Asim - > > I would work on this like they dig a tunnel - from both directions. First I > would write out what my DSL syntax would be. Then I would work backwards > and write out how this would be implemented in Python. The purpose of the > pyparsing DSL converter is to find your DSL syntax, and then create the > related Python code. > > Let's say your application was something about RGB colors, and A and B are > going to be colors which, when added together, result in a third color whose > RGB values are the respective sums of the R, G, and B values of A and B. > Let's say this is your color syntax: > > A = < 100 10 50 > > B = < 0 0 0 > > C = A+B > > The purpose of the DSL converter is to convert the "<>" syntax into a > Python-compatible API call (probably a constructor call to a Color class). > The "C=A+B" line probably doesn't need DSL handling, your Python Color class > can define the __add__() function to do the proper R, G, and B summing. > > So all your DSL converter in this case would need to do would be to change: > > A = < 100 10 50 > > > To: > > A = Color(100, 10, 50) > > In pyparsing this is a very simple thing: > > LT,GT = map(Suppress,"<>") > Integer = Word(nums) > colorDef = LT + integer*3 + RT > colorDef.setParseAction(lambda tokens : "Color(%s, %s, %s)" % tokens) > > Then use colorDef.transformString() to convert the source read from the > imported file into valid Python, and then use that to compile into the > executable module. > > Remember, you are generating a Python script from your DSL script, so you > don't have to worry about "carrying values forward from one line to the > next" - when your generated code gets compiled to Python bytecode, it will > do this for you. > > Don't try to re-invent Python with your DSL. You are *much* better off > using your DSL to *augment* the Python syntax. > > Thanks for reading the article! > > -- Paul > > > > -----Original Message----- > > From: Asim Malik [mailto:as...@ho...] > > Sent: Wednesday, June 10, 2009 1:58 PM > > To: pyp...@li... > > Subject: [Pyparsing] DSL using pyparsing > > > > > > Ok i am looking at building a DSL using pyparsing and am relatively new to > > python . Intially I am looking at something like this: > > > > A = {build some list, this calls some underlying python api and creates > > say a class A} > > B = {build another list, this creates another python class B } > > C= A+B > > > > Should i parse this as a single script or parse each statement? If i parse > > each statement how can i pass variables that were defined in the previous > > statement? As the complexity of the doamin increase, it might be the case > > that the construction of A depends on other statements. What i am looking > > for is the best approach. > > > > Thanks > > > > > > > > -------------------------------------------------------------------------- > > ---- > > Crystal Reports - New Free Runtime and 30 Day Trial > > Check out the new simplified licensing option that enables unlimited > > royalty-free distribution of the report engine for externally facing > > server and web deployment. > > http://p.sf.net/sfu/businessobjects > > _______________________________________________ > > Pyparsing-users mailing list > > Pyp...@li... > > https://lists.sourceforge.net/lists/listinfo/pyparsing-users > |