我已经问过这个问题了。但由于没有答案,我现在再次询问完整的可编译源代码片段。
由于 boost::variant 移动语义的一些问题,此代码片段应该在没有 std=c++11 选项的情况下编译。只是 'g++ -Wall -pedantic'。
在此代码片段中,您将找到“//Comment here”行。您可以评论以下 block ,直到“//And here -----”。 如果这个 block 没有注释,这个程序的性能会很差。
所以只要我能看到瓶颈就是替代解析器。我需要的是一些关于改进/更改语法以 boost 解析性能的建议。谢谢。
代码:
#include <string>
#include <vector>
#include <iostream>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_statement.hpp>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/spirit/include/phoenix_function.hpp>
#include <boost/spirit/include/phoenix_fusion.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/variant.hpp>
#include <boost/variant/recursive_wrapper.hpp>
#include <boost/array.hpp>
#include <boost/variant/apply_visitor.hpp>
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phoenix = boost::phoenix;
namespace client {
typedef std::string::iterator input_iterator_type;
struct op_and {};
struct op_or {};
struct op_eq {};
struct op_neq {};
struct is_part_of {};
struct more {};
struct more_eq {};
struct less {};
struct less_eq {};
struct mask_match {};
struct mask_not_match {};
struct in {};
namespace type {
enum code_t {
string = 0,
boolean = 1,
number = 2,
none = 3,
datetime = 4,
unknown = 5
};
}
template <typename tag> struct binop;
struct fn_call;
struct none_type {~none_type(){}};
struct datetime {
datetime(int yyyy, int mm, int dd, int hh24, int mi, int ss, int mls)
: yy(yyyy), mm(mm), dd(dd), hh(hh24), mi(mi), ss(ss), ms(mls) {}
datetime()
: yy(0), mm(0), dd(0), hh(0), mi(0), ss(0), ms(0) {}
int yy; int mm; int dd;
int hh; int mi; int ss;
int ms;
};
typedef boost::variant<
boost::recursive_wrapper<binop<op_and> >,
boost::recursive_wrapper<binop<op_or> >,
boost::recursive_wrapper<binop<op_eq> >,
boost::recursive_wrapper<binop<op_neq> >,
boost::recursive_wrapper<binop<is_part_of> >,
boost::recursive_wrapper<binop<more> >,
boost::recursive_wrapper<binop<more_eq> >,
boost::recursive_wrapper<binop<less> >,
boost::recursive_wrapper<binop<less_eq> >,
boost::recursive_wrapper<binop<mask_match> >,
boost::recursive_wrapper<binop<mask_not_match> >,
boost::recursive_wrapper<binop<in> >
> generic_binop;
typedef boost::variant <
std::string, double, none_type, datetime,
boost::recursive_wrapper<generic_binop>,
boost::recursive_wrapper<fn_call>
> node_value_t;
struct g_node {
mutable type::code_t type_id;
mutable node_value_t value;
};
typedef node_value_t value_type;
template <typename tag> struct binop {
explicit binop(const g_node& l, const g_node& r)
: oper1(l), oper2(r) {}
g_node oper1, oper2;
};
typedef std::vector<g_node> node_vector;
struct fn_call {
explicit fn_call(){}
std::string name;
node_vector params;
};
}
BOOST_FUSION_ADAPT_STRUCT(
client::g_node,
(client::type::code_t, type_id)
(client::node_value_t, value)
)
BOOST_FUSION_ADAPT_STRUCT(
client::fn_call,
(std::string, name)
(std::vector<client::g_node>, params)
)
namespace client {
template <typename Iterator> struct g_error_handler {
template<typename, typename, typename, typename>
struct result { typedef void type; };
void operator()(Iterator first, Iterator last,
Iterator err_pos, boost::spirit::info const& what) const {
std::cout << "Syntax error. Expected: " << what << " at: " <<
std::distance(first, err_pos) << std::endl;
}
};
template<typename Iterator, typename ErrorHandler = g_error_handler<Iterator> >
struct g_parser : qi::grammar<Iterator, g_node(), ascii::space_type> {
g_parser() : g_parser::base_type(or_, "G"), error_handler(ErrorHandler()) {
using phoenix::at_c;
or_ = (and_ >> "||" >> or_)[
at_c<0>(qi::_val) = type::unknown,
at_c<1>(qi::_val) = phoenix::construct<binop<op_or> >(qi::_1, qi::_2)] |
and_[qi::_val = qi::_1];
and_ = (bool_op >> "&&" >> and_)[
at_c<0>(qi::_val) = type::unknown,
at_c<1>(qi::_val) = phoenix::construct<binop<op_and> >(qi::_1, qi::_2)] |
bool_op[qi::_val = qi::_1];
bool_op =
(prim >> "=" >> bool_op)[
at_c<0>(qi::_val) = type::unknown,
at_c<1>(qi::_val) = phoenix::construct<binop<op_eq> >(qi::_1, qi::_2)] |
(prim >> "<>" >> bool_op)[
at_c<0>(qi::_val) = type::unknown,
at_c<1>(qi::_val) = phoenix::construct<binop<op_neq> >(qi::_1, qi::_2)] |
// Comment here ---------------------------------------------------
(prim >> ":" >> bool_op)[
at_c<0>(qi::_val) = type::unknown,
at_c<1>(qi::_val) = phoenix::construct<binop<is_part_of> >(qi::_1, qi::_2)] |
(prim >> ">" >> bool_op)[
at_c<0>(qi::_val) = type::unknown,
at_c<1>(qi::_val) = phoenix::construct<binop<more> >(qi::_1, qi::_2)] |
(prim >> ">=" >> bool_op)[
at_c<0>(qi::_val) = type::unknown,
at_c<1>(qi::_val) = phoenix::construct<binop<more_eq> >(qi::_1, qi::_2)] |
(prim >> "<" >> bool_op)[
at_c<0>(qi::_val) = type::unknown,
at_c<1>(qi::_val) = phoenix::construct<binop<less> >(qi::_1, qi::_2)] |
(prim >> "<=" >> bool_op)[
at_c<0>(qi::_val) = type::unknown,
at_c<1>(qi::_val) = phoenix::construct<binop<less_eq> >(qi::_1, qi::_2)] |
(prim >> "=~" >> bool_op)[
at_c<0>(qi::_val) = type::unknown,
at_c<1>(qi::_val) = phoenix::construct<binop<mask_match> >(qi::_1, qi::_2)] |
(prim >> "!~" >> bool_op)[
at_c<0>(qi::_val) = type::unknown,
at_c<1>(qi::_val) = phoenix::construct<binop<mask_not_match> >(qi::_1, qi::_2)] |
(prim >> "in" >> bool_op)[
at_c<0>(qi::_val) = type::unknown,
at_c<1>(qi::_val) = phoenix::construct<binop<in> >(qi::_1, qi::_2)] |
// And here------------------------------------------------------------
prim[qi::_val = qi::_1];
prim =
string_val [qi::_val = qi::_1] |
number [qi::_val = qi::_1] |
func_call [at_c<0>(qi::_val) = type::unknown, at_c<1>(qi::_val) = qi::_1] |
'(' > or_ [qi::_val = qi::_1] > ')';
quoted_string %= "'" > qi::lexeme[*(qi::char_ - "'")] > "'";
func_call = fn_name > '(' > -(or_ % ',') > ')';
fn_name %= +(qi::alpha) > -(qi::char_('-')) > *(qi::alnum);
string_val = quoted_string[
at_c<0>(qi::_val) = type::string, at_c<1>(qi::_val) = qi::_1];
number %= qi::attr(type::number) >> qi::double_;
or_.name ("OR expression" );
and_.name ("AND expression" );
bool_op.name ("BOOL expression");
prim.name ("Atom expression");
quoted_string.name ("quoted string" );
fn_name.name ("function name" );
number.name ("number" );
qi::on_error<qi::fail>(or_, error_handler(qi::_1, qi::_2, qi::_3, qi::_4));
}
qi::rule<Iterator, g_node(), ascii::space_type>
and_, bool_op, prim, or_, string_val, number;
qi::rule<Iterator, fn_call(), ascii::space_type> func_call;
qi::rule<Iterator, std::string(), ascii::space_type> quoted_string, fn_name;
boost::phoenix::function<ErrorHandler> error_handler;
};
typedef g_parser<input_iterator_type> grammar;
}
int main() {
std::string s = "((foo(bar('','')) = foo('')) || ('a' = 'gg')) && (3 <> 7) && (foo('','') = bar('','')) && 2=4 && 'a' ='b' && foo('') <> foo('')";
client::grammar g;
client::g_node ast;
std::string::iterator begin = s.begin();
std::string::iterator end = s.end();
bool success = qi::phrase_parse(begin, end, g,
boost::spirit::ascii::space, ast) && begin == end;
std::stringstream ss;
if(!success)
std::cout << "Syntax error at: " << std::distance(s.begin(), begin);
else std::cout << "Syntax is Ok\n";
}
最佳答案
您可以重构语法以减少回溯。我之前在 SO 上做过这样一个重构的例子。
链接:未找到
但是,这是要点。以下应该是等效的,除了大大减少了回溯的需要:
using boost::phoenix::construct;
using qi::_val;
using qi::_1;
bool_op =
prim [ _val = _1 ] >> -((
("=" >> bool_op [ at_c<1>(_val) = construct<binop<op_eq> > (_val, _1) ]) |
("<>" >> bool_op [ at_c<1>(_val) = construct<binop<op_neq> > (_val, _1) ]) |
(":" >> bool_op [ at_c<1>(_val) = construct<binop<is_part_of> > (_val, _1) ]) |
(">" >> bool_op [ at_c<1>(_val) = construct<binop<more> > (_val, _1) ]) |
(">=" >> bool_op [ at_c<1>(_val) = construct<binop<more_eq> > (_val, _1) ]) |
("<" >> bool_op [ at_c<1>(_val) = construct<binop<less> > (_val, _1) ]) |
("<=" >> bool_op [ at_c<1>(_val) = construct<binop<less_eq> > (_val, _1) ]) |
("=~" >> bool_op [ at_c<1>(_val) = construct<binop<mask_match> > (_val, _1) ]) |
("!~" >> bool_op [ at_c<1>(_val) = construct<binop<mask_not_match> > (_val, _1) ]) |
("in" >> bool_op [ at_c<1>(_val) = construct<binop<in> > (_val, _1) ])
) >> qi::eps [ at_c<0>(_val) = type::unknown ])
;
另请注意我倾向于避免重复代码,并展示代码。即使您的编码标准规定了最大行长,在对齐的列中布局代码也明显更易读、更易于维护并且大大更不容易出错,像我一样。事实上,您可以争辩说这段代码是“元数据”,如果您愿意的话,可以是一个表格,并且您可以做出合理的异常(exception)。
我认为我看到了许多其他“代码味道”——或者更积极地说,“简化的机会”。
事实上,我过去曾在 SO 上重构过许多这样的语法,通常将代码减少到原始代码的 <>
关于c++ - 使用替代解析器 boost spirit 不佳的性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23242408/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h