I wrote some grammar with boost::spirit::qi::rule to parse the internet packet. the grammar is something like:
qi::rule<Iterator> start, request, response, status, query ;
start = (request | response | status | query) >> lit("\r\n");
to improve the performance, user maybe want to skip some rules in the runtime, e.g. ignore "response","status","query" and only try to match request, so the rule will change to:
start = (request ) >> lit("\r\n");
is it possible to do that? e.g, is there a function like "disable()" to just disable the rule "response", "status" and "query"?
The most natural approach would be to just use a different parser for your more constrained occasions.
Also, if performance is so important that you can't even spare 3 or 4 extra character comparisons
That said, here are some options:
qi::lazy
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
int main()
{
typedef std::string::const_iterator It;
qi::rule<It>
request = "request",
response = "response",
status = "status",
query = "query",
//
allowed;
static qi::rule<It> const start = qi::lazy(phx::ref(allowed)) >> qi::lit("\r\n");
static const auto test = [](std::string const& input) { return qi::parse(begin(input), end(input), start); };
for (int i=0; i<10; ++i)
{
switch(rand()%3)
{
case 0: allowed = request; break;
case 1: allowed = request | response | query; break;
case 2: allowed = request | response | status | query; break;
}
std::cout << "status: " << test("status\r\n") << "\t"
<< "response: " << test("response\r\n") << "\t"
<< "request: " << test("request\r\n") << "\n";
}
}
Like Mike mentions, this is also used in the Nabialek trick, although qi::lazy
is the essential ingredient here.
This prints, e.g.:
status: 0 response: 1 request: 1
status: 0 response: 1 request: 1
status: 0 response: 0 request: 1
status: 0 response: 1 request: 1
status: 1 response: 1 request: 1
status: 0 response: 1 request: 1
status: 0 response: 1 request: 1
status: 0 response: 0 request: 1
status: 0 response: 0 request: 1
status: 0 response: 1 request: 1
qi::lazy
Pretty similar to the above, you could pass 'subrules' as inherited attributes. I'm not sure I'd recommend this, as I've seen Undefined Behaviour crop up in past examples, see e.g.
qi::lazy
itself doesn't support inherited attributesThis is most natural, in my opinion:
std::function<bool(string)> test;
switch(rand()%3)
{
case 0: test = [&](std::string const& input) { return qi::parse(begin(input), end(input), request); }; break;
case 1: test = [&](std::string const& input) { return qi::parse(begin(input), end(input), request | response | query); }; break;
case 2: test = [&](std::string const& input) { return qi::parse(begin(input), end(input), request | response | status | query); }; break;
}
See full sample: http://coliru.stacked-crooked.com/a/603f093add6b9799
Yes this is possible by using the qi::symbols
parser. One can change the used symbols at runtime, so you could alter the behaviour. To use this parser for complete rules there is a little trick, called the nabialek trick http://boost-spirit.com/home/articles/qi-example/nabialek-trick/.
Basically it shows how to hook complete rules in the symbols parser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With