Boost logo

Boost :

Subject: Re: [boost] compile time parser generator
From: Ábel Sinkovics (abel_at_[hidden])
Date: 2012-01-08 04:53:38


Hi,

> Regular expressions.
>
> If there were an interpreted language (perhaps domain specific) with a simple grammar then we could inline string constants with code into C++ code that gets parsed at compile time to generate executable code. So simple things like sed and awk scripts could also be inlined into C++ as string literals and translated to executable code at compile time instead of runtime if you combine the interpreter library with the compile time parsing library. The benefit over run time interpretation of the string is debatable. It is only a performance optimization. I would say regular expression is the thing that is crying out for it. The real question is what kind of syntax errors will it generate when parsing fails? It would be nice to catch badly formed regular expressions at compile time, for example, though not so nice if the errors are not so nice. What this allows us to do is extend the language in the form of libraries (in a hackish sort of way) which is the desire of modern
> language theorists from what I've been told by my language theorist friends. I'd say it's almost as exciting as overloading the comma operator.

If you check the examples for our compile-time parsing library, there is
a (yet incomplete) wrapper around Xpressive: you give it a compile-time
string, it parses and checks it and builds an sregex object for you.
Link to the example:
https://github.com/sabel83/mpllibs/tree/master/libs/metaparse/example/regexp

Our library deals with human-readable error messages as well, but the
error message is returned as a template metaprogramming structure which
can be pretty-printed. So the approach for getting a human-readable
error message is the following:
- You make a mistake in your DSL
- You get an error from the C++ compiler
- You instantiate a special template, debug_parsing_error in a separate
program
- You compile and run it
- The error message is printed to stdout

You can find an example for that (not for regular expressions but for
another parser) here:
https://github.com/sabel83/mpllibs/tree/master/libs/metaparse/example/parsing_error
The output of the example is the following:

Compile-time parsing results
----------------------------
Input text:
aaac

Parsing failed:
Error at source_position<int_<1>, int_<4>, char_<'a'>>: 'b' literal
expected.

Regards,
   Ábel


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk