Hammer is a parsing library. Like many modern parsing libraries, it provides a parser combinator interface for writing grammars as inline domain-specific languages, but Hammer also provides a variety of parsing backends. It's also bit-oriented rather than character-oriented, making it ideal for parsing binary data such as images, network packets, audio, and executables.
Hammer is written in C and provides packrat, regex/RVM, LL(k), LALR(k), and GLR parsing backends.
Hammer has a hosted-C99 core with build-selected POSIX, portable, and Windows providers.
| Setting | Provider |
|---|---|
auto |
POSIX on Unix-like systems, Windows on Windows, portable otherwise |
posix |
POSIX terminal, clock, mutex, and thread-local-storage support |
windows |
Windows terminal, performance-counter, synchronization, and TLS support |
portable |
Hosted-C99 fallback with no OS threading dependency |
custom |
An application-provided provider source |
- Bit-oriented -- grammars can include single-bit flags or multi-bit constructs that span character boundaries with no hassle
- Thread-safe, reentrant on threaded providers -- each parse uses independent parse-local state. POSIX and Windows provide thread-local parser graph construction and synchronized graph ownership/destruction. The portable provider supports one serial graph construction context.
- Parsing backends -- Packrat for general parser-combinator grammars, regex/RVM for regular grammars, plus LL(k), LALR(k), and GLR for context-free grammars
- Windows supports the core static library; shared libraries, examples, bindings, and pkg-config metadata remain unavailable there.
- SCons
- gcc
sudo apt install gcc sconsmacOS uses the POSIX provider. Its native C library build, test, and install workflow is validated with Apple's Command Line Tools and Homebrew:
xcode-select --install
brew install scons glib pkg-configOn Apple Silicon, use a native ARM64 shell and native Homebrew tools (normally
under /opt/homebrew), rather than mixing them with Rosetta or Intel Homebrew
tools. Hammer's install prefix is independent of Homebrew's prefix: select the
Hammer destination explicitly, then expose that destination's pkg-config
metadata when building consumers.
scons install prefix=/absolute/path/to/hammer
export PKG_CONFIG_PATH=/absolute/path/to/hammer/lib/pkgconfig
pkg-config --modversion libhammer- pkg-config
- glib-2.0-dev
sudo apt install pkg-config libglib2.0-devTo build, type scons.
To run the built-in test suite, type scons test.
To avoid the test dependencies, add --no-tests.
For a debug build, add --variant=debug.
To make Hammer available system-wide, use scons install. This places include files in /usr/local/include/hammer and library files in /usr/local/lib by default; to install elsewhere, add a prefix=<destination> argument, e.g. scons install prefix=$HOME.
To remove installed files, use scons uninstall (with the same prefix if non-default).
To check the version of Hammer currently installed:
PKG_CONFIG_PATH=/usr/local/lib/pkgconfig pkg-config --modversion libhammerJust #include <hammer/hammer.h> (also #include <hammer/glue.h> if you plan to use any of the convenience macros) and link with -lhammer.
If you've installed Hammer system-wide, you can use pkg-config in the usual way.
Packrat is the default backend:
HParser *p = h_sequence(h_ch('a'), h_ch('b'), NULL);
HParseResult *result = h_parse(p, (const uint8_t *)"ab", 2);To use another backend, compile the parser before parsing:
h_compile(p, PB_REGULAR, NULL); /* regex/RVM */
h_compile(p, PB_LL, (void *)2); /* LL(2) */
h_compile(p, PB_LALR, NULL); /* LALR(1), the default k */
h_compile(p, PB_GLR, NULL); /* GLR(1), accepts table conflicts */Backend names can also be queried with h_query_backend_by_name("regex"), h_query_backend_by_name("llk"),
h_query_backend_by_name("lalr"), or h_query_backend_by_name("glr").
For backend differences, limitations, and examples, see the Hammer wiki.
To learn about Hammer, see the Hammer wiki.
h_parse() takes an input length in bytes. With the Packrat backend,
h_parse_bits() takes an exact input length in bits, including partial final bytes:
const uint8_t input[] = {0xa5};
HParser *p = h_sequence(h_bits(3, false), h_end_p(), NULL);
HParseResult *result = h_parse_bits(p, input, 3);
if (result != NULL)
h_parse_result_free(result);The buffer must contain at least ceil(bit_length / 8) bytes. Bits beyond
the specified length are outside the logical input. The length limits the
input but does not require the parser to consume it all. Use h_end_p()
to require complete consumption.
h_parse_bits_debug() collects diagnostics like h_parse_debug(), with an
input length in bits. Both functions have __m variants for custom allocators.
These exact-bit APIs return NULL for backends other than Packrat.
h_seek(), h_seek__m(), and C++ Seek() now accept int64_t offsets
instead of ssize_t. Rebuild dependent applications and update function-pointer
declarations that use the previous signatures. This change can break binary
compatibility on 32-bit platforms.
Hammer provides bindings for use from languages other than C.
Requires SWIG 4.x, Python 3.8+, and setuptools.
sudo apt install swig
scons bindings=pythonSee src/bindings/python/README.md for the full API reference and usage guide.
Requires SWIG 4.x and JDK 11+.
sudo apt install swig default-jdk
scons bindings=javaSee src/bindings/java/README.md for the full API reference and usage guide.
Requires g++ and libgtest-dev (for tests).
sudo apt install libgtest-dev
scons bindings=cppSee src/bindings/cpp/README.md for the full API reference and usage guide.
Requires SWIG 4.x and Go 1.22.2.
sudo apt install swig golang
scons bindings=goSee src/bindings/go/README.md for the full API reference and usage guide.
The examples/ directory contains some simple examples, currently including:
See the example-specific READMEs and the wiki for build and walkthrough details.
For information on contributing to Hammer, including development setup, code formatting guidelines, and documentation generation, please see DEVELOPMENT.md.
Send an email to parsing@riversideresearch.org
This material is based upon work supported by the Defense Advanced Research Projects Agency (DARPA) under Prime Contract No. HR001119C0077. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the Defense Advanced Research Projects Agency (DARPA).
This work is a fork of the repository found at: https://gitlab.special-circumstanc.es/hammer/hammer
Distribution A: Approved for Public Release
