Grammar and how to combine rules?

View: New views
5 Messages — Rating Filter:   Alert me  

Grammar and how to combine rules?

by ariasgore :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,
Ive spent quite a lot of time reading the head documentation of spirit in 1.41 trunk  but I am unsure how to work with grammars.
(Disclaimer: I am spending most of the time in the Qi Tutorials and docs)

Is there a way to combine rules without having a grammar? I guess not, right?
Ok then, how do I combine rules in a grammar? The roman_numerals example falls short of showing how start can be build out of several other rules (no symbols or anything)

Thanks
--
GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Spirit-general mailing list
Spirit-general@...
https://lists.sourceforge.net/lists/listinfo/spirit-general

Re: Grammar and how to combine rules?

by Hartmut Kaiser :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Ive spent quite a lot of time reading the head documentation of spirit
> in 1.41 trunk  but I am unsure how to work with grammars.
> (Disclaimer: I am spending most of the time in the Qi Tutorials and
> docs)
>
> Is there a way to combine rules without having a grammar? I guess not,
> right?
> Ok then, how do I combine rules in a grammar? The roman_numerals
> example falls short of showing how start can be build out of several
> other rules (no symbols or anything)

Grammars are generally a good way of encapsulating several rules allowing to
have a parser recognizing more complex input.

template <typename Iterator>
struct my_grammar : qi::grammar<Iterator>
{
    my_grammar() : my_grammar::base_type(r1)
    {
        r1 = r2 % ',';
        r2 = *(char_ - ',');
    };
    qi::rule<Iterator> r1, r2;
};

Then, this grammar can be used as any other parser:

    my_grammar g;
    qi::parse(begin, end, g >> qi::eol);

Same is true for rules, once defined they can be used wherever a parser
instance is required (see r1 = r2 % ','; above).

Is that what you need to know?
Regards Hartmut

-------------------
Meet me at BoostCon
http://boostcon.com




------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Spirit-general mailing list
Spirit-general@...
https://lists.sourceforge.net/lists/listinfo/spirit-general

Re: Grammar and how to combine rules?

by ariasgore :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,
thanks for the reply.

Please correct me, but how must a rule look like that allows 0 or n repetitions?

My grammar looks like (with semantic actions)
     
 _comment = lit("//") >>
         *spirit::ascii::blank >>
         lexeme[*(char_ - eol)][boost::bind(&IACOSemantic::processComment, semantic, _1)] >>
         *spirit::ascii::blank >> -eol;

      start = *_comment;

"start" is supposed to be extend by other rules, but this time the semantic action should fire each "comment" line.
However this results in a compiler error:
C2440: '<function-style-cast>': 'value_type' can not be converted into 'std::basic_string<_Elem,_Traits,_Ax>'
 (attributes.hpp)

My iterator uses string::iterator, so start and _comment are defined as:
      spirit::qi::rule<std::string::iterator , spirit::ascii::space_type , std::string()> comment;

      spirit::qi::rule<std::string::iterator , spirit::ascii::space_type , std::string()> start;



-------- Original-Nachricht --------
> Datum: Mon, 26 Oct 2009 10:09:17 -0500
> Von: "Hartmut Kaiser" <hartmut.kaiser@...>
> An: "\'Spirit General Mailing List\'" <spirit-general@...>
> Betreff: Re: [Spirit-general] Grammar and how to combine rules?

> > Ive spent quite a lot of time reading the head documentation of spirit
> > in 1.41 trunk  but I am unsure how to work with grammars.
> > (Disclaimer: I am spending most of the time in the Qi Tutorials and
> > docs)
> >
> > Is there a way to combine rules without having a grammar? I guess not,
> > right?
> > Ok then, how do I combine rules in a grammar? The roman_numerals
> > example falls short of showing how start can be build out of several
> > other rules (no symbols or anything)
>
> Grammars are generally a good way of encapsulating several rules allowing
> to
> have a parser recognizing more complex input.
>
> template <typename Iterator>
> struct my_grammar : qi::grammar<Iterator>
> {
>     my_grammar() : my_grammar::base_type(r1)
>     {
>         r1 = r2 % ',';
>         r2 = *(char_ - ',');
>     };
>     qi::rule<Iterator> r1, r2;
> };
>
> Then, this grammar can be used as any other parser:
>
>     my_grammar g;
>     qi::parse(begin, end, g >> qi::eol);
>
> Same is true for rules, once defined they can be used wherever a parser
> instance is required (see r1 = r2 % ','; above).
>
> Is that what you need to know?
> Regards Hartmut
>
> -------------------
> Meet me at BoostCon
> http://boostcon.com
>
>
>
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> Spirit-general mailing list
> Spirit-general@...
> https://lists.sourceforge.net/lists/listinfo/spirit-general

--
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Spirit-general mailing list
Spirit-general@...
https://lists.sourceforge.net/lists/listinfo/spirit-general

Re: Grammar and how to combine rules?

by ariasgore :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,
after several investigations I am still confused:

I can write something like

start = _comment >> _comment;
or
start = _comment;

but not start = _comment >> + _comment;
or start = *_comment;

The result: C2440 error: 'conversion' : cannot convert from 'value_type' to 'std::basic_string<_Elem,_Traits,_Ax>'

What am I doing wrong here? Both are rules with same signature but it seems like any modificator results in some conversion errors? I tried to copy that from examples in the docs.

Thanks
Sam

-------- Original-Nachricht --------
> Datum: Mon, 26 Oct 2009 16:23:02 +0100
> Von: ariasgore@...
> An: hartmut.kaiser@..., Spirit General Mailing List <spirit-general@...>
> Betreff: Re: [Spirit-general] Grammar and how to combine rules?

> Hello,
> thanks for the reply.
>
> Please correct me, but how must a rule look like that allows 0 or n
> repetitions?
>
> My grammar looks like (with semantic actions)
>      
>  _comment = lit("//") >>
>          *spirit::ascii::blank >>
>          lexeme[*(char_ - eol)][boost::bind(&IACOSemantic::processComment,
> semantic, _1)] >>
>          *spirit::ascii::blank >> -eol;
>
>       start = *_comment;
>
> "start" is supposed to be extend by other rules, but this time the
> semantic action should fire each "comment" line.
> However this results in a compiler error:
> C2440: '<function-style-cast>': 'value_type' can not be converted into
> 'std::basic_string<_Elem,_Traits,_Ax>'
>  (attributes.hpp)
>
> My iterator uses string::iterator, so start and _comment are defined as:
>       spirit::qi::rule<std::string::iterator , spirit::ascii::space_type ,
> std::string()> comment;
>
>       spirit::qi::rule<std::string::iterator , spirit::ascii::space_type ,
> std::string()> start;
>
>
>
> -------- Original-Nachricht --------
> > Datum: Mon, 26 Oct 2009 10:09:17 -0500
> > Von: "Hartmut Kaiser" <hartmut.kaiser@...>
> > An: "\'Spirit General Mailing List\'"
> <spirit-general@...>
> > Betreff: Re: [Spirit-general] Grammar and how to combine rules?
>
> > > Ive spent quite a lot of time reading the head documentation of spirit
> > > in 1.41 trunk  but I am unsure how to work with grammars.
> > > (Disclaimer: I am spending most of the time in the Qi Tutorials and
> > > docs)
> > >
> > > Is there a way to combine rules without having a grammar? I guess not,
> > > right?
> > > Ok then, how do I combine rules in a grammar? The roman_numerals
> > > example falls short of showing how start can be build out of several
> > > other rules (no symbols or anything)
> >
> > Grammars are generally a good way of encapsulating several rules
> allowing
> > to
> > have a parser recognizing more complex input.
> >
> > template <typename Iterator>
> > struct my_grammar : qi::grammar<Iterator>
> > {
> >     my_grammar() : my_grammar::base_type(r1)
> >     {
> >         r1 = r2 % ',';
> >         r2 = *(char_ - ',');
> >     };
> >     qi::rule<Iterator> r1, r2;
> > };
> >
> > Then, this grammar can be used as any other parser:
> >
> >     my_grammar g;
> >     qi::parse(begin, end, g >> qi::eol);
> >
> > Same is true for rules, once defined they can be used wherever a parser
> > instance is required (see r1 = r2 % ','; above).
> >
> > Is that what you need to know?
> > Regards Hartmut
> >
> > -------------------
> > Meet me at BoostCon
> > http://boostcon.com
> >
> >
> >
> >
> >
> ------------------------------------------------------------------------------
> > Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> > is the only developer event you need to attend this year. Jumpstart your
> > developing skills, take BlackBerry mobile applications to market and
> stay
> > ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> > http://p.sf.net/sfu/devconference
> > _______________________________________________
> > Spirit-general mailing list
> > Spirit-general@...
> > https://lists.sourceforge.net/lists/listinfo/spirit-general
>
> --
> Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5
> -
> sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> Spirit-general mailing list
> Spirit-general@...
> https://lists.sourceforge.net/lists/listinfo/spirit-general

--
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Spirit-general mailing list
Spirit-general@...
https://lists.sourceforge.net/lists/listinfo/spirit-general

Re: Grammar and how to combine rules?

by Hartmut Kaiser :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> after several investigations I am still confused:
>
> I can write something like
>
> start = _comment >> _comment;
> or
> start = _comment;
>
> but not start = _comment >> + _comment;
> or start = *_comment;
>
> The result: C2440 error: 'conversion' : cannot convert from
> 'value_type' to 'std::basic_string<_Elem,_Traits,_Ax>'
>
> What am I doing wrong here? Both are rules with same signature but it
> seems like any modificator results in some conversion errors? I tried
> to copy that from examples in the docs.

You need to take into account the attribute propagation rules as well.
Kleene (*) and Plus (+) have this rule:

    a: A --> *a: vector<A>
    a: A --> +a: vector<A>

which reads as: if 'a' is a parser and its attribute type is 'A', then the
attribute of '*a' will be 'vector<A>' (or any STL container for that
matter). You should really read the introduction and browse through the rest
of the docs to get a better understanding how this works. Without an
understanding of Spirit's attribute handling you won't get far along (see
for instance here: http://tinyurl.com/kj9nvp).

That means the attributes of your grammar need to be

    rule<iterator, std::string> comment = ...;
    rule<iterator, std::vector<std::string> > start = *comment;    

which will compile just fine.

Regards Hartmut

-------------------
Meet me at BoostCon
http://boostcon.com

>
> Thanks
> Sam
>
> -------- Original-Nachricht --------
> > Datum: Mon, 26 Oct 2009 16:23:02 +0100
> > Von: ariasgore@...
> > An: hartmut.kaiser@..., Spirit General Mailing List <spirit-
> general@...>
> > Betreff: Re: [Spirit-general] Grammar and how to combine rules?
>
> > Hello,
> > thanks for the reply.
> >
> > Please correct me, but how must a rule look like that allows 0 or n
> > repetitions?
> >
> > My grammar looks like (with semantic actions)
> >
> >  _comment = lit("//") >>
> >          *spirit::ascii::blank >>
> >          lexeme[*(char_ -
> eol)][boost::bind(&IACOSemantic::processComment,
> > semantic, _1)] >>
> >          *spirit::ascii::blank >> -eol;
> >
> >       start = *_comment;
> >
> > "start" is supposed to be extend by other rules, but this time the
> > semantic action should fire each "comment" line.
> > However this results in a compiler error:
> > C2440: '<function-style-cast>': 'value_type' can not be converted
> into
> > 'std::basic_string<_Elem,_Traits,_Ax>'
> >  (attributes.hpp)
> >
> > My iterator uses string::iterator, so start and _comment are defined
> as:
> >       spirit::qi::rule<std::string::iterator ,
> spirit::ascii::space_type ,
> > std::string()> comment;
> >
> >       spirit::qi::rule<std::string::iterator ,
> spirit::ascii::space_type ,
> > std::string()> start;
> >
> >
> >
> > -------- Original-Nachricht --------
> > > Datum: Mon, 26 Oct 2009 10:09:17 -0500
> > > Von: "Hartmut Kaiser" <hartmut.kaiser@...>
> > > An: "\'Spirit General Mailing List\'"
> > <spirit-general@...>
> > > Betreff: Re: [Spirit-general] Grammar and how to combine rules?
> >
> > > > Ive spent quite a lot of time reading the head documentation of
> spirit
> > > > in 1.41 trunk  but I am unsure how to work with grammars.
> > > > (Disclaimer: I am spending most of the time in the Qi Tutorials
> and
> > > > docs)
> > > >
> > > > Is there a way to combine rules without having a grammar? I guess
> not,
> > > > right?
> > > > Ok then, how do I combine rules in a grammar? The roman_numerals
> > > > example falls short of showing how start can be build out of
> several
> > > > other rules (no symbols or anything)
> > >
> > > Grammars are generally a good way of encapsulating several rules
> > allowing
> > > to
> > > have a parser recognizing more complex input.
> > >
> > > template <typename Iterator>
> > > struct my_grammar : qi::grammar<Iterator>
> > > {
> > >     my_grammar() : my_grammar::base_type(r1)
> > >     {
> > >         r1 = r2 % ',';
> > >         r2 = *(char_ - ',');
> > >     };
> > >     qi::rule<Iterator> r1, r2;
> > > };
> > >
> > > Then, this grammar can be used as any other parser:
> > >
> > >     my_grammar g;
> > >     qi::parse(begin, end, g >> qi::eol);
> > >
> > > Same is true for rules, once defined they can be used wherever a
> parser
> > > instance is required (see r1 = r2 % ','; above).
> > >
> > > Is that what you need to know?
> > > Regards Hartmut
> > >
> > > -------------------
> > > Meet me at BoostCon
> > > http://boostcon.com
> > >
> > >
> > >
> > >
> > >
> > ---------------------------------------------------------------------
> ---------
> > > Come build with us! The BlackBerry(R) Developer Conference in SF,
> CA
> > > is the only developer event you need to attend this year. Jumpstart
> your
> > > developing skills, take BlackBerry mobile applications to market
> and
> > stay
> > > ahead of the curve. Join us from November 9 - 12, 2009. Register
> now!
> > > http://p.sf.net/sfu/devconference
> > > _______________________________________________
> > > Spirit-general mailing list
> > > Spirit-general@...
> > > https://lists.sourceforge.net/lists/listinfo/spirit-general
> >
> > --
> > Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla
> Firefox 3.5
> > -
> > sicherer, schneller und einfacher!
> http://portal.gmx.net/de/go/chbrowser
> >
> > ---------------------------------------------------------------------
> ---------
> > Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> > is the only developer event you need to attend this year. Jumpstart
> your
> > developing skills, take BlackBerry mobile applications to market and
> stay
> > ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> > http://p.sf.net/sfu/devconference
> > _______________________________________________
> > Spirit-general mailing list
> > Spirit-general@...
> > https://lists.sourceforge.net/lists/listinfo/spirit-general
>
> --
> Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox
> 3.5 -
> sicherer, schneller und einfacher!
> http://portal.gmx.net/de/go/chbrowser
>
> -----------------------------------------------------------------------
> -------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart
> your
> developing skills, take BlackBerry mobile applications to market and
> stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> Spirit-general mailing list
> Spirit-general@...
> https://lists.sourceforge.net/lists/listinfo/spirit-general


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Spirit-general mailing list
Spirit-general@...
https://lists.sourceforge.net/lists/listinfo/spirit-general