Thursday, June 16, 2011

Pyparsing examples in Parcon

A lot of people at #python have encouraged me to port some of Pyparsing's examples to Parcon, so I've decided to write up a blog post with some of those examples. Some of them parse the same input but produce output that's slightly different from what the corresponding Pyparsing example produces; such differences are noted accordingly.

The main difference between the Pyparsing version and the Parcon version is that the result doesn't contain the "," and the "!". In my experience, the output of literals in the grammar is typically ignored, so Parcon by default discards literal values. If the value of a literal is important, SignificantLiteral("...") should be used instead; changing "," to SignificantLiteral(",") and "!" to SignificantLiteral("!") would have made the Parcon example follow Pyparsing's behavior.

Now let's try another one. This one is Chemical Formulas, the second example on this page. It doesn't include the atomic weight calculator at present, but I'll add this in at some point. Here's the parser:

The last example I'm providing in this post is wordsToNum, the fourth example on the same page that chemicalFormula appeared on. It takes a human-readable number specification like "twelve thousand three hundred forty five" and parses it into the representative number (12345, in that case).

Blog Archive

Contributors

Donations

Parcon is written by Alex Boyd, a student who spends time working on Parcon that could otherwise be spent working at his job earning money to pay for college. Donations are therefore greatly appreciated.