|
Boost : |
From: David B. Held (dheld_at_[hidden])
Date: 2004-05-15 05:52:23
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
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk