boost::spirit::qi permutation parser and synthesized attributes(boost::spirit::qi 置换解析器和合成属性)
问题描述
我正在尝试将一个简单的命令行解析器与 SPIRIT 放在一起,而没有语义操作.我正在使用 BOOST 1.52,但我想避免使用 C++11 功能.语法具有以下语法:
I'm trying to put together a simple command line parser with SPIRIT without semantic actions. I'm using BOOST 1.52 but I would like to avoid using C++11 features. The grammar has the following syntax:
[-p num1] [-j] [--jobs num2] str1 str2
可选参数可以是任意顺序.我只成功解析了可选参数.一旦我添加了额外的强制性两个字符串解析器,它就会中断.即使我尝试明确写下rstart"属性并避免使用auto"进行类型推导,它也会中断.非常感谢任何帮助或建议!
Optional parameters can be in any order. I successfully parsed only optional parameters. Once I add the additional mandatory two string parsers it breaks. It breaks even when I try to write down the "rstart" attributes explicitly and avoid type deduction using "auto". Any help or suggestion is very appreciated!
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <boost/spirit/include/qi.hpp>
#include <boost/optional.hpp>
#include <boost/fusion/include/boost_tuple.hpp>
bool parse_line( const std::string&str,bool useStart1 )
{
bool rc=false;
namespace qi = boost::spirit::qi;
using boost::spirit::ascii::space_type;
using boost::spirit::ascii::space;
using boost::spirit::ascii::char_;
std::string::const_iterator iter( str.begin() );
std::size_t num1 =88;
bool bool1 =false;
std::size_t num2 =88;
std::string str1,str2;
qi::rule< std::string::const_iterator,std::string() > rstring=+~space;
qi::rule< std::string::const_iterator,std::size_t() ,space_type >
rOption1=qi::lit( "-p" ) >> qi::int_;
qi::rule< std::string::const_iterator,bool() ,space_type >
rOption2=qi::lit( "-j" ) >> qi::attr(true);
qi::rule< std::string::const_iterator,std::size_t() ,space_type >
rOption3=qi::lit( "--jobs" ) >> qi::int_;
#if defined(AAA)
qi::rule< std::string::const_iterator,
boost::spirit::ascii::space_type,
boost::tuple< boost::optional<std::size_t>,
boost::optional<bool>,
boost::optional<std::size_t >
>
>
#endif
auto rstart1 = ( rOption1 ^ rOption2 ^ rOption3 ) ;
auto rstart2 = ( rOption1 ^ rOption2 ^ rOption3 ) >> rstring >> rstring;
if( useStart1 )
qi::phrase_parse( iter,str.end(),
( qi::lit( "-p" ) >> qi::int_ ) ^
( qi::lit( "-j" ) >> qi::attr(true) ) ^
( qi::lit( "--jobs" ) >> qi::int_ ),space,num1,bool1,num2);
else
{
// qi::phrase_parse(
// iter,str.end(),rstart2,space,num1,bool1,num2,str1,str2);
}
if(iter==str.begin())
iter=str.begin(); //NOP
else
if(iter!=str.end())
std::cerr<<"syntax error: "<<std::string(iter,str.end())<<"!
";
else
rc=true;
std::cout << "num1:" << num1 << std::endl;
std::cout << "bool1:"<< bool1 << std::endl;
std::cout << "num2:" << num2 << std::endl;
std::cout << "str1:" << str1 << std::endl;
std::cout << "str2:" << str2 << std::endl;
return rc;
}
int main( int argc,char**argv )
{
std::vector< std::string > testData1;
testData1.push_back( "-p 100 -j" );
testData1.push_back( "-j -p 100 --jobs 16" );
testData1.push_back( "--jobs 16 -j -p 100" );
for( std::vector< std::string >::const_iterator it=testData1.begin();
it!=testData1.end(); ++it )
{
std::cout << "
parsing string:" << *it << std::endl;
parse_line( *it,true );
}
std::vector< std::string > testData2;
testData2.push_back( "-p 100 -j ifile ofile" );
testData2.push_back( "-j -p 100 --jobs 16 ifile ofile" );
testData2.push_back( "--jobs 16 -j -p 100 ifile ofile" );
for( std::vector< std::string >::const_iterator it=testData2.begin();
it!=testData2.end(); ++it )
{
std::cout << "
parsing string:" << *it << std::endl;
parse_line( *it,false );
}
return 0;
}
推荐答案
你面临的问题是你的组合规则的属性基本上是:
The problem you are facing is that the attribute of your combined rule is basically:
tuple< tuple<size_t,bool,size_t>, std::string, std::string >
并通过将您的变量一个一个地放在对短语解析的调用上,您基本上:
and by putting your variables one by one on the call to phrase_parse you have basically:
tuple< size_t, bool, size_t, std::string, std::string >
由于属性传播的工作方式,这就是正在发生的事情:
Because of the way attribute propagation works in spirit this is what is happening:
整个 tuple
被分配给你的 num1(忽略 bool 和第二个 size_t),在该精神尝试将第一个字符串分配给你的 bool 之后,导致你有错误.
the whole tuple<size_t,bool,size_t>
is assigned to your num1 (ignoring the bool and second size_t), after that spirit tries to assign the first string to your bool, resulting in the error you have.
我相信解决这个问题的最简洁的方法是创建一个自定义结构来保存反映规则结构的结果.
I believe the cleanest way to solve this is creating a custom struct to hold your result that reflects the structure of your rules.
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
struct optional_command_line_options
{
int num1;
bool bool1;
int num2;
};
struct command_line_options
{
optional_command_line_options opt;
std::string str1;
std::string str2;
};
BOOST_FUSION_ADAPT_STRUCT(
optional_command_line_options,
(int, num1)
(bool, bool1)
(int, num2)
)
BOOST_FUSION_ADAPT_STRUCT(
command_line_options,
(optional_command_line_options, opt)
(std::string, str1)
(std::string, str2)
)
bool parse_line( const std::string&str )
{
bool rc=false;
namespace qi = boost::spirit::qi;
using boost::spirit::ascii::space;
using boost::spirit::ascii::char_;
std::string::const_iterator iter( str.begin() );
command_line_options options;
options.opt.num1=88;
options.opt.bool1=false;
options.opt.num2=88;
qi::rule< std::string::const_iterator, std::string() > rstring=+~space;
qi::rule<std::string::const_iterator, boost::spirit::ascii::space_type,optional_command_line_options() > trule;
trule=
( qi::lit( "-p" ) >> qi::int_ ) ^
( qi::lit( "-j" ) >> qi::attr(true) ) ^
( qi::lit( "--jobs" ) >> qi::int_ )
;
qi::rule< std::string::const_iterator, boost::spirit::ascii::space_type, command_line_options() >arule;
arule = -trule >> rstring >> rstring;
bool result=qi::phrase_parse( iter,str.end(),
arule,
space,
options
);
if(result && iter==str.end())
{
std::cout << "Parse successful." << std::endl;
rc=true;
}
else
{
std::cerr<<"syntax error: "<<std::string(iter,str.end())<<"!
";
}
std::cout << std::boolalpha;
std::cout << "num1:" << options.opt.num1 << std::endl;
std::cout << "bool1:"<< options.opt.bool1 << std::endl;
std::cout << "num2:" << options.opt.num2 << std::endl;
std::cout << "str1:" << options.str1 << std::endl;
std::cout << "str2:" << options.str2 << std::endl;
return rc;
}
int main( int /*argc*/,char**/*argv*/ )
{
std::vector< std::string > testData;
testData.push_back( "-p 100 -j ifile ofile" );
testData.push_back( "-j -p 100 --jobs 16 ifile ofile" );
testData.push_back( "--jobs 16 -j -p 100 ifile ofile" );
testData.push_back( "--jobs 16 -p 100 ifile ofile" );
testData.push_back( "ifile ofile" );
for( std::vector< std::string >::const_iterator it=testData.begin();
it!=testData.end(); ++it )
{
std::cout << "
parsing string:" << *it << std::endl;
parse_line( *it );
}
return 0;
}
在 LWS 上运行.
PS:如果不使用 boost::proto::deep_copy,您不能直接分配给使用 auto
声明的规则,这些规则中嵌入了文字(例如字符串或数字);
PS: You can't assign directly to rules declared with auto
that have literals embedded in them (strings or numbers for example) without using boost::proto::deep_copy;
auto trule = boost::proto::deep_copy(qi::lit( "-p" ) >> qi::int_);
有一个叫做 BOOST_SPIRIT_AUTO 的宏可以让它更容易使用:
There is a macro called BOOST_SPIRIT_AUTO that makes it easier to use:
#define BOOST_SPIRIT_AUTO(domain_, name, expr)
typedef boost::proto::result_of::
deep_copy<BOOST_TYPEOF(expr)>::type name##_expr_type;
BOOST_SPIRIT_ASSERT_MATCH(
boost::spirit::domain_::domain, name##_expr_type);
BOOST_AUTO(name, boost::proto::deep_copy(expr));
BOOST_SPIRIT_AUTO(qi,trule,qi::lit( "-p" ) >> qi::int_);
这篇关于boost::spirit::qi 置换解析器和合成属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:boost::spirit::qi 置换解析器和合成属性
基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01