Tuesday, April 9, 2013

pyparsing black magic

I really love python and I really hate black magic. Especially when it comes to programming, I hate it when I find solutions I cannot explain... exactly. This is one of those cases.

I've been using pyparsing to make some parsers for at least 3 different types of syntax. Pyparsing is an excellent module that allows you to easily create parsers, based on BnF notation. Each individual parser I made, was eventually working as expected when tested alone. As soon as I tried combining the parsers all hell broke loose. I started getting strange errors from the pyparsing module such as infinite loops and a TypeError with `obj must be an instance or subtype of type`. It felt like one parser 'influenced' the other and I was able to confirm that by changing the order in which they were called. I even made a post to the pyparsing wiki which at this time hasn't been answered.

After much searching I came to the following 'solution'. Most tutorials on pyparsing suggest you import everything from the pyparsing module like so:
from pyparsing import *

Now I remember reading somewhere that this is not a good practice in general and its better to import specific modules or the generic module itself. So, I changed my parsers to just import pyparsing
import pyparsing

Of course I had to start using the full path name for the classes I used but it actually paid off. After I did that everything started working as expected. If anyone can offer a better solution I'm open to suggestions.

No comments: