Free-Parsec v2.0
April 24, 2026 ยท View on GitHub
C++ Monadic Parsers library
Monadic parsers similar to Haskell's Parsec.
Features
- Monadic parser combinators in C++ (like Haskell's Parsec)
- Build complex parsers by combining simple ones
- Parsers for digits, letters, literals, sequences, separated lists, etc.
- Combinators:
many,many1,sepBy1,between,count,discard,both,left,right, etc. - Easy mapping of parsed tuples into user-defined structs
- Safe runtime with error handling and position tracking
Examples
Parse a single digit:
using namespace ps;
std::string src = "1";
ParserRuntime runtime(src, State{});
ParserResult<Char> result = parseWithRuntime<Char>(runtime, digit);
if (isRight(result)) {
Char c = getParseSucceeded(result).parsed; // c == '1'
}
Parse a capitalized word:
Parser<char> firstChar = upper;
Parser<std::list<char>> restChars = many(lower);
auto seqp = sequence(firstChar, restChars);
auto parser = merge(seqp);
Parse a person info line:
"John, Doe,30,123-45-6789}"
struct PersonInfo
{
std::string firstName,
lastName;
int age;
std::string ssn;
};
auto seqp = sequence(
firstNameParser(), skip(comma), skip(many(space)),
lastNameParser(), skip(comma), skip(many(space)),
ageParser(), skip(comma), skip(many(space)),
ssnParser()
);
ps::Parser<PersonInfo> p = as<PersonInfo>(seqp);
Architecture Overview
- Parser: Main parser type, parameterized by result type.
- ParserResult: Holds either a successful parse or failure with error message and position.
- Combinators: Functions like
many,sequence,bind,fmap, etc., to build complex parsers. - ParserRuntime: Holds input string, state, and error messages. Used for running parsers safely.
- State: Simple user state (not implemented yet).
- Error Handling: Failures are tracked with messages and positions.
- Tuple Mapping: Results from sequences can be mapped into user structs with
as<T>.
This library lets you write expressive and type-safe parsers in C++ using a functional style, similar to Haskell's Parsec. You can parse structured text, validate formats, and build custom DSLs with clear and composable code.
Additional materials
- Monadic Parsers in C++ (Talk, Rus) | Slides (Eng) | My talk about previous implementation at C++ Russia 2019 Moscow.
- cpp_stm_free | Software Transactional Memory with the same Free Monads approach