Boost.Spirit comments and stuff

I have been learning Boost.Spirit over the past few days and here are a few queries: [1] Is it possible to process a token stream rather than a character stream? If so, are there any examples? The reason I am asking is... [2] Is it possible to cascade spirit grammars, e.g. wave.parse() >> cpp.parse(); // hypothetical where the output of the wave grammar feeds the token stream of the cpp grammar. I am not propsing that the above is how this would be expressed. [3] Is it possible to combine the file_iterator with the position_iterator to provide error reporting on that? [4] I am now making use of boost::spirit::sub_grammar from libs/spirit/doc/techniques.html. One thing I get from this is that the typedef for the grammar type can be very difficult to read, especially for non-trivial grammars! I have started writing a set of type helpers to simplify the process: namespace boost { namespace spirit { template< typename T1, typename T2, typename T3 > struct alternative3_t { typedef alternative< alternative< T1, T2 >, T3 > type; }; template< typename T1, typename T2, typename T3 > struct sequence3_t { typedef sequence< sequence< T1, T2 >, T3 > type; }; // confix_type = T1 >> *( CompositeT - T2 ) >> T2 template< typename CompositeT, typename T1 = strlit< const char * >, typename T2 = T1 > struct confix_t { typedef typename sequence3_t < T1, kleene_star< difference< CompositeT, T2 > >, T2 >::type type; }; template< typename T2 = strlit< const char * >, typename T1 = strlit< const char * > > struct comment_t { typedef typename confix_t< anychar_parser, T1, T2 >::type type; }; typedef comment_t< chlit< char > >::type line_comment; typedef comment_t<>::type range_comment; }} using these, the typedef for the skip_grammar example becomes: typedef typename boost::spirit::alternative3_t < boost::spirit::space_parser, boost::spirit::line_comment, boost::spirit::range_comment
::type start_t;
If there is interest, I shall create a proper submission for them. Regards, Reece _________________________________________________________________ Stay in touch with absent friends - get MSN Messenger http://www.msn.co.uk/messenger

Reece Dunn wrote:
I have been learning Boost.Spirit over the past few days and here are a few queries:
[1] Is it possible to process a token stream rather than a character stream? If so, are there any examples? The reason I am asking is...
Yes, this is possible. The only thing you'll have to respect is, that your token type should be implicitly convertible to an integer type, which isn't an limitation though, because a token always has a token id, which may returned by the corresponding operator. This technique is used by Wave, which uses a lexer component for token building and the preprocessor (and the related grammars) take the generated token stream as its input.
[2] Is it possible to cascade spirit grammars, e.g.
wave.parse() >> cpp.parse(); // hypothetical
where the output of the wave grammar feeds the token stream of the cpp grammar. I am not propsing that the above is how this would be expressed.
Yes, this is possible. Wave exposes an interator interface, where the corresponding dereferencing operator returns the preprocessed tokens. You may use this token stream for any subsequent parsing (simply pass the pair of Wave iterators to your parse() function).
[3] Is it possible to combine the file_iterator with the position_iterator to provide error reporting on that?
Yes. The position iterator is simply a wrapper, which takes any other input iterator, from where it gets its 'input'.
[4] I am now making use of boost::spirit::sub_grammar from libs/spirit/doc/techniques.html. One thing I get from this is that the typedef for the grammar type can be very difficult to read, especially for non-trivial grammars! I have started writing a set of type helpers to simplify the process:
[snip]
If there is interest, I shall create a proper submission for them.
Nice idea. Do you want to submit a patch for the libs/spirit/doc/techniques.html file? Regards Hartmut

Reece Dunn wrote:
I have been learning Boost.Spirit over the past few days and here are a few queries:
[1] Is it possible to process a token stream rather than a character stream? If so, are there any examples? The reason I am asking is...
Yes. See Hartmut Kaiser's Wave CPP preprocessor.
[2] Is it possible to cascade spirit grammars, e.g.
wave.parse() >> cpp.parse(); // hypothetical
Yes.
where the output of the wave grammar feeds the token stream of the cpp grammar. I am not propsing that the above is how this would be expressed.
[3] Is it possible to combine the file_iterator with the position_iterator to provide error reporting on that?
Yes.
[4] I am now making use of boost::spirit::sub_grammar from libs/spirit/doc/techniques.html. One thing I get from this is that the typedef for the grammar type can be very difficult to read, especially for non-trivial grammars! I have started writing a set of type helpers to simplify the process:
namespace boost { namespace spirit { template< typename T1, typename T2, typename T3 > struct alternative3_t { typedef alternative< alternative< T1, T2 >, T3 > type; };
template< typename T1, typename T2, typename T3 > struct sequence3_t { typedef sequence< sequence< T1, T2 >, T3 > type; };
// confix_type = T1 >> *( CompositeT - T2 ) >> T2
template< typename CompositeT, typename T1 = strlit< const char *
, typename T2 = T1 > struct confix_t { typedef typename sequence3_t < T1, kleene_star< difference< CompositeT, T2 > >, T2 >::type type; };
template< typename T2 = strlit< const char * >, typename T1 = strlit< const char * > > struct comment_t { typedef typename confix_t< anychar_parser, T1, T2 >::type type; };
typedef comment_t< chlit< char > >::type line_comment; typedef comment_t<>::type range_comment; }}
using these, the typedef for the skip_grammar example becomes:
typedef typename boost::spirit::alternative3_t < boost::spirit::space_parser, boost::spirit::line_comment, boost::spirit::range_comment
::type start_t;
If there is interest, I shall create a proper submission for them.
Oh, very cool! That would be very nifty indeed. Cheers, -- Joel de Guzman http://www.boost-consulting.com http://spirit.sf.net
participants (3)
-
hartmutkaiser@t-online.de
-
Joel de Guzman
-
Reece Dunn