Search code examples
c++ragel

What are the reasons for using Ragel to parse strings in a C++ codebase?


I inherited a C++ project which uses Ragel for string parsing.

This is the first time I have seen this being done and I would like to understand why someone would use Ragel instead of C++ to parse a string?


Solution

  • parser generators (improperly called "compiler-compilers") are very handsome to use and produce reliable and efficient C++ or C code (notably because parsing theory is well understood).

    In general, using source code generators could be a wise thing to do. Sometimes, notably in large projects, it is sensible to write your own one (read about metaprogramming, notably SICP and even J.Pitrat's blog). Good build automation tools like GNU make or ninja can easily be configured to run C or C++ code generators and use them at build time.

    Read Ragel intro. Look also into flex, bison, ANTLR, rpcgen, Qt moc, swig, gperf as common examples of C or C++ generators.

    In some programs, you could even use some JIT compilation library (such as libgccjit or LLVM) to dynamically generate code at run time and use it. On POSIX systems you could also generate at runtime a temporary C or C++ file, compile it as a plugin, and load that temporary plugin using dlopen & dlsym. Having a good culture about compilers and interpreters (e.g. thru the Dragon Book) is then worthwhile.

    Embedding some interpreter (like lua or guile) in your application is also an interesting approach. But it is a strong architectural decision.

    In many cases, generating source code is easier than hand writing it. Of course that is not always possible.

    PS. I never heard of Ragel before reading your question!