一部の文字列が引用符で囲まれていないか、引用符で囲まれているか、引用符で囲まれている文章を解析したいとします。以下のコードはほとんど動作しますが、引用符を閉じても一致しません。私はこれがqq参照のためだと推測しています。変更はコード内でコメントされ、変更は "quoted"または "quoted"でも解析され、元の問題は終了クオートに表示されます。このコードでは、正確な文法についても説明しています。引用符付きの文字列をboost :: spiritで構文解析します。
完全にクリアするには、引用符で囲まれていない文字列を解析します。 'hello'
のような引用符で囲まれた文字列は、開かれた引用文'
、すべての文字hello
を解析しますが、最後の引用文の解析に失敗します。'
boost tutorialsで始まるタグと終わりのタグが同じように試しましたが、成功しませんでした。ボーナス信用のため
template <typename Iterator>
struct test_parser : qi::grammar<Iterator, dectest::Test(), ascii::space_type>
{
test_parser()
:
test_parser::base_type(test, "test")
{
using qi::fail;
using qi::on_error;
using qi::lit;
using qi::lexeme;
using ascii::char_;
using qi::repeat;
using namespace qi::labels;
using boost::phoenix::construct;
using boost::phoenix::at_c;
using boost::phoenix::push_back;
using boost::phoenix::val;
using boost::phoenix::ref;
using qi::space;
char qq;
arrow = lit("->");
open_quote = (char_('\'') | char_('"')) [ref(qq) = _1]; // Remember what the opening quote was
close_quote = lit(val(qq)); // Close must match the open
// close_quote = (char_('\'') | char_('"')); // Enable this line to get code 'almost' working
quoted_string =
open_quote
>> +ascii::alnum
>> close_quote;
unquoted_string %= +ascii::alnum;
any_string %= (quoted_string | unquoted_string);
test =
unquoted_string [at_c<0>(_val) = _1]
> unquoted_string [at_c<1>(_val) = _1]
> repeat(1,3)[any_string] [at_c<2>(_val) = _1]
> arrow
> any_string [at_c<3>(_val) = _1]
;
// .. <snip>set rule names
on_error<fail>(/* <snip> */);
// debug rules
}
qi::rule<Iterator> arrow;
qi::rule<Iterator> open_quote;
qi::rule<Iterator> close_quote;
qi::rule<Iterator, std::string()> quoted_string;
qi::rule<Iterator, std::string()> unquoted_string;
qi::rule<Iterator, std::string()> any_string; // A quoted or unquoted string
qi::rule<Iterator, dectest::Test(), ascii::space_type> test;
};
// main()
// This example should fail at the very end
// (ie not parse "str3' because of the mismatched quote
// However, it fails to parse the closing quote of str1
typedef boost::tuple<string, string, vector<string>, string> DataT;
DataT data;
std::string str("addx001 add 'str1' \"str2\" -> \"str3'");
std::string::const_iterator iter = str.begin();
const std::string::const_iterator end = str.end();
bool r = phrase_parse(iter, end, grammar, boost::spirit::ascii::space, data);
:(上記の例ではchar qq
など)ローカル・データ・メンバーを避けるソリューションは好ましいであろうが、実用的な観点から、私は働くものを使用します!
、charが '構造体test_parser'が全く同じように失敗したのメンバ変数をqq''作ります。 – Zero
「同じ方法で」失敗しますか?あなたは私にこのことがどう失敗したかを教えていない(私はそれが 'qq'参照によるものであるとイメージすることができるが)。 –
@NicolBolasそれはコードのコメントでした - 私はそれ以来、質問を明確にしました、指摘してくれてありがとう。私もref(qq)は疑わしいですが、boost lambda&coの欠点は、伝統的な意味で踏み込めないほどデバッグが難しいことです! – Zero