Skip to content

Instantly share code, notes, and snippets.

@magcius
Forked from washort/tutorial.rst
Created August 14, 2012 04:36
Show Gist options
  • Save magcius/3346293 to your computer and use it in GitHub Desktop.
Save magcius/3346293 to your computer and use it in GitHub Desktop.
parsley docs

Parsley Tutorial

Let's parse it up with Parsley. Following the standard example of other parsing frameworks, we're going to build a standard mathematical expression parser. Unlike other frameworks like PyParsing, Parsley grammars are defined in a giant string, rather than a special DSL.

Let's start with something that can interpret a collection of digits, and spit out a number:

import parsley

grammar = """
    digit = :x ?(x in '0123456789') -> x
    number = <digit+> :ds -> int(''.join(ds))
"""

parse_digits = parsley.make_parser(grammar)

And now we can use it:

>>> from digitparser import parse_digits
>>> parse_digits("12345")
12345

Don't ask me how the hell that works or what the hell that means I stole it from washort and he doesn't know either apparently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment