Hartmut Kaiser | 1 Sep 01:42
Picon
Gravatar

Re: Synthetic attributes


> > Alastair Rankine wrote:
> >> I'm probably doing something very obviously wrong, but for the life
> of
> >> me I can't see it. Here's my version of what is supposed to be a
> >> simplistic HTML parser using synthetic attributes and locals. When I
> >> compile using gcc against the latest SVN trunk, I get a compiler
> error:
> >>
> >> .../spirit.cpp:24: error: no match for 'operator=' in
> >> 'boost::spirit::_val = boost::spirit::_1'
> >> .../boost-trunk/boost/spirit/home/phoenix/core/actor.hpp:61: note:
> >> candidates are: boost::phoenix::actor<boost::spirit::attribute<0> >&
> >> boost::phoenix::actor<boost::spirit::attribute<0> >::operator=(const
> >> boost::phoenix::actor<boost::spirit::attribute<0> >&)
> >
> > Try including:
> > #include <boost/spirit/include/phoenix_core.hpp>
> > #include <boost/spirit/include/phoenix_operator.hpp>
> >
> > Regards,
> 
> Ah actually it looks like I was trying to get the result of the start
> rule which wasn't defined as returning one...  (thus satisfying the
> "very obviously wrong" criteria above).
> 
> However I've fixed the problem but now it seems to behave somewhat
> erratically, and again I can't quite see why. By erratically I mean
> that the "iter == testString.end()" test check fails, even though the
> parse is successful. And slightly more worryingly, if I change the test
(Continue reading)

Joel de Guzman | 1 Sep 02:55
Picon
Favicon

Re: Synthetic attributes

Hartmut Kaiser wrote:
>> However I've fixed the problem but now it seems to behave somewhat
>> erratically, and again I can't quite see why. By erratically I mean
>> that the "iter == testString.end()" test check fails, even though the
>> parse is successful. And slightly more worryingly, if I change the test
>> case to use std::string::const_iterators, the whole application
>> segfaults inside spirit...
> 
> You need to specify the same skipper for all of the rules as you're using
> for the grammar. If you do that it doesn't segfault anymore (but it hangs,
> but this seems to be because of the grammar itself, I didn't look too
> closely, though).
> 
> I'll see what we can do to detect this kind of error at compile time.

This is definitely a bug that should be fixed. Spirit should not
segfault on wrong skippers.

Regards,
--

-- 
Joel de Guzman
http://www.boostpro.com
http://spirit.sf.net

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
(Continue reading)

Steffen Roeber | 1 Sep 10:11
Picon
Favicon

grammar as rule

Hi,

I think my problem is a little bit difficult to describe. But I will try. I have a grammar called “base grammar”. This grammar parses something and saves it to a member variable (result). This works fine.

Now I have a new grammar with an enhanced syntax. This grammar uses the “base grammar” as a rule, what works too. But now I want to take the result of the base grammar and assign it to a member of my enhanced grammar.

For this I use a closure. But the result is always empty though I see that the base grammar works fine by itself.

 

  struct StationClosure : boost::spirit::closure<StationClosure, cObref, QString, QString>

  {

    member1 data; //this shall get the result of the base grammar

    member2 type;

    member3 name;

  };

 

 

      stationRule = (stringRule[stationRule.type = myConstruct(phoenix::arg1, phoenix::arg2)] >>

                    stringRule[stationRule.name = myConstruct(phoenix::arg1, phoenix::arg2)] >>

                    baseGrammar[stationRule.data = baseGrammar.result]) >>

                    [self.stationFunc(stationRule.data, stationRule.type, stationRule.name)];

 

 

BaseGrammar baseGrammar;

rule<ScannerT, StationClosure::context_t>    stationRule;

 

What could go wrong?

 

Kind regards

 

Hella Aglaia Mobile Vision GmbH

Dipl. Ing. Steffen Roeber

Software Tools

Treskowstraße 14, 13089 Berlin,

Handelsregister Amtsgericht Charlottenburg HRB 66976

Geschäftsführer: Thomas Schilling

Tel: +49 30 2000 429 – 412
Fax: +49 30 2000 429 – 109
Mail: steffen.roeber <at> aglaia-gmbh.de
URL: www.aglaia-gmbh.de
URL: www.mobilevision.de

 

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Spirit-general mailing list
Spirit-general <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/spirit-general
Hartmut Kaiser | 1 Sep 11:01
Picon
Gravatar

Re: Synthetic attributes

> Hartmut Kaiser wrote:
> >> However I've fixed the problem but now it seems to behave somewhat
> >> erratically, and again I can't quite see why. By erratically I mean
> >> that the "iter == testString.end()" test check fails, even though
> the
> >> parse is successful. And slightly more worryingly, if I change the
> test
> >> case to use std::string::const_iterators, the whole application
> >> segfaults inside spirit...
> >
> > You need to specify the same skipper for all of the rules as you're
> using
> > for the grammar. If you do that it doesn't segfault anymore (but it
> hangs,
> > but this seems to be because of the grammar itself, I didn't look too
> > closely, though).
> >
> > I'll see what we can do to detect this kind of error at compile time.
> 
> This is definitely a bug that should be fixed. Spirit should not
> segfault on wrong skippers.

I agree. I'll see what I can do.

Regards Hartmut

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
Carl Barron | 1 Sep 17:02

[lex] type in token_def to get an unused_type as attribute when handled by qi.

  What type x is needed in token_def<x> to get an attribute of  
unused_type when the token is
parsed by qi?

	key %= tok.key >> tok.item >> tok.end_key;

	tok.key and tok.end_key should have synthetic attribute of  
unused_type, so they
are silently omitted.   tok,item has the iterator range as its  
attribute so
attribute of key can be std::string, which is its synthetic attribute...

	

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
Hartmut Kaiser | 1 Sep 19:39
Picon
Gravatar

Re: [lex] type in token_def to get an unused_type as attribute when handled by qi.

>   What type x is needed in token_def<x> to get an attribute of
> unused_type when the token is
> parsed by qi?
> 
> 	key %= tok.key >> tok.item >> tok.end_key;
> 
> 	tok.key and tok.end_key should have synthetic attribute of
> unused_type, so they
> are silently omitted.   tok,item has the iterator range as its
> attribute so
> attribute of key can be std::string, which is its synthetic
> attribute...

token_def<lex::omitted>

Regards Hartmut

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
Lindley M French | 2 Sep 03:32
Picon
Favicon

[Spirit] Creating a vector of structs

(This has already been asked on the Boost list, I was told I should repeat it here. I'm stuck with Boost 1.35
for now, so I don't think I have access to Spirit 2.)

I'm playing around with Spirit for a particular parsing task. I could do this manually, but I figured this
was a good chance to learn what Spirit had to offer.

The trouble is this. While Spirit offers ready-made semantic actions for parsing primitives, it doesn't
seem to offer anything terribly useful for compound statements. As far as I can tell, you can define your
own semantic actions but they simply take in the matched string as an argument----there's no way to get at
the values parsed on that level.

For example, let's say I have a type
struct Person
{
   string name;
   double height;
   int age;
   Person(string n, double h, int a)
   : name(n), height(h), age(a)
};

And I have a document containing

5.5 15 Henry
4.6 32 Jane
6.1 21 Bob

I could parse this using

rule<> personrule = strict_real_p >> int_p >> ~eol_p;
parse(str, *(personrule[WhatGoesHere] >> eol_p) >> end_p);

But how can I build a vector<Person> out of this? I can write my own functor to do the push_back, but I need to
get at the parsed values of name, age, and height for each personrule somehow.

I suppose I *could* just run another parse() on the input the semantic action for that specific
person....but that really seems like it shouldn't be necessary.

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
Carl Barron | 2 Sep 05:10

Re: [Spirit] Creating a vector of structs


On Sep 1, 2009, at 9:32 PM, Lindley M French wrote:

> (This has already been asked on the Boost list, I was told I should  
> repeat it here. I'm stuck with Boost 1.35 for now, so I don't think  
> I have access to Spirit 2.)
>
> I'm playing around with Spirit for a particular parsing task. I  
> could do this manually, but I figured this was a good chance to  
> learn what Spirit had to offer.
>
> The trouble is this. While Spirit offers ready-made semantic actions  
> for parsing primitives, it doesn't seem to offer anything terribly  
> useful for compound statements. As far as I can tell, you can define  
> your own semantic actions but they simply take in the matched string  
> as an argument----there's no way to get at the values parsed on that  
> level.
>
> For example, let's say I have a type
> struct Person
> {
>   string name;
>   double height;
>   int age;
>   Person(string n, double h, int a)
>   : name(n), height(h), age(a)
> };
>
> And I have a document containing
>
> 5.5 15 Henry
> 4.6 32 Jane
> 6.1 21 Bob
>
> I could parse this using
    you have a file of triples  (double,int,std::string)

  you could use phoenix 1.x and closures but its probably easier in  
this case since
you have a vector of these and the order is not the same as your struct.

     something like

     struct parse_struct:spirit::grammar<...>
     {
	YourType & triple;

	parse_struct( Your_type &t):triple(t){}

	template <class Scanner>
	struct definition
	{
	   definition(const parse_struct &s)
	   {
		r1 =  strict_real [assign_a(s.triple.height)]
		   >> int_p [assign_a(s.triple.age)]
		   >> (+(char_p - eol_p) ) [assign_a(s.triple.name)]
		   >> eol_p;
	   }
	   const rule<Scanner>  r1;
	   const rule<Scanner> const &start() { return r1;}
	};
     };

     struct parse_file:spirit::grammar<parse_file>
     {
	std::vector<Person> & people;
	parse_file(std::vector<Person> &p):people(p){}

	template <class Scanner>
	struct definition
	{
	   Person t;
	   definition(const parse_file &file):person(t)
	   {
		file = 	person [push_back_a(file.people,t)]
			>> *( person [push_back_a(file.people,t)]);
	   }
	   rule<Scanner> file;
	   rule<Scanner> const & start() const { return file;}
	};
    };

    looks close [its been ages since I used spirit 1.x]

    now  std::vector<Person>  people;
	parse_file	file(people);
	parse( ..., file,skipper);
	
	should produce the results in people, up to a parse error.

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
Alastair Rankine | 2 Sep 12:52
Favicon

Re: Synthetic attributes

On 2009-09-01 09:42:09 +1000, "Hartmut Kaiser" <hartmut.kaiser <at> gmail.com> said:

> You need to specify the same skipper for all of the rules as you're using
> for the grammar. If you do that it doesn't segfault anymore (but it hangs,
> but this seems to be because of the grammar itself, I didn't look too
> closely, though).

Ah right, thanks, that fixed the problem. The infinite loop was 
relatively easy to fix too.

Just for the record, here is the correctly working test case:

#define BOOST_TEST_MODULE spirit test
#include <boost/test/unit_test.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>

using namespace boost::spirit;
using namespace boost::spirit::qi;
using boost::spirit::ascii::space_type;

template <typename Iterator>
struct MiniHTML : public qi::grammar<Iterator, space_type>
{
    MiniHTML()
        : MiniHTML::base_type(htmlBlock)
    {
        using boost::spirit::ascii::char_;
        using boost::spirit::ascii::no_case;
        using boost::spirit::ascii::string;

        blockTag %= no_case[
            // Just keep it simple for now...
            string("div") | lexeme[char_('h') >> char_("1-6")] | string("p")
            ];

        blockStart = lit('<')
            >> blockTag [ _val = _1 ]
            >> '>';

        blockEnd = lit('<')
            >> '/'
            >> lit(_r1)
            >> '>';

        blockElement = blockStart [ _a = _1 ]
            >> *( +(char_ - '<') | htmlBlock )
            >> blockEnd(_a);

        blockEmpty = lit('<')
            >> blockTag
            >> '/'
            >> '>';

        htmlBlock = blockEmpty | blockElement;
    }

    qi::rule<Iterator, qi::locals<std::string>, space_type > blockElement;
    qi::rule<Iterator, space_type> blockEmpty, htmlBlock;
    qi::rule<Iterator, void(std::string), space_type> blockEnd;
    qi::rule<Iterator, std::string(), space_type> blockTag, blockStart;
};

BOOST_AUTO_TEST_CASE(TestParsing)
{
    using boost::spirit::ascii::space;
    MiniHTML<std::string::const_iterator> parser;

    const std::string testString("< div >\n< h1 >foo< / h1 >\nHello < p 
>World< / p > < p / > </div>");
    std::string::const_iterator iter = testString.begin();
    BOOST_CHECK(qi::phrase_parse(iter, testString.end(), parser, space));
    BOOST_CHECK(iter == testString.end());
}

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
Joel de Guzman | 3 Sep 03:53
Picon
Favicon

Spirit Webmaster Volunteer?

Hi,

Spirit once had a terrific webmaster, Martijn van der Lee.
He has retired a long time ago. I'm the one maintaining the
site (http://spirit.sourceforge.net/) since then. Anyway,
now that Spirit2 is about to be released, at last(!), I'd
like to do a minor makeover. I can continue working on the
site, but, I have to ask: is there anyone out there interested
in being the Spirit Webmaster? At the very minimum, I just want
to have an easy to use CMS with web administration to make it,
well, painless to add content. Any taker?

Regards,
--

-- 
Joel de Guzman
http://www.boostpro.com
http://spirit.sf.net

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july

Gmane