我有幾個腳本語言的命令,所以我需要決議它們。在決議程序中,我想檢查語法是否正確以及命令的型別及其引數(每個腳本命令型別的引數數量是可變的,因此我使用 astd::vector<std::string>來存盤它們)。
我遇到了問題,因為在決議時,只有第一個字串包含在向量中,無論字串的實數是多少。
此外,qi::as_string為了編譯器作業,我必須在所有引數中使用規則。
我的專案的最小作業示例如下所示:
//#define BOOST_SPIRIT_DEBUG
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iomanip>
#include <iostream>
#include <sstream>
namespace qi = boost::spirit::qi;
enum class TYPE {
NONE,
CMD1,
CMD2,
FAIL
};
struct Command {
TYPE type = TYPE::NONE;
std::vector<std::string> args;
};
using Commands = std::vector<Command>;
BOOST_FUSION_ADAPT_STRUCT(Command, type, args)
template <typename It>
class Parser : public qi::grammar<It, Commands()>
{
private:
qi::rule<It, Command(), qi::blank_type> none, cmd1, cmd2, fail;
qi::rule<It, Commands()> start;
public:
Parser() : Parser::base_type(start)
{
using namespace qi;
none = omit[*blank] >> &(eol | eoi)
>> attr(TYPE::NONE)
>> attr(std::vector<std::string>{});
cmd1 = lit("CMD1") >> '('
>> attr(TYPE::CMD1)
>> as_string[lexeme[ ~char_(")\r\n")]] >> ')';
cmd2 = lit("CMD2") >> '('
>> attr(TYPE::CMD2)
>> as_string[lexeme[ ~char_(",)\r\n")]] >> ','
>> as_string[raw[double_]] >> ')';
fail = omit[*~char_("\r\n")] //
>> attr(TYPE::FAIL);
start = skip(blank)[(none | cmd1 | cmd2 | fail) % eol] > eoi;
}
};
Commands parse(std::string text)
{
std::istringstream in(std::move(text));
using It = boost::spirit::istream_iterator;
static const Parser<It> parser;
Commands commands;
It first(in >> std::noskipws), last;//No white space skipping
if (!qi::parse(first, last, parser, commands))
// throw std::runtime_error("command parse error")
;
return commands;
}
int main()
{
std::string test{
R"(CMD1(some ad hoc text)
CMD2(identity, 25.5))"};
try {
auto commands = parse(test);
std::cout << "elements: " << commands.size() << std::endl;
std::cout << "CMD1 args: " << commands[0].args.size() << std::endl;
std::cout << "CMD2 args: " << commands[1].args.size() << std::endl;// Error! Should be 2!!!!!
} catch (std::exception const& e) {
std::cout << e.what() << "\n";
}
}
此外,這里是編譯器資源管理器的鏈接:https : //godbolt.org/z/qM6KTcTTK
任何幫助解決這個問題?提前致謝
uj5u.com熱心網友回復:
啟用除錯顯示:https : //godbolt.org/z/o3nvjz9bG
對我來說不夠清楚。讓我們添加一個引數規則:
struct Command {
using Arg = std::string;
using Args = std::vector<Arg>;
enum TYPE { NONE, CMD1, CMD2, FAIL };
TYPE type = NONE;
Args args;
};
qi::rule<It, Command::Arg()> arg;
和
none = omit[*blank] >> &(eol | eoi)
>> attr(Command::NONE)
/*>> attr(Command::Args{})*/;
arg = raw[double_] | ~char_(",)\r\n");
cmd1 = lit("CMD1") >> attr(Command::CMD1) //
>> '(' >> arg >> ')';
cmd2 = lit("CMD2") >> attr(Command::CMD2) //
>> '(' >> arg >> ',' >> arg >> ')';
fail = omit[*~char_("\r\n")] //
>> attr(Command::FAIL);
現在我們可以看到 https://godbolt.org/z/3Kqr3K41v
<cmd2>
<try>CMD2(identity, 25.5)</try>
<arg>
<try>identity, 25.5)</try>
<success>, 25.5)</success>
<attributes>[[i, d, e, n, t, i, t, y]]</attributes>
</arg>
<arg>
<try>25.5)</try>
<success>)</success>
<attributes>[[2, 5, ., 5]]</attributes>
</arg>
<success></success>
<attributes>[[CMD2, [[i, d, e, n, t, i, t, y]]]]</attributes>
</cmd2>
顯然,兩個引數都被決議,但只分配了一個。可悲的事實是,您通過調整一個二元素結構并決議一個由 3 個元素組成的序列,正在積極地混淆規則。
你可以讓它作業,但你會得到幫助(例如,使用transform_attribute,attr_cast<>或單獨的規則):
arg = raw[double_] | ~char_(",)\r\n");
args = arg % ',';
cmd1 = lit("CMD1") >> attr(Command::CMD1) //
>> '(' >> arg >> ')';
cmd2 = lit("CMD2") >> attr(Command::CMD2) //
>> '(' >> args >> ')';
現在你得到:
<cmd2>
<try>CMD2(identity, 25.5)</try>
<args>
<try>identity, 25.5)</try>
<arg>
<try>identity, 25.5)</try>
<success>, 25.5)</success>
<attributes>[[i, d, e, n, t, i, t, y]]</attributes>
</arg>
<arg>
<try> 25.5)</try>
<success>)</success>
<attributes>[[ , 2, 5, ., 5]]</attributes>
</arg>
<success>)</success>
<attributes>[[[i, d, e, n, t, i, t, y], [ , 2, 5, ., 5]]]</attributes>
</args>
<success></success>
<attributes>[[CMD2, [[i, d, e, n, t, i, t, y], [ , 2, 5, ., 5]]]]</attributes>
</cmd2>
Now this hints at an obvious improvement: improve the grammar by simplifying:
none = omit[*blank] >> &(eol | eoi) >> attr(Command{Command::NONE, {}});
fail = omit[*~char_("\r\n")] >> attr(Command::FAIL);
arg = raw[double_] | ~char_(",)\r\n");
args = '(' >> arg % ',' >> ')';
cmd = no_case[type_] >> -args;
start = skip(blank)[(cmd|fail) % eol] > eoi;
Then add validation to the commands after the fact.
Demo
Live On Compiler Explorer
//#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <iomanip>
#include <iostream>
namespace qi = boost::spirit::qi;
struct Command {
using Arg = std::string;
using Args = std::vector<Arg>;
enum Type { NONE, CMD1, CMD2, FAIL };
Type type = NONE;
Args args;
friend std::ostream& operator<<(std::ostream& os, Type type) {
switch(type) {
case NONE: return os << "NONE";
case CMD1: return os << "CMD1";
case CMD2: return os << "CMD2";
case FAIL: return os << "FAIL";
default: return os << "???";
}
}
friend std::ostream& operator<<(std::ostream& os, Command const& cmd) {
os << cmd.type << "(";
auto sep = "";
for (auto& arg : cmd.args)
os << std::exchange(sep, ", ") << std::quoted(arg);
return os << ")";
}
};
using Commands = std::vector<Command>;
BOOST_FUSION_ADAPT_STRUCT(Command, type, args)
template <typename It> struct Parser : qi::grammar<It, Commands()> {
Parser() : Parser::base_type(start) {
using namespace qi;
none = omit[*blank] >> &(eol | eoi) >> attr(Command{Command::NONE, {}});
fail = omit[*~char_("\r\n")] >> attr(Command::FAIL);
arg = raw[double_] | ~char_(",)\r\n");
args = '(' >> arg % ',' >> ')';
cmd = no_case[type] >> -args;
start = skip(blank)[(cmd|none|fail) % eol] > eoi;
BOOST_SPIRIT_DEBUG_NODES((start)(fail)(none)(cmd)(arg)(args))
}
private:
struct type_sym : qi::symbols<char, Command::Type> {
type_sym() { this->add//
("cmd1", Command::CMD1)
("cmd2", Command::CMD2);
}
} type;
qi::rule<It, Command::Arg()> arg;
qi::rule<It, Command::Args()> args;
qi::rule<It, Command(), qi::blank_type> cmd, none, fail;
qi::rule<It, Commands()> start;
};
Commands parse(std::string const& text)
{
using It = std::string::const_iterator;
static const Parser<It> parser;
Commands commands;
It first = text.begin(), last = text.end();
if (!qi::parse(first, last, parser, commands))
throw std::runtime_error("command parse error");
return commands;
}
int main()
{
try {
for (auto& cmd : parse(R"(
CMD1(some ad hoc text)
this is a bogus line
cmd2(identity, 25.5))"))
std::cout << cmd << "\n";
} catch (std::exception const& e) {
std::cout << e.what() << "\n";
}
}
Prints
NONE()
CMD1("some ad hoc text")
FAIL()
CMD2("identity", " 25.5")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/337811.html
