rules related question

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

rules related question

by varnie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

good day, spirit developers. i have a simple question about rules.
suppose we have two rules:

qi::rule<Iterator, boost::shared_ptr<Foo>(), ascii::space_type> start;
qi::rule<Iterator, boost::shared_ptr<Foo>(boost::shared_ptr<Bar>, boost::shared_ptr<Fred>), ascii::space_type> test_rule;

why "start %= test_rule" will be wrong and how to write such thing correctly? thanks a lot.

Re: rules related question

by Hartmut Kaiser :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> good day, spirit developers. i have a simple question about rules.
> suppose we have two rules:
>
> qi::rule<Iterator, boost::shared_ptr<Foo>(), ascii::space_type> start;
> qi::rule<Iterator, boost::shared_ptr<Foo>(boost::shared_ptr<Bar>,
> boost::shared_ptr<Fred>), ascii::space_type> test_rule;
>
> why "start %= test_rule" will be wrong and how to write such thing
> correctly? thanks a lot.

A attribute of 'shared_ptr<Foo>(shared_ptr<Bar>,shared_ptr<Fred>)' tells me
that you need to invoke the rule 'test_rule' while supplying two inherited
attributes, you're not doing that in your example.

What do you want to achieve?

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: rules related question

by varnie :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

thanks for such quick response.
just for learning i want to parse simplified functions, such as: "void foo(int i)" without real body.
here is my rules:
//////////////////////
qi::rule<Iterator, shared_ptr<func_impl_treenode>(shared_ptr<func_head_treenode>, shared_ptr<func_body_treenode>), ascii::space_type> func_impl;
qi::rule<Iterator, std::vector<func_param_treenode>(), ascii::space_type> func_params_list;
qi::rule<Iterator, shared_ptr<func_head_treenode>(), spirit::locals<int, std::string, std::vector< func_param_treenode> >, ascii::space_type> func_head;
qi::rule<Iterator, shared_ptr<func_body_treenode>(), ascii::space_type> func_body;
qi::rule<Iterator, func_param_treenode(), ascii::space_type> func_param;              
qi::rule<Iterator, std::string(), ascii::space_type> ident;
qi::rule<Iterator, shared_ptr<func_impl_treenode>(), ascii::space_type> start;

//////////////////////
//here is the symbol tables used for keywords:

struct func_types_keywords : qi::symbols<char, int> {
    func_types_keywords() {
        add
        ("void", 1)
        ("string", 2)
        ("float", 3)
        ("int", 4)
        ("bool", 5)
        ;
    }
} func_types_keywords_;

struct var_types_keywords : qi::symbols<char, int> {
    var_types_keywords() {
        add
        ("string", 2)
        ("float", 3)
        ("int", 4)
        ("bool", 5)
        ;
    }
} var_types_keywords_;

//////////////////////
//assignments

ident = %raw[lexeme[((alpha | char_('_')) >> *(alnum | char_('_'))) - var_types_keywords_]];
func_param %= var_types_keywords >> ident;
func_params_list %= func_param % ',';
func_head =
        func_types_keywords_[_a = _1] >> ascii::space > ident[_b = _1] >>
        char_('(') >>
        func_params_list[_c = _1] >>
        char(')')
        [_val = construct<shared_ptr<func_head_node> >(new_<func_head_node>(_a, _b, _c))];
func_body = eps //suppose it is empty to simplify our example
        [_val = construct<shared_ptr<func_body_node> >(new_<func_body_node>())];
func_impl = (func_head >> func_body)
        [_val = construct<shared_ptr<func_impl_node> >(new_<func_impl_node>(_r1, _r2))]; //use inherited attributes
start %= *func_impl; //the problem is here

//////////////////////
//executing
std::string input;
//...

MyGrammar grammar;
std::vector<shared_ptr<func_impl_node> > parsed_funcs;

bool result = qi::phrase_parse(input.begin(), input.end(), grammar, ascii::space, parsed_funcs);
//////////////////////

guess my goal is pretty clear (even without the completely MyGrammar source and nodes classes used above). i can post them if there's a need.

thank you for your time. i appreciate it.