All Projects → vlasovskikh → Funcparserlib

vlasovskikh / Funcparserlib

Licence: mit
Recursive descent parsing library for Python based on functional combinators

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Funcparserlib

Parsing With Haskell Parser Combinators
🔍 A step-by-step guide to parsing using Haskell parser combinators.
Stars: ✭ 72 (-71.2%)
Mutual labels:  parser-combinators, parsing, functional-programming
Parser Combinators From Scratch
Code that accompanies the series
Stars: ✭ 56 (-77.6%)
Mutual labels:  parser-combinators, parsing, functional-programming
Parjs
JavaScript parser-combinator library
Stars: ✭ 145 (-42%)
Mutual labels:  parser-combinators, parsing, functional-programming
Angstrom
Parser combinators built for speed and memory efficiency
Stars: ✭ 434 (+73.6%)
Mutual labels:  parser-combinators, parsing
loquat
Monadic parser combinators for JavaScript / TypeScript
Stars: ✭ 47 (-81.2%)
Mutual labels:  parsing, parser-combinators
parser-combinators
Lightweight package providing commonly useful parser combinators
Stars: ✭ 41 (-83.6%)
Mutual labels:  parsing, parser-combinators
metal
A Java library for parsing binary data formats, using declarative descriptions.
Stars: ✭ 13 (-94.8%)
Mutual labels:  parsing, parser-combinators
Comby
A tool for structural code search and replace that supports ~every language.
Stars: ✭ 912 (+264.8%)
Mutual labels:  parser-combinators, parsing
Pidgin
C#'s fastest parser combinator library
Stars: ✭ 469 (+87.6%)
Mutual labels:  parser-combinators, parsing
Lug
Parsing expression grammar (PEG) embedded domain specific language and parsing machine for C++17
Stars: ✭ 44 (-82.4%)
Mutual labels:  parser-combinators, parsing
Funcj
Assorted functional-oriented data structures and algorithms for Java.
Stars: ✭ 60 (-76%)
Mutual labels:  parser-combinators, functional-programming
parser-lang
A parser combinator library with declarative superpowers
Stars: ✭ 25 (-90%)
Mutual labels:  parsing, parser-combinators
Ramble
A R parser based on combinatory parsers.
Stars: ✭ 19 (-92.4%)
Mutual labels:  parsing, parser-combinators
Pom
PEG parser combinators using operator overloading without macros.
Stars: ✭ 310 (+24%)
Mutual labels:  parser-combinators, parsing
ParsecSharp
The faster monadic parser combinator library for C#
Stars: ✭ 23 (-90.8%)
Mutual labels:  parsing, parser-combinators
Scala Parser Combinators
simple combinator-based parsing for Scala. formerly part of the Scala standard library, now a separate community-maintained module
Stars: ✭ 523 (+109.2%)
Mutual labels:  parser-combinators, parsing
autumn
A Java parser combinator library written with an unmatched feature set.
Stars: ✭ 112 (-55.2%)
Mutual labels:  parsing, parser-combinators
Syntax
Write value-driven parsers quickly in Swift with an intuitive SwiftUI-like DSL
Stars: ✭ 134 (-46.4%)
Mutual labels:  parsing, parser-combinators
Pyparsing
Python library for creating PEG parsers
Stars: ✭ 1,052 (+320.8%)
Mutual labels:  parser-combinators, parsing
Pegtl
Parsing Expression Grammar Template Library
Stars: ✭ 1,295 (+418%)
Mutual labels:  parser-combinators, parsing

funcparserlib

Recursive descent parsing library for Python based on functional combinators.

PyPI PyPI - Downloads

Description

Parser combinators are just higher-order functions that take parsers as their arguments and return them as result values. Parser combinators are:

  • First-class values
  • Extremely composable
  • Tend to make the code quite compact
  • Resemble the readable notation of xBNF grammars

Parsers made with funcparserlib are pure-Python LL(*) parsers. It means that it's very easy to write them without thinking about lookaheads and all that hardcore parsing stuff. However, the recursive descent parsing is a rather slow method compared to LL(k) or LR(k) algorithms.

So the primary domain for funcparserlib is parsing little languages or external DSLs (domain specific languages).

The library itself is very small. Its source code is only 600 lines of code, with lots of comments included. It features the longest parsed prefix error reporting, as well as a tiny lexer generator for token position tracking.

Show Me the Code

This is an excerpt from a JSON parser (RFC 4627) written using funcparserlib. This full example as well as others can be found here.

def parse(seq):
    """Sequence(Token) -> object"""
    ...
    n = lambda s: a(Token('Name', s)) >> tokval
    def make_array(n):
        if n is None:
            return []
        else:
            return [n[0]] + n[1]
    ...
    null = n('null') >> const(None)
    true = n('true') >> const(True)
    false = n('false') >> const(False)
    number = toktype('Number') >> make_number
    string = toktype('String') >> make_string
    value = forward_decl()
    member = string + op_(':') + value >> tuple
    object = (
        op_('{') +
        maybe(member + many(op_(',') + member)) +
        op_('}')
        >> make_object)
    array = (
        op_('[') +
        maybe(value + many(op_(',') + value)) +
        op_(']')
        >> make_array)
    value.define(
          null
        | true
        | false
        | object
        | array
        | number
        | string)
    json_text = object | array
    json_file = json_text + skip(finished)

    return json_file.parse(seq)

Installation

You can install the funcparserlib library from PyPI via pip:

$ pip install funcparserlib

There are no dependencies on other libraries.

Documentation

See also comments inside the modules funcparserlib.parser and funcparserlib.lexer or generate the API docs from the modules using pydoc.

There a couple of examples available in the tests/ directory:

See also the changelog and FAQ.

Performance and Code Size

Despite being an LL(*) parser, funcparserlib has a reasonable performance. For example, a JSON parser written using funcparserlib is 3 times faster than a parser using the popular pyparsing library and only 5 times slower than the specialized JSON library simplejson that uses ad hoc parsing. Here are some stats1:

File Size cjson simplejson funcparserlib json-ply pyparsing
6 KB 0 ms 45 ms 228 ms n/a 802 ms
11 KB 0 ms 80 ms 395 ms 367 ms 1355 ms
100 KB 4 ms 148 ms 855 ms 1071 ms 2611 ms
134 KB 11 ms 957 ms 4775 ms n/a 16534 ms
1009 KB 87 ms 6904 ms 36826 ms n/a 116510 ms
User Code 0.9 KLOC 0.8 KLOC 0.1 KLOC 0.5 KLOC 0.1 KLOC
Library Code 0 KLOC 0 KLOC 0.5 KLOC 5.3 KLOC 3.7 KLOC

Both funcparserlib and pyparsing have the smallest user code size (that is a common feature of parsing libraries compared to ad hoc parsers). The library code of funcparserlib is 7 times smaller (and much cleaner) than pyparsing. The json-ply uses a LALR parser ply (similar to Yacc) and performs like funcparserlib. cjson is a C library, hence the incredible performance :)

Similar Projects

  • LEPL. A recursive descent parsing library that uses two-way generators for backtracking. Its source code is rather large: 17 KLOC.
  • pyparsing. A recursive descent parsing library. Probably the most popular Python parsing library. Nevertheless, its source code is quite dirty (though 4 KLOC only).
  • Monadic Parsing in Python. A series of blog entries on monadic parsing.
  • Pysec (aka Parsec in Python). A blog entry on monadic parsing, with nice syntax for Python.

1 Testing hardware: Pentium III, 1 GHz, 512 MB. I took JSON files from a real project, in a normalized encoding, i.e. they contained no extra separators. I used version 0.3.2 of the library for testing.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].