AI, Software Engineering
Hybrid approach combining formal rules and AI outperforms both for translating C macros to Rust
A new study shows that pairing formal translation methods with large language models beats either approach alone when converting legacy C code with macros to safer languages.
Translating C code to memory-safe languages like Rust is a critical challenge for maintaining legacy infrastructure, but real-world C programs rely heavily on macros—named code fragments that aren't part of C itself and pose unique translation problems.
The macro translation problem#
Existing translation systems sidestep macros by preprocessing C code before converting it, which eliminates macro usage entirely by expanding definitions inline. This approach loses structural fidelity to the original code. Researchers investigating whether macros could be translated directly while preserving their usage in the resulting code discovered that no standardized solution existed—and the task had barely been studied.
Introducing MerC and MacroBench#
To address this gap, researchers developed MerC, the first formally-specified macro translator, which identifies and codifies language features that macros and C have in common. They also created MacroBench, the first evaluation benchmark for macro translation, populated with test cases drawn from macros occurring in real-world C programs.
In initial testing, MerC successfully handled 50% of MacroBench's test cases, producing translations with no errors.
LLMs show promise but with caveats#
Large language models proved significantly more capable, translating between 22% and 77% more test cases than MerC alone. However, this greater coverage came at a quality cost: 8% to 28% of LLM translations contained errors requiring human developer review, whereas MerC's translations were consistently correct.
The tag team advantage#
The crucial finding emerged when researchers combined both approaches sequentially: running MerC first, then applying LLMs to unresolved cases. This hybrid strategy achieved a 32% lower failure rate than LLMs operating independently, while also translating an average of 51% more test cases than MerC by itself.
The research demonstrates that pairing formal verification with neural models creates synergies neither method achieves in isolation—a template potentially applicable to other code modernization and translation tasks.

