忍者ブログ
ゲーム関連の話題に絞っていましたが、全然書かないのでいろいろ解禁してみたり。
[12]  [11]  [10]  [9]  [8]  [7]  [6]  [5]  [4]  [3]  [2
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

意外と進展があったのでいきなり続きなど。


何はともあれ、単一引数の関数だけじゃなくて複数の引数をとる関数をアクションに指定できないかな-……といったところが発端で、結構使えるようになってきました。

昨日の電卓から機能を削減したバージョンです。
とりあえず全ソースを。
 

#pragma warning(disable:4100) //引数は関数の本体部で 1 度も参照されません。
#pragma warning(disable:4512) //代入演算子を生成できません。

#include <iostream>
#include <string>
#include <stack>

#include <boost/bind.hpp>
#include <boost/spirit/home/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>


template<typename String>
struct Calc : boost::spirit::qi::grammar<typename String::const_iterator, boost::spirit::ascii::space_type>{
 typedef typename String::const_iterator Iterator;

 Calc() : Calc::base_type(line){

  using namespace boost::spirit;
  namespace sp_arg = boost::spirit::arg_names;

  // 構文定義
  factor  = int_[sp_arg::_val = sp_arg::_1];
  expression = (factor >> '+' >> factor)[sp_arg::_val = sp_arg::_1 + sp_arg::_2]
     | factor[sp_arg::_val = sp_arg::_1];
  line  = expression[boost::bind(&Calc::PUSH, this, _1)];
 };

 boost::spirit::qi::rule<Iterator, int(), boost::spirit::ascii::space_type> expression;
 boost::spirit::qi::rule<Iterator, int(), boost::spirit::ascii::space_type> factor;

 boost::spirit::qi::rule<Iterator, boost::spirit::ascii::space_type> line;

public:
 std::stack<int> stack;
 
 int GetResult(){
  int i = stack.top();
  Reset();
  return i;
 }

 void Reset(){
  std::stack<int> empty;
  std::swap(empty, stack);
 }

 void PUSH(int i){
  stack.push(i);
 }
};


int main(void)
{
 Calc<std::string> calc;

 std::string str;
 while (getline(std::cin, str))
 {
  if (str.empty() || str[0] == 'q' || str[0] == 'Q')
   break;

  //boost::algorithm::trim(str);
  std::string::const_iterator it = str.begin();
  std::string::const_iterator end = str.end();

  bool result = phrase_parse(it, end, calc, boost::spirit::ascii::space);

  if (result && it == end)
  {
   std::cout << "-------------------------\n";
   std::cout << "Parsing succeeded\n";
   std::cout << str << " Parses OK: " << std::endl;
   std::cout << calc.GetResult() << std::endl;
  }
  else if(result)
  {
   std::cout << "-------------------------\n";
   std::cout << "Parsing succeeded\n";
   std::cout << std::string(str.begin(), it) << " Parses OK: " << std::endl;
   std::cout << std::string(it, end) << " amari" << std::endl;
   std::cout << calc.GetResult() << std::endl;
  }
  else
  {
   std::cout << "-------------------------\n";
   std::cout << "Parsing failed\n";
   std::cout << "-------------------------\n";
   calc.Reset();
  }
 }

 return 0;
}

重要なのはここ。

expression = (factor >> '+' >> factor)[sp_arg::_val = sp_arg::_1 + sp_arg::_2]
     | factor[sp_arg::_val = sp_arg::_1];

1行目、factorが二つあるから2つ引数が使えるかなー……って適当に作ったらうまくいきました。
ただ、このままだと1+2+3……って感じの物がうまく解析できません。
ちなみに、

factor  = int_[sp_arg::_val = sp_arg::_1] | expression[sp_arg::_val = sp_arg::_1];

というようにしてもアウトでした。
よく考えれば1+2が構文解析された時点でexpression (factor+factor)が完成してしまってそのまま終了しちゃいますね。
さてさて、どうしましょう……ってところで次へ。

//2009-10-25追記
コードの修正(mutableとconstの勘違い)

PR
この記事にコメントする
お名前
タイトル
文字色
メールアドレス
URL
コメント
パスワード   Vodafone絵文字 i-mode絵文字 Ezweb絵文字
この記事へのトラックバック
この記事にトラックバックする:
Twitter
Twitter Update
ブログ内検索
カレンダー
05 2025/06 07
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
最新コメント
最新トラックバック
プロフィール
HN:
100poisha
性別:
非公開
アクセス解析
Copyright © 100poishaのブログ All Rights Reserved.
Designed by north sound
Powered by Ninja Blog

忍者ブログ [PR]