前几天我问过question
一个开放点是,不知道如何处理值(在我的例子中为-23.0)。字符串应解析为值(表示为字符串类型)而不是选项。
我现在尝试扩展提议的语法,但又没有成功。我也试图放宽我的要求,所以我认为用双短划线“ - ”定义一个参数是有效的。我们的想法是获得参数的唯一标识符。这是我目前的语法,但解析失败了,我不知道为什么:
//#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <map>
#include <string>
#include <vector>
// Structure stores the parsed command line information:
struct CmdData
{
typedef std::string Name;
typedef std::string ArgName;
typedef std::string Value;
typedef std::vector<Value> Values; // Type defines a list of values:
typedef std::map<ArgName, Values> Args; // Type defines a map storing the relation between a argument and the corresponding values:
Name cmd; // Stores the command name as a string.
Args arg; // Stores the arguments and the corresponding values as strings.
};
BOOST_FUSION_ADAPT_STRUCT(CmdData, (CmdData::Name, cmd)(CmdData::Args, arg))
namespace Grammar
{
namespace qi = boost::spirit::qi;
// This class implements the grammar used to parse a command line.
// The expected format is as follows:
// - command
// - command value0 ... valueN
// - command -arg0 ... -argN
// - command -arg0 value0 ... valueN ... -argN value0 ... valueN
template <typename It>
struct decode : qi::grammar<It, CmdData()>
{
decode() : decode::base_type(data)
{
using namespace qi;
token = +( ~char_( "\r\n -" ) );
values = +( ~char_( "--" ) >> +token );
//
entry = (lexeme[ "--" >> token ] >> -values | attr( "empty" ) >> values );
args = *entry;
//
data = skip(qi::blank) [ token >> args ];
BOOST_SPIRIT_DEBUG_NODES( (token)(values)(entry)(args)(data) )
}
private:
qi::rule<It, CmdData()> data;
// The following variables define the rules used within this grammar:
typedef std::pair<CmdData::ArgName, CmdData::Values> Entry;
qi::rule<It, CmdData::Values(), qi::blank_type> values;
qi::rule<It, Entry(), qi::blank_type> entry;
qi::rule<It, CmdData::Args(), qi::blank_type> args;
// lexemes
qi::rule<It, std::string()> token;
};
} // namespace
bool parse(const std::string& in)
{
CmdData data;
// Create an instance of the used grammar:
Grammar::decode<std::string::const_iterator> gr;
// Try to parse the data stored within the stream according the grammar and store the result in the tag variable:
bool b = boost::spirit::qi::parse(in.begin(), in.end(), gr, data);
std::cout << "Parsing: '" << in << "' ok: " << std::boolalpha << b << "\n";
if (b)
std::cout << "Entries parsed: " << data.arg.size() << "\n";
return b;
}
int main()
{
parse(" cmd0");
parse(" cmd0 value0 value1 value2 -23.0");
parse(" cmd0 -23.0 value0 value1 value2");
parse(" cmd0 --arg0 --arg1 123 --arg2 -23.0");
parse(" cmd0 --arg0 value0 --arg1 value0 value1 --arg2 value0 value1 value2");
}
答案 0 :(得分:2)
好的,我玩你的语法,我觉得我可以上班了。
让我做一个免责声明,我不是提升精神的专家,而且我只有中等水平的经验。
以下是我改变的事情:
我不知道~
运营商在精神上是什么,这里没有记录:http://www.boost.org/doc/libs/1_44_0/libs/spirit/doc/html/spirit/qi/reference/operator.html在我的版本中我将其删除了。
我认为你正在使用〜试图表达&#34;而不是这些字符&#34;。我这样做的方法通常是使用-
运算符。那就是我做了一个&#34;将军&#34;表达式然后使用-
从中排除内容。
我摆脱了所有的跳过语法,只添加了一个空白规则。只要空白规则没有属性,它就不会影响自动属性推断,它将具有qi::unused_type
。这可能不是必要的/最佳的,但我以这种方式做出有效的答案要快得多。
我认为我在你的语法中修复的两个主要问题是,使用~char_( "--" )
时你应该使用cv_and_he指出的- "--"
或- lit("--")
之类的内容注释,以及解析参数类"--" >> token
并且没有使用lit
的部分,它确实混淆了自动属性收集系统。
这是我最终的结果:
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <string>
#include <vector>
// Structure stores the parsed command line information:
struct CmdData
{
typedef std::string Name;
typedef std::string ArgName;
typedef std::string Value;
typedef std::vector<Value> Values; // Type defines a list of values:
typedef std::map<ArgName, Values> Args; // Type defines a map storing the relation between a argument and the corresponding values:
Name cmd; // Stores the command name as a string.
Args arg; // Stores the arguments and the corresponding values as strings.
};
BOOST_FUSION_ADAPT_STRUCT(CmdData, (CmdData::Name, cmd)(CmdData::Args, arg))
namespace Grammar
{
namespace qi = boost::spirit::qi;
// This class implements the grammar used to parse a command line.
// The expected format is as follows:
// - command
// - command value0 ... valueN
// - command -arg0 ... -argN
// - command -arg0 value0 ... valueN ... -argN value0 ... valueN
template <typename It>
struct decode : qi::grammar<It, CmdData()>
{
decode() : decode::base_type(data)
{
using namespace qi;
ws = char_("\r\n ");
token = +( char_ - ws - lit("--") );
values = token % (+ws);
//
arg_label = lit("--") >> token;
entry = arg_label >> -(+ws >> values);
args = entry % (+ws);
//
data = *ws >> token >> -(+ws >> args) >> *ws;
BOOST_SPIRIT_DEBUG_NODES( (token)(values)(entry)(args)(data) )
}
private:
qi::rule<It, CmdData()> data;
// The following variables define the rules used within this grammar:
typedef std::pair<CmdData::ArgName, CmdData::Values> Entry;
qi::rule<It, CmdData::Values()> values;
qi::rule<It, Entry()> entry;
qi::rule<It, CmdData::Args()> args;
// lexemes
qi::rule<It, std::string()> token;
qi::rule<It, std::string()> arg_label;
qi::rule<It> ws;
};
} // namespace
bool parse(const std::string& in)
{
CmdData data;
// Create an instance of the used grammar:
Grammar::decode<std::string::const_iterator> gr;
// Try to parse the data stored within the stream according the grammar and store the result in the tag variable:
bool b = boost::spirit::qi::parse(in.begin(), in.end(), gr, data);
std::cout << "Parsing: '" << in << "' ok: " << std::boolalpha << b << "\n";
if (b) {
std::cout << "Entries parsed: " << data.arg.size() << "\n";
for (const auto & p : data.arg) {
std::cout << " " << p.first;
bool first = true;
for (const auto & v : p.second) {
if (first) {
std::cout << " : ";
first = false;
} else {
std::cout << " , ";
}
std::cout << v;
}
std::cout << std::endl;
}
}
return b;
}
int main()
{
parse(" cmd0");
parse(" cmd0 value0 value1 value2 -23.0");
parse(" cmd0 -23.0 value0 value1 value2");
parse(" cmd0 --arg0 --arg1 123 --arg2 -23.0");
parse(" cmd0 --arg0 value0 --arg1 value0 value1 --arg2 value0 value1 value2");
}
使用gcc版本4.8.4编译。这是我的输出:
$ g++ -std=c++11 main.cpp -o main
$ ./main
Parsing: ' cmd0' ok: true
Entries parsed: 0
Parsing: ' cmd0 value0 value1 value2 -23.0' ok: true
Entries parsed: 0
Parsing: ' cmd0 -23.0 value0 value1 value2' ok: true
Entries parsed: 0
Parsing: ' cmd0 --arg0 --arg1 123 --arg2 -23.0' ok: true
Entries parsed: 3
arg0
arg1 : 123
arg2 : -23.0
Parsing: ' cmd0 --arg0 value0 --arg1 value0 value1 --arg2 value0 value1 value2' ok: true
Entries parsed: 3
arg0 : value0
arg1 : value0 , value1
arg2 : value0 , value1 , value2
编辑:
正如评论中指出的那样,我的第一个答案是不正确的,因为它没有处理&#34;空的&#34;参数类型。我现在看到第1部分的答案是正确地做了那一部分。在这个版本中,我修复了这个问题,并且我还修复了空格,以便处理得更干净/更像原始代码示例。
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <string>
#include <vector>
// Structure stores the parsed command line information:
struct CmdData
{
typedef std::string Name;
typedef std::string ArgName;
typedef std::string Value;
typedef std::vector<Value> Values; // Type defines a list of values:
typedef std::map<ArgName, Values> Args; // Type defines a map storing the relation between a argument and the corresponding values:
Name cmd; // Stores the command name as a string.
Args arg; // Stores the arguments and the corresponding values as strings.
};
BOOST_FUSION_ADAPT_STRUCT(CmdData, (CmdData::Name, cmd)(CmdData::Args, arg))
namespace Grammar
{
namespace qi = boost::spirit::qi;
// This class implements the grammar used to parse a command line.
// The expected format is as follows:
// - command
// - command value0 ... valueN
// - command -arg0 ... -argN
// - command -arg0 value0 ... valueN ... -argN value0 ... valueN
template <typename It>
struct decode : qi::grammar<It, CmdData()>
{
decode() : decode::base_type(data)
{
using namespace qi;
token = +( char_ - blank - lit("--") );
//
arg_label = lit("--") >> token;
entry = skip(blank) [
(arg_label >> *token) | ( attr("empty") >> +token)
];
args = *entry;
//
data = skip(blank) [ token >> args ];
BOOST_SPIRIT_DEBUG_NODES( (token)(entry)(args)(arg_label)(data) )
}
private:
qi::rule<It, CmdData()> data;
// The following variables define the rules used within this grammar:
typedef std::pair<CmdData::ArgName, CmdData::Values> Entry;
qi::rule<It, Entry()> entry;
qi::rule<It, CmdData::Args()> args;
// lexemes
qi::rule<It, std::string()> token;
qi::rule<It, std::string()> arg_label;
};
} // namespace
bool parse(const std::string& in)
{
CmdData data;
// Create an instance of the used grammar:
Grammar::decode<std::string::const_iterator> gr;
// Try to parse the data stored within the stream according the grammar and store the result in the tag variable:
bool b = boost::spirit::qi::parse(in.begin(), in.end(), gr, data);
std::cout << "Parsing: '" << in << "' ok: " << std::boolalpha << b << "\n";
if (b) {
std::cout << "Entries parsed: " << data.arg.size() << "\n";
for (const auto & p : data.arg) {
std::cout << " " << p.first;
bool first = true;
for (const auto & v : p.second) {
if (first) {
std::cout << " : ";
first = false;
} else {
std::cout << " , ";
}
std::cout << v;
}
std::cout << std::endl;
}
}
return b;
}
int main()
{
parse(" cmd0");
parse(" cmd0 value0 value1 value2 -23.0");
parse(" cmd0 -23.0 value0 value1 value2");
parse(" cmd0 --arg0 --arg1 123 --arg2 -23.0");
parse(" cmd0 --arg0 value0 --arg1 value0 value1 --arg2 value0 value1 value2");
}
我的输出现在是这样的:
$ ./main
Parsing: ' cmd0' ok: true
Entries parsed: 0
Parsing: ' cmd0 value0 value1 value2 -23.0' ok: true
Entries parsed: 1
empty : value0 , value1 , value2 , -23.0
Parsing: ' cmd0 -23.0 value0 value1 value2' ok: true
Entries parsed: 1
empty : -23.0 , value0 , value1 , value2
Parsing: ' cmd0 --arg0 --arg1 123 --arg2 -23.0' ok: true
Entries parsed: 3
arg0
arg1 : 123
arg2 : -23.0
Parsing: ' cmd0 --arg0 value0 --arg1 value0 value1 --arg2 value0 value1 value2' ok: true
Entries parsed: 3
arg0 : value0
arg1 : value0 , value1
arg2 : value0 , value1 , value2
我不得不在那个版本中稍微改变一下,因为我得到了*entry
然后attr("empty") >> *tokens
的无限循环。我认为这很可能是最简单的方法,让它仍然使用所有自动归因,但不确定。