[#grammar] [section grammar] [h1 Synopsis] template struct grammar { template struct apply; template struct import; template struct rule; }; [h1 Description] [note Note that using this adds a significant overhead to your builds. When someone uses your parser, the compiler will have to build your grammar parser, use it to parse your grammar and build your parser and then it can parse the input the user would like to parse with your parser. You might consider using the parser combinators the library provides. ] Parser combinator for constructing parsers based on an embedded DSL similar to EBNF. It can be used the following way: grammar<> // definitions where a definition can be a rule or an import command. Rules look like on of the following: ::rule ::rule `name` consists of letters, digits and the `_` character. It is the name of the symbol being defined. `def` describes the rule. It can be * the name of a symbol * a terminal, which is a character between single quotes. `\` can be used for escaping. The following are accepted: `\n`, `\r`, `\t`, `\\`, `\'` * a sequence of definitions * a definition followed by the `*` character, which means repetition accepting 0 match * a definition followed by the `+` character, which means repetition expecting at least one match * a definition in brackets Rules take an optional `semantic_action` argument. It is a placeholder expression taking one argument. When this is given, this is used to transform the result of the rule. Imports can be used to turn an arbitrary parser into a symbol available for the rules. Import definitions look like the following: ::import `name` is the name of the symbol, `parser` is the parser to bind the name to. The start symbol of the grammar is specified by the template argument of the `grammar` template. This is optional, the default value is `S`. Note that the current implementation "inlines" the referenced symbols while parsing the grammar and recursion is not supported because of this. [h1 Header] #include [h1 Example] #define BOOST_METAPARSE_LIMIT_STRING_SIZE 64 #include #include #include #include #include #include #include #include #include #include #include using boost::metaparse::token; using boost::metaparse::int_; using boost::metaparse::build_parser; using boost::metaparse::entire_input; using boost::metaparse::grammar; using boost::mpl::front; using boost::mpl::back; using boost::mpl::plus; using boost::mpl::fold; using boost::mpl::lambda; using boost::mpl::_1; using boost::mpl::_2; template struct lazy_plus : boost::mpl::plus {}; template struct lazy_fold : fold {}; using plus_action = lazy_fold, front<_1>, lambda>>::type>; using plus_grammar = grammar ::import>::type ::rule::type ::rule>::type ::rule::type ; using plus_parser = build_parser>; static_assert( plus_parser::apply::type::value == 10, "Arithmetic expression should be evaluated" ); [endsect]