orangeduck/mpc logo

orangeduck/mpc

Free

A Parser Combinator library for C

FreeFree tier
Type
Open Source

About orangeduck/mpc

mpc is a lightweight and powerful Parser Combinator library for C, designed for building parsers for programming languages, data formats, and domain-specific languages (DSLs). It provides type-generic, predictive, recursive descent parsing with automatic error message generation. mpc is easy to integrate as a single ANSI C source file and header, and includes both a regular expression parser generator and a language/grammar parser generator for defining grammars directly. The library avoids reliance on garbage collection, setjmp/longjmp, and namespace pollution. It offers a grammar specification mode where users can define parsing rules as strings, enabling rapid parser development. It is used for tasks such as building new languages, parsing existing ones, and embedding DSLs.

Key Features

Type-Generic Predictive Recursive Descent Parsing
Single File Integration (ANSI C source and header)
Automatic Error Message Generation
Regular Expression Parser Generator
Language/Grammar Parser Generator
No Dependence on Garbage Collection (no Boehm GC)
No setjmp/longjmp for error handling
Minimal Namespace Pollution

Pros & Cons

Pros
  • Lightweight and easy to integrate (single ANSI C file)
  • Type-generic, allowing use with arbitrary user data types
  • Automatic generation of clear error messages
  • Supports both combinator-based and grammar-string-based parsing
  • No external dependencies or garbage collection required
  • Predictive recursive descent parsing with good performance
  • Well-documented with examples and quickstart guide
Cons
  • Requires C programming knowledge and compilation
  • Limited to C/C++ projects; not a standalone application
  • Grammar syntax may have a learning curve for beginners

Best For

Building a new programming languageBuilding a new data format parserParsing an existing programming languageParsing an existing data formatEmbedding a Domain Specific Language (DSL)Implementing Greenspun's Tenth Rule

FAQ

What is mpc?
mpc is a lightweight and powerful parser combinator library for C, useful for building parsers for programming languages, data formats, and domain-specific languages.
What are the main features of mpc?
Key features include type-generic recursive descent parsing, automatic error message generation, regular expression and grammar parser generators, single-file ANSI C integration, and no reliance on garbage collection or setjmp/longjmp.
How do I use mpc to define a grammar?
You can define grammars as strings using mpca_lang() function, specifying rules in a BNF-like syntax. The library then generates parsers that produce abstract syntax trees (ASTs).
Is mpc free and open-source?
Yes, mpc is open-source and distributed under a permissive license (LICENSE.md in the repository). It is available for free on GitHub.
What are the dependencies for building mpc?
mpc has no external dependencies. It is a single C source file (mpc.c) and header (mpc.h) that compiles with any ANSI C compliant compiler.