[Spirit] Grammar as parser problem

In the program below, I am having trouble using a grammar as a parser for a rule. In particular, I get an error with gcc 3.3 on the line: Module_ = *Statement; The code below is fully self-contained. Dave //-------------------------------------------------------------------------- - #include <stdexcept> #include <string> #include <iostream> #include <boost/spirit/core.hpp> #include <boost/spirit/iterator/file_iterator.hpp> #include <boost/spirit/attribute.hpp> #include <boost/spirit/tree/ast.hpp> //-------------------------------------------------------------------------- - using namespace boost::spirit; using namespace phoenix; //-------------------------------------------------------------------------- - typedef file_iterator<char> TFileIterator; typedef node_val_data<TFileIterator>::const_iterator_t TNodeIterator; typedef tree_match<TFileIterator>::node_t node_t; typedef tree_match<TFileIterator>::parse_node_t parse_node_t; typedef tree_match<TFileIterator>::container_t trees_t; //-------------------------------------------------------------------------- - enum TRuleID { idModuleTag, idDeclarationTag, idIdentifierTag }; //-------------------------------------------------------------------------- - struct Statement : public grammar<Statement> { template <typename ScannerT> struct definition { public: // Types template <TRuleID ID> struct TRule { typedef rule<ScannerT, parser_context, parser_tag<ID> > type; }; public: // Definition definition(Statement const&) { Declaration_ = root_node_d[Identifier_] >> Identifier_ >> discard_node_d[ch_p(';')]; Identifier_ = lexeme_d[(alpha_p | '_') >> *(alnum_p | '_')]; } typename TRule<idDeclarationTag>::type const& start(void) const { return Declaration_; } public: // Rules typename TRule<idDeclarationTag>::type Declaration_; typename TRule<idIdentifierTag>::type Identifier_; }; }; //-------------------------------------------------------------------------- - struct Module : public grammar<Module> { template <typename ScannerT> struct definition { public: // Types template <TRuleID ID> struct TRule { typedef rule<ScannerT, parser_context, parser_tag<ID> > type; }; public: // Definition definition(Module const&) { Module_ = *Statement; } public: // Rules typename TRule<idModuleTag>::type Module_; } }; //-------------------------------------------------------------------------- - void Generate(std::string const& File); //-------------------------------------------------------------------------- - int main(int argc, char* argv[]) { if (argc > 1) { Generate(argv[1]); } else { std::cout << "No source file specified." << std::endl; } return 0; } //-------------------------------------------------------------------------- - tree_parse_info<TFileIterator> Parse(std::string const& File) { TFileIterator Begin(File.c_str()); TFileIterator End = Begin.make_end(); static Statement Parser; return ast_parse(Begin, End, Parser, space_p); } //-------------------------------------------------------------------------- - void Generate(std::string const& File) { tree_parse_info<TFileIterator> Info = Parse(File); if (!Info.full) { std::cerr << "Parsing failed\n"; std::cerr << "stopped at: \"" << *Info.stop << "\"\n"; return; } node_t const& node = Info.trees[0]; try { std::cout << std::string(node.value.begin(), node.value.end()) << std::endl; } catch (std::runtime_error const& e) { std::cerr << e.what() << std::endl; } } //-------------------------------------------------------------------------- - --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.683 / Virus Database: 445 - Release Date: 5/12/2004

"David B. Held" <dheld@codelogicconsulting.com> wrote in message news:c84snq$u3k$1@sea.gmane.org...
Ok, I realized that Statement is a type, and you can't assign types to variables. Duh. But now my problem is that when I make it into a temporary, like so: Module_ = *Statement(); my grammar will parse the first instance, but then quit. For instance, I want to be able to parse this: a b; c d; e f; It will parse "a b;" and then stop on c. Any clues? Dave --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.683 / Virus Database: 445 - Release Date: 5/12/2004

Ok, I'm an idiot again. It would help if I actually used the correct grammar for the parse. But it does bring up an interesting question anyway. Is it best to always use a temporary when using a grammar in another rule, or should there be just one instance? And how does that interact with BOOST_SPIRIT_SINGLE_GRAMMAR_INSTANCE Dave P.S. BTW, did I mention how cool Spirit is? ;> --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.683 / Virus Database: 445 - Release Date: 5/12/2004
participants (1)
-
David B. Held