Spirit.Qi : forfait !
Ok, nothing to do, I'm not able to write a working parser with Boost.Spirit!! I attach to this mail the original flex/bison based parser, that I wrote some years ago, and the one developed with Spirit. It seems that parsing doesn't start () but I don't understand why that happens, I've tryied several ways without success. Help me, please. I'm going crazy!! It's so simple, and so difficult at the same time... :-( Thanks in advance to all! Michele
Michael,
Ok, nothing to do, I'm not able to write a working parser with Boost.Spirit!! I attach to this mail the original flex/bison based parser, that I wrote some years ago, and the one developed with Spirit. It seems that parsing doesn't start () but I don't understand why that happens, I've tryied several ways without success. Help me, please. I'm going crazy!! It's so simple, and so difficult at the same time... :-(
I really would like to help but I can't compile your example (parser.h and ../exception/sapecngexception.h is missing). And even if I could, it is always the best if you try to minimize the code you send to expose exactly the problem you're having. It's very difficult to understand such complex programs as yours without investing a significant amount of time. Regards Hartmut --------------- Meet me at BoostCon www.boostcon.com
Il 16/04/2010 17:13, Hartmut Kaiser ha scritto:
Michael,
I really would like to help but I can't compile your example (parser.h and ../exception/sapecngexception.h is missing). And even if I could, it is always the best if you try to minimize the code you send to expose exactly the problem you're having. It's very difficult to understand such complex programs as yours without investing a significant amount of time.
Regards Hartmut
Hi dear,
thank you very much.
Parser is part of a more complex software so it is not so easy to
extract it from the software. However, if you can, I'd like to
understand with your help steps to do.
Suppose our grammatic, like this, very simple (I put into quotes literal
component, like ".OUT", ok?):
out := ".OUT" integer
end := ".END"
cmp := "R"(char)* integer integer double bool
cmp := "L"(char)* integer integer double bool
comment := "#"(char*) // it goes to the end of line
start := (comment)*
(cmp)+
out
end
That's all, if I'm not clear (this is not BNF, and this is not Spirit
like, I know!), please ask me.
Ok, go on.
I'd like to fill structs like this (adapted by BOOST_FUSION_ADAPT_STRUCT
macro, quite simple to do):
struct cmp_s {
int id;
std::string name;
int va, vb;
double val;
bool sym;
};
struct main_s {
std::vectorstd::string comments;
std::vector
On Fri, Apr 16, 2010 at 10:07 AM, Michele Caini
Il 16/04/2010 17:13, Hartmut Kaiser ha scritto:
Michael,
I really would like to help but I can't compile your example (parser.h and ../exception/sapecngexception.h is missing). And even if I could, it is always the best if you try to minimize the code you send to expose exactly the problem you're having. It's very difficult to understand such complex programs as yours without investing a significant amount of time.
Regards Hartmut
Hi dear, thank you very much. Parser is part of a more complex software so it is not so easy to extract it from the software. However, if you can, I'd like to understand with your help steps to do.
Suppose our grammatic, like this, very simple (I put into quotes literal component, like ".OUT", ok?):
out := ".OUT" integer end := ".END"
cmp := "R"(char)* integer integer double bool cmp := "L"(char)* integer integer double bool
comment := "#"(char*) // it goes to the end of line
start := (comment)* (cmp)+ out end
That's all, if I'm not clear (this is not BNF, and this is not Spirit like, I know!), please ask me.
Ok, go on. I'd like to fill structs like this (adapted by BOOST_FUSION_ADAPT_STRUCT macro, quite simple to do):
struct cmp_s { int id; std::string name; int va, vb; double val; bool sym; };
struct main_s { std::vectorstd::string comments; std::vector
cmps; int out; }; Suppose id comes from that symbols table (real table is quite more complex but this is a foo-example, is it?):
struct table_ : boost::spirit::qi::symbols< char, int > { table_() { add ("R", 1) ("L", 2) ; } };
That's all. Follows my grammar:
template < typename It > struct parser : boost::spirit::qi::grammar < It, main_c(), boost::spirit::qi::ascii::space_type > {
typedef boost::spirit::qi::ascii::space_type space_type;
parser(std::ostream& err): parser::base_type(start, "start") { using namespace boost::spirit; using namespace boost::phoenix;
comment %= qi::lexeme['#' >> +(ascii::char_)];
selm = qi::char_("RL") [at_c<0>(_val) = (table_() << _1), at_c<1>(_val) = _1] >> *qi::char_("_a-zA-Z0-9") [at_c<1>(_val) += _1] >> qi::int_ [at_c<2>(_val) = _1] >> qi::int_ [at_c<3>(_val) = _1] >> qi::double_ [at_c<4>(_val) = _1] >> qi::bool_ [at_c<5>(_val) = _1] ;
start = eps [at_c<4>(_val) = 0] >> *(comment) [push_back(at_c<0>(_val), _1)] >> +(selm) [push_back(at_c<1>(_val), _1)] >> ".OUT" >> qi::int_ [at_c<4>(_val) = _1] >> ".END"; ;
comment.name("comment"); selm.name("selm"); start.name("start");
qi::on_errorqi::fail (start, err << val("Syntax error. Expecting ") << _4 << val(" here: \"") << constructstd::string(_3, _2) << val("\"") << std::endl ); }
boost::spirit::qi::rule
comment; boost::spirit::qi::rule selm; boost::spirit::qi::rule start; };
A more complex example on this idea compiles fine, but invoking it with something like:
# Comment 1 R1 1 2 2.5 0 L32 4 5 0.003 1 .OUT 3 .END
Returns from phrase_parse with a false value. I don't understand why that! I'm sure to be in error (of course, it doesn't work as I want, so something is wrong!!) but really since two days ago I look for the error whitout find it.
Something to suggest?
Have you turned on the Spirit debugging support? The documentation and tests show how to use it and it can show you exactly how it is working.
Parser is part of a more complex software so it is not so easy to extract it from the software.
Usually a parser uses structs definition only. If it's too much "blended" with the other parts of your program, then maybe it's worth improving your code structure anyway. <...>
A more complex example on this idea compiles fine, but invoking it with something like:
# Comment 1 R1 1 2 2.5 0 L32 4 5 0.003 1 .OUT 3 .END
Returns from phrase_parse with a false value. I don't understand why that!
Is it "something like" or it's the exact input? It looks like you try to parse "1"/"0" with qi::bool_. This is incorrect, because bool_ expects "true"/"false", so use qi::int_ instead.
participants (4)
-
Hartmut Kaiser
-
Igor R
-
Michele Caini
-
OvermindDL1