0

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?

Greg
  • 8,175
  • 16
  • 72
  • 125
  • Perhaps because it is some parser generator which is simple to use? – Basile Starynkevitch Sep 29 '17 at 10:25
  • 1
    Basically what you are asking is what the benefits are of using a library such as Ragel for string parsing, instead of rolling your own implementation in plain c++? – simon Sep 29 '17 at 10:26
  • Did you read [Ragel intro](http://www.colm.net/open-source/ragel/)? Alternatives might be [flex](https://github.com/westes/flex), [bison](https://www.gnu.org/software/bison/), [ANTLR](http://www.antlr.org/) – Basile Starynkevitch Sep 29 '17 at 10:27
  • @gurka I've never come across Ragel before so it is completely new to me. Found it odd in a strictly c++ codebase – Greg Sep 29 '17 at 10:28
  • @BasileStarynkevitch Thank you. Are there other common alternatives to Ragel? – Greg Sep 29 '17 at 10:29
  • Why do people use C++ instead of C? Why do people use C instead of assembly? I can write my own string lexing code but ragel (or re2c, etc) does it better, faster, and with fewer bugs. – Kelvin Sherlock Oct 09 '17 at 19:11

1 Answers1

5

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!

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547