Skip to main content

emmtrix Regex-to-C Code Generator (emx-regex-cgen) compiles regular expressions into portable, static C code for embedded and performance-critical applications.

CI License: MIT

Key Characteristics

  • Two backend engines — choose between a table-driven DFA and a bit-parallel NFA (--engine dfa, --engine bitnfa, or the default --engine auto which tries DFA first and falls back to bitnfa when the DFA state limit is exceeded).
  • Table-driven DFA — every regex is compiled to a minimised deterministic finite automaton; the generated C code performs a single linear scan over the input.
  • Bit-parallel NFA — the regex is compiled to a position NFA (a Thompson NFA without ε-states) and simulated with precomputed bitmasks; the hot loop is fully unrolled with no loops over words. The variant (uint8, uint16, uint32, or uint32_t[N]) is selected automatically based on the number of NFA positions.
  • No dynamic memory allocation — all data is static const; no malloc, no free.
  • Simple inner loop — the matching loop performs a single table lookup per byte (DFA, branch-free unless early_exit is set) or one bit test per NFA position (bitnfa); no data-dependent inner loops.
  • re2 feature set — supports the same subset of regular expressions as Google's re2 library (no back-references, no look-around).
  • Fullmatch semantics — the generated function checks whether the entire input matches the pattern.

Installation

pip install emx-regex-cgen

The distribution name is emx-regex-cgen. The Python import path is emx_regex_cgen.

After installation, you can use the package either as a CLI tool via emx-regex-cgen or as a Python library via from emx_regex_cgen import generate.

Quick Start

Python Library

generate() returns a GeneratedCode object with four parts. Call .render() to get the combined C source string, or access individual fields to embed them in an existing code-generation pipeline.

from emx_regex_cgen import generate

# generate() returns a GeneratedCode object
result = generate(r"\d{4}-\d{2}-\d{2}")

# .render() assembles the complete C source (same output as before)
print(result.render())

# Access individual parts:
result.includes        # ['stddef.h', 'stdbool.h', 'stdint.h']
result.globals         # static const transition table (and optional maps)
result.match_function  # bool regex_match(const char *input, size_t len) { … }
result.main_function   # int main(…) { … }  — None when emit_main=False

# Include a main() for standalone testing
result = generate(r"[a-z]+", emit_main=True)

# Bytes mode: '.' matches any single byte, classes work on raw byte values
result = generate(r"[\x80-\xff]+", encoding="bytes")

# Use the bit-parallel NFA backend instead of DFA
result = generate(r"hello", engine="bitnfa")

Invalid or unsupported patterns raise emx_regex_cgen.PatternError, a subclass of re.error; invalid option values raise ValueError.

from emx_regex_cgen import PatternError, generate

try:
    generate(r"(?<=a)b")
except PatternError as exc:
    print(exc)  # look-behind is not supported at position 0

generate() options

generate(
    pattern,
    flags="",
    *,
    emit_main=False,
    prefix="regex",
    encoding="utf8",
    engine="auto",
    row_dedup="auto",
    alphabet_compression="auto",
    size_threshold=8192,
    early_exit=False,
)

Library arguments map directly to the generator settings:

Argument Type Default Meaning
pattern str - Regular expression to compile.
flags str "" Regex flags: i (ignore case), s (dot-all), m (multiline). x is accepted and ignored; inline (?x) enables free-spacing syntax.
emit_main bool False Include a standalone main() function in the generated C code.
prefix str "regex" Prefix for generated C identifiers such as regex_match; must be a C identifier (ValueError otherwise).
encoding str "utf8" "utf8" for Unicode-aware UTF-8 input, or "bytes" for raw byte semantics (as re2's Latin-1 mode: the i flag folds ASCII letters only).
engine str "auto" Backend engine: "auto" (DFA first, bitnfa fallback), "dfa", or "bitnfa".
row_dedup str "auto" DFA only: "yes", "no", or "auto" for transition-row deduplication.
alphabet_compression str "auto" DFA only: "yes", "no", or "auto" for byte equivalence-class compression.
size_threshold int 8192 DFA only: threshold used by "auto" for row_dedup and alphabet_compression.
early_exit bool False DFA only: break out of the loop once the dead state is reached.

Example with multiple options:

from emx_regex_cgen import generate

result = generate(
    r"[a-z]+\d+",
    flags="i",
    prefix="user_id",
    emit_main=True,
    engine="dfa",
    encoding="utf8",
    row_dedup="yes",
    alphabet_compression="auto",
    size_threshold=4096,
    early_exit=True,
)

print(result.render())

CLI

# Write generated C code to stdout
emx-regex-cgen '[a-z]+\d+'

# Write to a file with a main() function
emx-regex-cgen '[a-z]+\d+' --emit-main -o matcher.c

# Compile and test
gcc -O2 -o matcher matcher.c
./matcher "hello42"   # exit 0 (match)
./matcher "HELLO"     # exit 1 (no match)

# Bytes mode: match any sequence of high bytes
emx-regex-cgen --encoding bytes '[\x80-\xff]+' --emit-main -o byte_matcher.c

# Use the bit-parallel NFA backend
emx-regex-cgen --engine bitnfa 'hello' --emit-main -o bitnfa_matcher.c

CLI Reference

usage: emx-regex-cgen [-h] [-o OUTPUT] [--emit-main] [--prefix PREFIX]
                      [--flags FLAGS] [--encoding {utf8,bytes}]
                      [--row-dedup {auto,yes,no}]
                      [--alphabet-compression {auto,yes,no}]
                      [--size-threshold SIZE_THRESHOLD]
                      [--early-exit {yes,no}] [--engine {auto,dfa,bitnfa}]
                      pattern

Generate C code that performs a fullmatch for a regular expression.

positional arguments:
  pattern               Regular expression pattern

options:
  -h, --help            show this help message and exit
  -o, --output OUTPUT   Output file (default: stdout)
  --emit-main           Also emit a main() function (exit 0=match, 1=no match,
                        2=error)
  --prefix PREFIX       Prefix for all generated C identifiers: arrays and
                        match function; must be a C identifier (default:
                        regex)
  --flags FLAGS         Regex flags: i (case-insensitive), s (dot-all), m
                        (multiline); x is accepted and ignored (use inline
                        (?x) for free-spacing)
  --encoding {utf8,bytes}
                        Input encoding: utf8 (default, Unicode-aware) or bytes
                        (raw byte semantics)
  --row-dedup {auto,yes,no}
                        Transition-row deduplication: yes (always), no
                        (never), auto (when table exceeds --size-threshold;
                        default)
  --alphabet-compression {auto,yes,no}
                        Alphabet compression into equivalence classes: yes
                        (always), no (never), auto (when table exceeds --size-
                        threshold; default)
  --size-threshold SIZE_THRESHOLD
                        Table-size threshold (cells = states × 256) for auto
                        mode (default: 8192)
  --early-exit {yes,no}
                        Emit early-exit check inside the DFA loop: yes (break
                        when dead state is reached), no (always process full
                        input; default)
  --engine {auto,dfa,bitnfa}
                        Backend engine: auto (try DFA first, fall back to
                        bitnfa; default), dfa (table-driven minimised DFA),
                        bitnfa (bit-parallel NFA)

Supported Features

Regex Features

Feature Example pattern DFA bitnfa
Literal string hello literal.c literal_bitnfa.c
Character class [a-z0-9_]+ char_class.c char_class_bitnfa.c
Negated class [^aeiou]+ negated_class.c negated_class_bitnfa.c
Dot (any char except \n) .+ dot.c dot_bitnfa.c
Alternation cat|dog|fish alternation.c alternation_bitnfa.c
Star quantifier * ab*c quantifier_star.c quantifier_star_bitnfa.c
Plus quantifier + ab+c quantifier_plus.c quantifier_plus_bitnfa.c
Optional quantifier ? colou?r quantifier_optional.c quantifier_optional_bitnfa.c
Bounded repeat {m,n} a{2,4} quantifier_repeat.c quantifier_repeat_bitnfa.c
Digit escape \d \d{4}-\d{2}-\d{2} escape_digit.c escape_digit_bitnfa.c
Word escape \w \w+ escape_word.c escape_word_bitnfa.c
Space escape \s \s+ escape_space.c escape_space_bitnfa.c
Unicode / UTF-8 \x{00e9}+ unicode.c unicode_bitnfa.c
Anchors ^ / $ ^start.*end$ anchors.c anchors_bitnfa.c
Word boundary \b / \B \bword\b word_boundary.c word_boundary_bitnfa.c
Unicode property \p{…} / \P{…} \p{Nd}+ unicode_property.c unicode_property_bitnfa.c

Syntax and Semantics

The accepted syntax and the match semantics follow re2:

  • Groups — (…), (?:…), (?P<name>…) and (?<name>…) all group (a name is letters, digits and _, as in re2); captures do not exist in a fullmatch automaton. Lazy quantifiers (*?, +?, …) and the U flag are accepted and match the same language as their greedy forms.
  • Inline flags — (?i), (?s), (?m), (?x) and (?i:…) / (?-i:…). A bare (?flags) applies until the end of the enclosing group, including later alternatives. Under (?x) blanks and # comments are ignored, also before a quantifier ((?x)a * is a*).
  • Anchors — ^ / \A match at the start of the input, $ / \z only at its end (no "before a final \n" as in Python or PCRE). With m, ^ and $ also match after / before every \n. \b / \B are ASCII word boundaries. Since the generated function performs a fullmatch, anchors matter where they sit inside the pattern, e.g. a(?m:$)\nb.
  • Character classes — ranges, escapes, \p{…} / \P{…} and POSIX classes [[:alpha:]] / [[:^alpha:]] (ASCII, inside a bracket expression only).
  • Errors as in re2 — back-references, look-ahead and look-behind, unknown escapes (\h, \R, \Z, …: only ASCII punctuation may be escaped to stand for itself), reversed ranges ([z-a]), {m,n} with m > n and a counted repeat of a repeat (a*{2}) raise PatternError.
  • Repeat counts — unlike re2, counts above 1000 are accepted; the automaton size limits apply instead.

CLI Options

Option Effect Golden reference
--flags i Case-insensitive matching flag_ignorecase.c · bitnfa
--flags s Dot matches \n (dot-all) flag_dotall.c · bitnfa
--flags m Multiline anchors flag_multiline.c · bitnfa
--flags x Accepted and ignored; free-spacing needs inline (?x) (as in the golden) flag_verbose.c · bitnfa
--encoding bytes Raw byte semantics (no UTF-8) encoding_bytes.c · bitnfa
--prefix NAME Custom identifier prefix prefix.c · bitnfa
--emit-main Include standalone main() emit_main.c · bitnfa
--alphabet-compression yes Byte equivalence-class compression alphabet_compression.c
--row-dedup yes Transition-row deduplication row_dedup.c
--early-exit yes Break DFA loop on dead state (early exit) early_exit.c

Bit-NFA Variants

The bitnfa engine automatically selects the narrowest integer type that can hold all NFA positions:

Variant NFA positions Golden reference
uint8_t ≤ 8 bitnfa_uint8.c
uint16_t ≤ 16 bitnfa_uint16.c
uint32_t ≤ 32 bitnfa_uint32.c
uint32_t[N] > 32 bitnfa_uint32_array.c

Generated Code Structure

DFA Engine (default)

The generator produces:

  1. An optional alphabet map (static const uint8_t regex_alphabet[256]) mapping each byte to its equivalence class (emitted when alphabet compression is active).
  2. A transition table (static const uint8_t regex_transitions[M][C]) mapping (row, column) → next_state. Dimensions depend on active optimisations: M is the number of unique rows (with row dedup) or states; C is the number of equivalence classes (with alphabet compression) or 256.
  3. An optional row map (static const uint8_t regex_row_map[N]) mapping state → row index (emitted when row deduplication is active).
  4. A match function with the signature:
    bool regex_match(const char *input, size_t len);
    
  5. Optionally a main() function for standalone executables.

Example Output (pattern hello, --alphabet-compression yes --row-dedup yes)

static const uint8_t regex_alphabet[256] = { /* byte → class */ };

static const uint8_t regex_transitions[6][5] = {
    /* states 0, 6 */ { 0 },
    /* state 1 */ { [2] = 2 },
    ...
};

static const uint8_t regex_row_map[7] = { 0, 1, 2, 3, 4, 5, 0 };

bool regex_match(const char *input, size_t len) {
    uint8_t state = 1;
    for (size_t i = 0; i < len; i++) {
        state = regex_transitions[regex_row_map[state]][regex_alphabet[(unsigned char)input[i]]];
    }
    return state >= 6;
}

Bit-NFA Engine (--engine bitnfa)

The generator produces:

  1. One transition mask table per NFA position that needs one (static const uint8_t regex_trans_P[256]). Each entry is the bitmask of destination positions for the input byte. The integer width (uint8_t / uint16_t / uint32_t, or uint32_t[N] words beyond 32 positions) is chosen automatically. A position that moves on a single byte gets an inline comparison instead of a table.
  2. A match function with a fully unrolled inner loop — one if (state & bit) next |= …; per active position.
  3. Optionally a main() function for standalone executables.

Example Output (pattern h[ae]llo, --engine bitnfa)

static const uint8_t regex_trans_1[256] = { ['a'] = 0x04u, ['e'] = 0x04u };

bool regex_match(const char *input, size_t len) {
    uint8_t state = 0x01u;
    for (size_t i = 0; i < len; i++) {
        unsigned char b = (unsigned char)input[i];
        uint8_t next = 0;
        if (state & 0x01u) next |= ((b == 'h') ? 0x02u : 0u);
        if (state & 0x02u) next |= regex_trans_1[b];
        if (state & 0x04u) next |= ((b == 'l') ? 0x08u : 0u);
        if (state & 0x08u) next |= ((b == 'l') ? 0x10u : 0u);
        if (state & 0x10u) next |= ((b == 'o') ? 0x20u : 0u);
        state = next;
    }
    return (state & 0x20u) != 0;
}

Testing

Tests are parameterised from two suites in tests/data/: re2_compat_results.json (2 500+ patterns extracted from the PCRE2 test suite and validated against Google re2) and tokenizer_compat_results.json (pre-tokenizer regexes of popular Hugging Face tokenizer.json files, validated with the regex module).

# Run all tests (parallel)
pytest -n auto -q

# Run linter
ruff check src/ tests/

Test Strategy

  1. Generate C code from the regex pattern.
  2. Compile with gcc -O2.
  3. Execute the binary on each test subject.
  4. Compare the exit code against the expected match/no-match result.

Development

# Clone
git clone --recurse-submodules https://github.com/emmtrix/emx-regex-cgen.git
cd emx-regex-cgen

# Install
pip install emx-regex-cgen

# Or, for local development
pip install -e ".[dev]"

# Test
pytest -n auto -q

# Lint
ruff check src/ tests/

License

MIT — Copyright (c) 2026 emmtrix Technologies GmbH

Maintained by

emmtrix Technologies GmbH

Metadata

Release files for emx-regex-cgen 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for emx-regex-cgen 0.2.1
File Size Uploaded Provenance Actions
emx_regex_cgen-0.2.1.tar.gz 186.0 kB With provenance Details for emx_regex_cgen-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for emx-regex-cgen 0.2.1
File Interpreter ABI Platform Actions
emx_regex_cgen-0.2.1-py3-none-any.whl Python 3 none any Details for emx_regex_cgen-0.2.1-py3-none-any.whl

Total release size: 234.7 kB

Release files / emx_regex_cgen-0.2.1.tar.gz

Download URL emx_regex_cgen-0.2.1.tar.gz
Size 186.0 kB
Tags Source
SHA-256 checksum
How to use checksums
23a995b3b64db7c7ef39880767bb2c073b4e15cb323edc603eb8e9d134c1091c
BLAKE2b-256 checksum
How to use checksums
118cd4118db78bcd892db97b06d1a2f712d67f969adc8471611ac15db3dcb7fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

Release files / emx_regex_cgen-0.2.1-py3-none-any.whl

Download URL emx_regex_cgen-0.2.1-py3-none-any.whl
Size 48.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
56e7e6770c1b5b35b8f4e6284be9bf8935b2a5f600b69d56cd682bfb2f7bdaac
BLAKE2b-256 checksum
How to use checksums
92a6b765cd1926f74b1d17224177fdf4c4fc6474a8b83cfd8f7d47b1528e8ebd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

Security for emx-regex-cgen 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for emx_regex_cgen-0.2.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page