pixel art of Toronto

Monkey's Typewriter

Monkey's Typewriter is a PEG parsing framework for Python designed for simplicity, ease of understanding and liberal interpretation of PEG expression grammars.

The Details.

Monkey's Typewriter is zero dependency and uses python's internal re regex library for matching axiomatic elements and then 3 complex matchers to allow you to express your grammars.

Pros and Cons

  • Pro: The entire library is 6 files (ignoring examples and module files), and consists of only 6 classes and two functions to make writing easy
  • Pro: You can write the grammars in an object oriented way, declaring your own custom matcher classes, allowing you spread your parser across multiple files, and unit test individual smaller matchers
  • Con: This library does not perfectly conform to PEG parsing standards, for example, left recursion can be made to work using custom matchers
  • Con: This library does not have complex quantification expressions, in-built zero-consumption negative lookahead expressions etc. They're fairly easily implementable, but not available as standard

Monkey's Typewriter is a tiny PEG library for writing any grammars

import mnkytw
Integer = mnkytw.MatchAlternation([
    mnkytw.RegexMatch(r"[1-9][0-9]*"),
    mnkytw.LiteralMatch("0")
])

mnkytw.parse("42", Integer)

Monkey's Typewriter implements six basic matchers, LiteralMatch, RegexMatch, MatchAlternation, MatchJoin, and MatchQuantity, which allow you to build more complex grammars, capable of parsing any language. Because each matcher returns only basic results you can build more complex match classes that structure your parse tree in a data structure you like.

Licence.

Monkey's Typewriter is licenced under the Apache Licence 2.0 and may be integrated into both non-commercial and commercial projects.