James H. Hill | 9 Feb 23:46
Picon

Explicit Instantiation support

Hi,

I am trying to compile a really large grammar (~110+ rules and  
counting). As expected, it takes forever to compile. But what is worse  
is that Visual Studio runs out of heap space and the compilation does  
not finish. :-) I am going to take a guess and say there is a memory  
leak somewhere in their preprocessor (or its not that optimized to  
handle the cool features of Boost.Spirit 2.0), but that is besides the  
point.

So, I tried breaking the grammar to into many different files and  
compiling it. Unfortunately, I got the same results as before. I then  
realized that these are template and no matter how I break up the  
grammar, I will get the same results!!

After scratching my head, I remember *explicit instantiation* and  
thought that would help me out. The plan was to break up the large  
grammar into smaller grammars, instantiate the grammars with the  
desire iterator type, and export it from an shared library. I thought  
this should work, however, there are compile errors when trying to do  
explicit instantiation. Below is my simple grammar (thanks to breaking  
it up, I can now include the example ;-) ):

--------
namespace qi = ::boost::spirit::qi;
namespace ascii = ::boost::spirit::ascii;

template <typename IteratorT>
class ident : public qi::grammar < IteratorT,
                                    std::string (),
(Continue reading)

mlx1982 | 9 Feb 22:13
Picon
Picon
Favicon

Spirit 2.1 - Question about rules and phoenix


Hi,

I am rewriting a parser for algebraic expression from the old spirit classic
to spirit QI.

I have a problem with the new syntax for the grammar. 

The following code is working:

template <typename iterator>
struct SpiritCalculator : qi::grammar< iterator, std::vector<Real>(),
ascii::space_type >
{
    SpiritCalculator() :
        SpiritCalculator::base_type(Start)

         Start =
            (
                Expression % ','
            )
        ;

        Expression =
            (
                Element[qi::_val = qi::_1] >> *( ( qi::lit('+') >>
Element[qi::_val += qi::_1] ) |
                                                            ( qi::lit('-')

>> Element[qi::_val -= qi::_1] ) )
) ;
(Continue reading)

Michael Caisse | 9 Feb 22:55

Re: Spirit 2.1 - Question about rules and phoenix


mlx1982 wrote: > > > template <typename iterator> > struct SpiritCalculator : qi::grammar< iterator, std::vector<Real>(), > ascii::space_type > > { > SpiritCalculator() : > SpiritCalculator::base_type(Start) > > Start = > ( > Expression % ',' > ) > ; > > Expression = > ( > Element[qi::_val = qi::_1] >> *( PlusMinus[qi::_val = > qi::_1] ) > ) > ; > > PlusMinus = > ( > ( qi::lit('+') >> Element[qi::_val += qi::_1] ) | > ( qi::lit('-') >> Element[qi::_val -= qi::_1] ) > ) > ; > Element = > ( > qi::double_ > ) > ; > > qi::rule< iterator, Results_Type(), ascii::space_type > Start; > qi::rule< iterator, double(), ascii::space_type > Expression, > PlusMinus, Element; > } > > The two codes should do the same thing. However if I pass a string > "2+4,1+2+3" to the first one he correctly return ( 6, 6 ) while the second > one return ( 4, 3 ), which are the last two elements. > > Can anyone tell me how to make the second one working. I need that kind of > structure to build my parser (I need to have PlusMinus, MultiplyDivide, and > other operators spitted from the Expression). > > Thanks, > > Cristiano >
Hello Cristiano - Take a look at your PlusMinus rule. The synthesized attribute is of type double. The _val represents the rule's attribute. Entering the rule it is apparently 0. Some double_ is parsed and the added to the initial value of 0. The Expression rule also has a synthesized attribute of a double. The semantic actions are written so that its attribute is equal to whatever was last parsed, which is exactly what you are seeing. There a lot of ways to do what you want... take a look at inherited attributes in the Spirit documentation to see if that helps you out. regards - michael -- -- ---------------------------------- Michael Caisse Object Modeling Designs www.objectmodelingdesigns.com ------------------------------------------------------------------------------ SOLARIS 10 is the OS for Data Centers - provides features such as DTrace, Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW http://p.sf.net/sfu/solaris-dev2dev
(Continue reading)

Carl Barron | 9 Feb 22:58
Favicon

Re: Spirit 2.1 - Question about rules and phoenix


On Feb 9, 2010, at 4:13 PM, mlx1982 wrote:


> > Hi, > > I am rewriting a parser for algebraic expression from the old spirit classic > to spirit QI. > > I have a problem with the new syntax for the grammar. > > The following code is working: > > template <typename iterator> > struct SpiritCalculator : qi::grammar< iterator, std::vector<Real>(), > ascii::space_type > > { > SpiritCalculator() : > SpiritCalculator::base_type(Start) > > Start = > ( > Expression % ',' > ) > ; > > Expression = > ( > Element[qi::_val = qi::_1] >> *( ( qi::lit('+') >> > Element[qi::_val += qi::_1] ) | > ( qi::lit('-') >>> Element[qi::_val -= qi::_1] ) ) > ) > ; > > Element = > ( > qi::double_ > ) > ; > > qi::rule< iterator, Results_Type(), ascii::space_type > Start; > qi::rule< iterator, double(), ascii::space_type > Expression, Element; > } > > However I need to split the rule Expression to insert other parts. Doing > this in the following way my code is no more working (it compiles but the > result is wrong): > > template <typename iterator> > struct SpiritCalculator : qi::grammar< iterator, std::vector<Real>(), > ascii::space_type > > { > SpiritCalculator() : > SpiritCalculator::base_type(Start) > > Start = > ( > Expression % ',' > ) > ; > > Expression = > ( > Element[qi::_val = qi::_1] >> *( PlusMinus[qi::_val = > qi::_1] ) > ) > ; > > PlusMinus = > ( > ( qi::lit('+') >> Element[qi::_val += qi::_1] ) | > ( qi::lit('-') >> Element[qi::_val -= qi::_1] ) > ) > ; > Element = > ( > qi::double_ > ) > ;
well x=0; x+=y ,x contains y, That is what your actions are doing. easiest solution is to define an expression in terms of both + and - operators, since you are just computing the expression. Something like: expr = term [ql::_val=qi::_1] >> *( '+' >> term[ qi::_val += qi::_1] | '-' >> term [qi::_val -= qi::_1] ); ------------------------------------------------------------------------------ SOLARIS 10 is the OS for Data Centers - provides features such as DTrace, Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW http://p.sf.net/sfu/solaris-dev2dev
(Continue reading)

Chris Hoeppler | 9 Feb 23:01
Picon

Re: Spirit 2.1 - Question about rules and phoenix

> I am rewriting a parser for algebraic expression from the old spirit
> classic to spirit QI.

<snip />

> However I need to split the rule Expression to insert other parts. Doing
> this in the following way my code is no more working (it compiles but the
> result is wrong):
>
> template <typename iterator>
> struct SpiritCalculator : qi::grammar< iterator, std::vector<Real>(),
> ascii::space_type >
> {
>     SpiritCalculator() :
>         SpiritCalculator::base_type(Start)
>
>          Start =
>             (
>                 Expression % ','
>             )
>         ;
>
>         Expression =
>             (
>                 Element[qi::_val = qi::_1] >> *( PlusMinus[qi::_val =
> qi::_1] )
>             )
>         ;
>
>         PlusMinus =
>             (
>                 ( qi::lit('+') >> Element[qi::_val += qi::_1] ) |
>                 ( qi::lit('-') >> Element[qi::_val -= qi::_1] )
>             )
>         ;
>         Element =
>             (
>                   qi::double_
>             )
>         ;
>
>     qi::rule< iterator, Results_Type(), ascii::space_type > Start;
>     qi::rule< iterator, double(), ascii::space_type > Expression,
> PlusMinus, Element;
> }
>
> The two codes should do the same thing. However if I pass a string
> "2+4,1+2+3" to the first one he correctly return ( 6, 6 ) while the second
> one return ( 4, 3 ), which are the last two elements.
>
> Can anyone tell me how to make the second one working. I need that kind of
> structure to build my parser (I need to have PlusMinus, MultiplyDivide, and
> other operators spitted from the Expression).

Did you have a look at boost/libs/spirit/example/qi/calc3.cpp ? 

If you really want to split the rules as shown above, you'd have pass
the current value of Expression to PlusMinus. See attached.

HTH,
Chris
Attachment (mlx1982.cpp): text/x-c++src, 1669 bytes
------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev
_______________________________________________
Spirit-general mailing list
Spirit-general <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/spirit-general
(Continue reading)

Steffen Rauh | 9 Feb 19:31
Picon
Picon

Parsing escaped strings

Hi all,

I am trying to parse escaped strings (backslash as escape char) with spirit 2.2. I do not want spirit to
unescape the strings itself but to simply provide the complete string to the attached action (I have my own
code to unescape those strings afterwards).

The code at the end shows how I try to do it. With line #2 active, the code compiles and fails with parsing the
provided string as expected. With line #1 active, compilation fails with a message that value_type
cannot be converted to std::basic_string (\spirit\home\support\attributes.hpp(409)). Is there
something I got wrong with the attributes?

A search on google showed one solution by using a symbol table. But this won't be practical in my case,
because escaped strings can be multiple bytes encoded as hex (i.e. \x11223344). I did not show this in the
example for simplification.

Any suggestions?

Regards,
Steffen

--------------------------------------------

#include <iostream>
#include <string>

#include "boost/config/warning_disable.hpp"
#include "boost/spirit/include/qi.hpp"
#include "boost/spirit/include/phoenix_core.hpp"
#include "boost/spirit/include/phoenix_operator.hpp"
#include "boost/spirit/include/phoenix_fusion.hpp"
(Continue reading)

Michael Caisse | 9 Feb 20:36

Re: Parsing escaped strings

Steffen Rauh wrote:
> Hi all,
>   
> I am trying to parse escaped strings (backslash as escape char) with spirit 2.2. I do not want spirit to
unescape the strings itself but to simply provide the complete string to the attached action (I have my own
code to unescape those strings afterwards).
>
> The code at the end shows how I try to do it. With line #2 active, the code compiles and fails with parsing the
provided string as expected. With line #1 active, compilation fails with a message that value_type
cannot be converted to std::basic_string (\spirit\home\support\attributes.hpp(409)). Is there
something I got wrong with the attributes?
>   

Yes. StringChar has a synthesized attribute as char and Escapped has
one of string. The result is therefore a variant or char or string.

Also... if this is what you want, why break out the escape character at
all? If you are doing something with it later anyhow I don't see why
you are trying to detect it in the parse.

michael

--

-- 

----------------------------------
Michael Caisse
Object Modeling Designs
www.objectmodelingdesigns.com

------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev
(Continue reading)

Hartmut Kaiser | 8 Feb 17:09
Picon

[Boost.Spirit] How to Adapt Templates as a Fusion Sequence

Boost.Spirit has posted a new item, 'How to Adapt Templates as a Fusion
Sequence'

Here is another question raised from time to time: "I know how to use a plain
struct as an attribute for a sequence parser in Qi by adapting it with
BOOST_FUSION_ADAPT_STRUCT. Unfortunately this does not work if the struct is a
template. What can I do in this case?".

There have been plans for a while to create a separate Fusion facility
BOOST_FUSION_ADAPT_TPL_STRUCT allowing to adapt templated data types, but this
is not in place yet. Today I will describe a trick you can apply to adapt your
templates into 'proper' Fusion sequences anyway.

You may view the latest post at
http://boost-spirit.com/home/2010/02/08/how-to-adapt-templates-as-a-fusion-sequence/

You received this e-mail because you asked to be notified when new updates are
posted.
Best regards,
Hartmut Kaiser
hartmut.kaiser <at> gmail.com

------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
_______________________________________________
Spirit-general mailing list
(Continue reading)

John Wilkinson | 7 Feb 21:43
Picon

template parameter for BOOST_FUSION_ADAPT_STRUCT?

Hi, I have a grammer that I want to work with string or wstrings or perhaps a user supplied string type. I
cannot see a way to get BOOST_FUSION_ADAPT_STRUCT to work with different sting types without defining a
BOOST_FUSION_ADAPT_STRUCT for each type, e.g.:

template< class String_t >
struct Pair
{
    String_t s1, s2;
};

template < class String_t >
struct Grammer : qi::grammar< typename String_t::const_iterator, Pair< String_t >(),
ascii::space_type >
{
    Grammer() : Grammer::base_type( pair_ )
    {
        pair_ = str_ >> ':' >> str_;
        str_ = *(qi::char_ - ':');
    }

    qi::rule< iterator_type, Pair< String_t >(), ascii::space_type > pair_;
    qi::rule< iterator_type, String_t(), ascii::space_type > str_;
};

BOOST_FUSION_ADAPT_STRUCT(
    Pair< string >,
    (string, s1)
    (string, s2)
)

(Continue reading)

Carl Barron | 7 Feb 22:30
Favicon

Re: template parameter for BOOST_FUSION_ADAPT_STRUCT?


On Feb 7, 2010, at 3:43 PM, John Wilkinson wrote:

> > Is there a way to only have one BOOST_FUSION_ADAPT_STRUCT that will work with any Pair type?
#include <boost/fusion/include/std_pair.hpp> Now any std::pair<T1,T2> is a fusion sequence ------------------------------------------------------------------------------ The Planet: dedicated and managed hosting, cloud storage, colocation Stay online with enterprise data centers and the best network in the business Choose flexible plans and management services without long-term contracts Personal 24x7 support from experience hosting pros just a phone call away. http://p.sf.net/sfu/theplanet-com
Joel de Guzman | 8 Feb 02:43
Picon

Re: template parameter for BOOST_FUSION_ADAPT_STRUCT?


On 2/8/2010 4:43 AM, John Wilkinson wrote: > Hi, I have a grammer that I want to work with string or wstrings or perhaps a user > supplied string type. I cannot see a way to get BOOST_FUSION_ADAPT_STRUCT to work with > different sting types without defining a BOOST_FUSION_ADAPT_STRUCT for each type,
CC'ing Christopher Schmidt This is an often requested feature that's not implemented yet. Anyone with an itch for Boost PP hacking is welcome to hack on it. Perhaps something like: BOOST_FUSION_ADAPT_TPL_STRUCT( name ((T1)(T2) ... (TN)) // template parameters (member_type0, member_name0) (member_type1, member_name1) ... ) In the meantime, you can use std::pair since it has already been adapted. Regards, -- -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net http://www.facebook.com/djowel Meet me at BoostCon http://www.boostcon.com/home http://www.facebook.com/boostcon ------------------------------------------------------------------------------ The Planet: dedicated and managed hosting, cloud storage, colocation
(Continue reading)


Gmane