2

私はブースト スピリットを使用して数式を解析し、次のコードに抽出した問題に遭遇しました。

一致した文字列を保持する属性を持つ、1 つのトークンを持つ単純なレクサーがあります。パーサーは、トークンの属性を取得し、それを使用して関数を呼び出すための単一のルールを定義します。関数呼び出しの結果は、ルールの属性値である必要があります。

これはコンパイルに失敗します (calc_something: パラメーター 1 を const boost::spirit::_1_type から const std::string & に変換できません) - 明らかに qi::_1 の型が正しく推論されないためです。ただし、アクションを単純な「cout << qi::_1」に変更すると機能します。

私は精神を高めるのはかなり新しいですが、文法を正しく動作させることができました. 解析された値を取得する必要があるので、ここで立ち往生しており、私が得ることができる助けをいただければ幸いです。

// spiritTest.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <tchar.h>

#include <string>
#include <iostream>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>

namespace qi = boost::spirit::qi;
namespace lex = boost::spirit::lex;
namespace phoenix = boost::phoenix;

template <typename Lexer>
class TestLexer : public lex::lexer<Lexer>
{
public:
    TestLexer()
    {
        number = "(\\d*\\.)?\\d+([eE][-+]?\\d+)?";      
        self = number;
    }

    lex::token_def<std::string> number;
};

int calc_something(const std::string & s)
{
    return 5;
}

template <typename Iterator>
class Parser : public qi::grammar<Iterator, int>
{
public:
    template <typename TokenDef>
    Parser(const TokenDef& tok) : Parser::base_type(value)
    {   
        // the following line causes error C2664: 'calc_something' : cannot convert parameter 1 from 'const boost::spirit::_1_type' to 'const std::string &'    
        value = tok.number [qi::_val = calc_something(qi::_1)];         

        // the following line works as expected
        //value = tok.number [std::cout << qi::_1 << std::endl];            
    }

    qi::rule<Iterator, int> value;
};

int _tmain(int argc, _TCHAR* argv[])
{
    typedef const char* base_iterator_type;
    typedef lex::lexertl::token<base_iterator_type> token_type;
    typedef lex::lexertl::lexer<token_type> lexer_type;
    typedef TestLexer<lexer_type> TestLexer;
    typedef TestLexer::iterator_type iterator_type;
    typedef Parser<iterator_type> Parser;

    TestLexer lexer;
    Parser parser(lexer);

    const char * formula = "530";
    bool result = lex::tokenize_and_parse(formula, formula + strlen(formula), lexer, parser);

    return 0;
}
4

2 に答える 2

2

の経験はありませんがspirit lex、に似ているqiと思いますので、 a を使用しphoenix functionてそれを行う必要があります。

#include <boost/spirit/include/phoenix_function.hpp>

struct calc_something_impl
{
  template <typename T1>
  struct result { typedef int type; };

  int operator()(const std::string & s) const
  {
    return 5;
  }
};

boost::phoenix::function<calc_something_impl> calc_something;
于 2012-03-15T15:25:11.810 に答える