Electronic – How to implement regular expressions on an embedded device

picxc8

I have some (I thought) pretty portable code that uses <regex.h>. When I tried to port this over to compile for a PIC18 with xc8, the compiler could not find the header file for the library.

module/ciface/ciface.h:12:10: fatal error: 'regex.h' file not found
#include <regex.h>
         ^~~~~~~~~
1 error generated.

Is there any way to use regexs on an embedded device?

Best Answer

Since you wrote your own answer, it's clear that you wanted to promote a particular solution.

In a more general sense, the standard C regex library is for both compiling and executing regular expressions, and relies on services provided by a POSIX-compliant operating system. Compiling is much more complicated (it involves first parsing the regular expression itself) and memory-intensive, and that's why it isn't available on a target system that doesn't have an OS, or has only a simple OS that doesn't include regex support.

But if your regex is known at compile time, then the only thing you need to do on the embedded target is execute it. The development system host can do all of the heavy lifting associated with compiling it.

re2c is an interesting approach, but hardly the only one. The lexical analyzer generators lex and flex do the same thing, using a different syntax to describe the expression(s). They also generate "pure C" code that can be compiled into a target system.

If you need to parse more complicated syntax, then you also have the parser generators yacc and bison. These tools have been used for ages to process command lines, both on desktop and embedded systems.