提升精神:断言在Windows下失败但在Linux下失败。为什么?

时间:2011-11-30 10:25:27

标签: windows visual-c++ boost boost-spirit

[通过完整程序替换代码并更新版本]

以下代码在Windows下失败,并显示以下消息(抱歉,由于来自于 法语,我不知道如何强制Visual C ++用英语工作)。这是 在Windows下。它在Linux下运行。你可以编译 Linux:gcc LimaTokenizerParser.cpp -o LimaTokenizerPars -lboost_system-mt -lstdc++ Windows(失败):cl LimaTokenizerParser.cpp /Ic:\boost\path\include /EHsc

我在Windows和Boost 1.48下使用Boost 1.50和Visual C ++ 2010 和Linux下的gcc 4.6.3。

boost语法中的断言伴随着这个评论:

// If you see the assertion below failing then the start rule
// passed to the constructor of the grammar is not compatible with
// the grammar (i.e. it uses different template parameters).

但我不明白在我的情况下如何理解......

有什么想法吗?

此致

GAEL

代码:

LimaTokenizerParser.cpp

#include "SpiritTokenizerParser.hpp"

#include <iostream>
#include <fstream>

void readStream(std::istream &is,std::string &dest)
{
  while (is.good() && !is.eof())
  {
    const int bufferSize = 10240;
    char buffer[bufferSize];
    is.read(buffer,bufferSize);
    const int size = is.gcount();
    if (size>0)
      dest.insert(dest.end(),buffer,buffer+size);
  }
}

int main(int argc, char* argv[])
{
  if (argc != 2)
  {
    std::cerr << "Needs exactly one argument" << std::endl;
    return 1;
  }
  namespace qi = boost::spirit::qi;
  namespace ascii = boost::spirit::ascii;
  using ascii::space;
  typedef std::string::const_iterator iterator_type;
  typedef tokenizer<iterator_type> tokenizer;

  // @ERROR DOES NOT WORK ON WINDOWS PORT
 tokenizer tokenizer_parser; 
  std::string str;
  std::ifstream file(argv[1]);
  readStream(file, str);

  std::string::const_iterator iter = str.begin();
  std::string::const_iterator end = str.end();

  tokenizer_automaton automaton;
  bool r = false;
  // @ERROR DOES NOT WORK ON WINDOWS PORT
  r = phrase_parse(iter, end, tokenizer_parser, skipper, automaton);

  if (r && iter == end)
  {
      std::cout << "Parsing succeeded: "<<automaton.size()<<" states" << std::endl;
  }
  else
  {
      std::string rest(iter, end);
      std::cout << "Parsing failed. Stopped at: \": " << rest << "\"\n";
  }

  return 0;
}

SpiritTokenizerParser.hpp

#ifndef SPIRITTOKENIZERPARSER_HPP
#define SPIRITTOKENIZERPARSER_HPP

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>

#include <string>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

#define skipper qi::space | ascii::char_('#') >> *(ascii::char_ - qi::eol) >> qi::eol
typedef BOOST_TYPEOF(skipper) skipper_type;

enum transitions {
  STORE,
  FLUSH,
  TOKEN,
  EXIT,
  LTOKEN
};

typedef std::vector<std::string> tokenizer_precondition;
typedef std::vector<std::string> tokenizer_postcondition;
struct tokenizer_transition
{
  std::vector<tokenizer_precondition> preconditions;
  std::vector<std::string> event;
  std::vector<tokenizer_postcondition> postconditions;
  transitions transition;
  std::string target;
  std::vector<std::string> statuses;
};

struct tokenizer_state
{
  std::string id;
  std::vector<tokenizer_transition> transitions;
};

typedef std::vector<tokenizer_state> tokenizer_automaton;

BOOST_FUSION_ADAPT_STRUCT(
                          tokenizer_transition,
                          (std::vector<tokenizer_precondition>, preconditions)
                          (std::vector<std::string>, event)
                          (std::vector<tokenizer_postcondition>, postconditions)
                          (transitions, transition)
                          (std::string, target)
                          (std::vector<std::string>, statuses)
                          )

BOOST_FUSION_ADAPT_STRUCT(
                          tokenizer_state,
                          (std::string, id)
                          (std::vector<tokenizer_transition>, transitions)
                          )

using ascii::space_type;

template <typename Iterator>
struct tokenizer : qi::grammar<Iterator, tokenizer_automaton(), skipper_type>
{
  struct transitionsymbol_ : qi::symbols<char, unsigned>
  {
    // > is +1 store
    // / is +1 flush (forget the current token)
    // = is +1 token
    // ^ is exit

    transitionsymbol_()
    {
      add
      (">", STORE)
      ("/", FLUSH)
      ("=", TOKEN)
      ("^", EXIT)
      ;
    }

  } transitionsymbol;

  tokenizer() : tokenizer::base_type(start)
  {
    using qi::alnum;
    using qi::lexeme;
    using ascii::char_;

    start %= *state ;
    state %= '(' >> identifier >> ')' >> '{' >> *transition >> '}';
    transition %= '-' >> *precondition >> event >> *postcondition >> transitionsymbol >> identifier >> -('(' >> identifier % ',' >> ')');
    identifier %= lexeme[+(alnum | char_('_'))];
    precondition %= '[' >> (identifier % '|') >> ']';
    event %= identifier % '|';
    postcondition %= identifier % '|';
  }

  qi::rule<Iterator, tokenizer_automaton(), skipper_type> start;
  qi::rule<Iterator, tokenizer_state(), skipper_type> state;
  qi::rule<Iterator, tokenizer_transition(), skipper_type> transition;
  qi::rule<Iterator, std::string(), skipper_type> identifier;
  qi::rule<Iterator, std::vector<std::string>(), skipper_type> precondition;
  qi::rule<Iterator, std::vector<std::string>(), skipper_type> event;
  qi::rule<Iterator, std::vector<std::string>(), skipper_type> postcondition;
};

//]

#endif // SPIRITTOKENIZERPARSER_HPP

消息:

LimaTokenizerParser.cpp
c:\Program Files\boost\boost_1_50\include\boost/spirit/home/qi/nonterminal/grammar.hpp(77) : error C2664: 'boost::mpl::assertion_failed'<FF>: can not convert parameter 1 from 'boost::mpl::failed ************(__cdecl boost::spirit::qi::grammar<Iterator,T1,T2>::{ctor}::incompatible_start_rule::* ***********)(boost::spirit::qi::rule<Iterator,T1,boost::spirit::qi::alternative<Elements>>)' en 'boost::mpl::assert<false>::type'
        with
        [
            Iterator=iterator_type,
            T1=tokenizer_automaton (void),
            T2=skipper_type,
            Elements=boost::fusion::cons<boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space,boost::spirit::char_encoding::standard>>,boost::fusion::cons<boost::spirit::qi::sequence<boost::fusion::cons<boost::spirit::qi::literal_char<boost::spirit::char_encoding::ascii,false,false>,boost::fusion::cons<boost::spirit::qi::kleene<boost::spirit::qi::difference<boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::char_,boost::spirit::char_encoding::ascii>>,boost::spirit::qi::eol_parser>>,boost::fusion::cons<boost::spirit::qi::eol_parser,boost::fusion::nil>>>>,boost::fusion::nil>>
        ]
        No constructor could take the source type, or the constructor overload resolution was ambiguous
        e:\projets\amose\sources\lima_linguisticprocessing\src\linguisticprocessing\core\flattokenizer\SpiritTokenizerParser.hpp(89)<FF>: see reference to function template instantiation 'boost::spirit::qi::grammar<Iterator,T1,T2>::grammar<Iterator,tokenizer_automaton(void),boost::spirit::qi::alternative<Elements>,boost::spirit::unused_type,boost::spirit::unused_type>(const boost::spirit::qi::rule<Iterator,T1,boost::spirit::qi::alternative<Elements>> &,const std::string &)' en cours de compilation
        with
        [
            Iterator=iterator_type,
            T1=tokenizer_automaton (void),
            T2=skipper_type,
            Elements=boost::fusion::cons<boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space,boost::spirit::char_encoding::standard>>,boost::fusion::cons<boost::spirit::qi::sequence<boost::fusion::cons<boost::spirit::qi::literal_char<boost::spirit::char_encoding::ascii,false,false>,boost::fusion::cons<boost::spirit::qi::kleene<boost::spirit::qi::difference<boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::char_,boost::spirit::char_encoding::ascii>>,boost::spirit::qi::eol_parser>>,boost::fusion::cons<boost::spirit::qi::eol_parser,boost::fusion::nil>>>>,boost::fusion::nil>>
        ]

TokenizerParser.hpp(88)<FF>: during the compilation of the member function ':: tokenizer tokenizer <iterator> (void)' model class
        with
        [
            Iterator=iterator_type
        ]
        LimaTokenizerParser.cpp(33)<FF>: see reference to class template instantiation 'tokenizer <iterator>' being compiled
        with
        [
            Iterator=iterator_type
        ]

1 个答案:

答案 0 :(得分:3)

当张贴到精神列表时(是的,我看到这张贴在那里),最好提供一些最小的东西。制作最小的东西有很大帮助。它避免了在理解代码时往往会浪费时间的视觉混乱。无论如何,任何寻求支持的人都可以做到,这只是一个消除问题。附件是我的意思是最小的。如果我看到这样的东西,它只需要几秒钟而不是10分钟来理解这个问题。

无论如何,这里的代码已被提炼出来,尽管仍然存在问题但仍然是最小的:

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <string>

namespace qi = boost::spirit::qi;
typedef qi::space_type skipper_type;

template <typename Iterator>
struct tokenizer : qi::grammar<Iterator, skipper_type>
{
  tokenizer() : tokenizer::base_type(start)
  {
  }

  qi::rule<Iterator, skipper_type> start;
};



int main()
{
  typedef std::string::const_iterator iterator_type;
  typedef tokenizer<iterator_type> tokenizer;

  // @ERROR DOES NOT WORK ON WINDOWS PORT
  tokenizer tokenizer_parser;
  return 0;
}

我注意到如果我将“skipper_type”重命名为“skipper_type_”(注意尾随下划线)。然后编译好!这让我相信这是一个MSVC错误。

解决方案:只需更改名称skipper_type。