boost-spirit-x3

Statefulness of Spirit V2 and X3

大城市里の小女人 提交于 2019-12-13 08:11:18
问题 What is the intent of Spirit X3 being so much 'stateless'? Bad aspects of 'states' in Spirit V2 Looking back to Spirit V2, the "grammar" was, say, conceptually stateful - in many ways. This is because the grammar was a class instance. Basically, there are lots of bad aspects for making your grammar -- or even any single rule -- to be stateful: It might make your grammar non-re-entrant; It might add thread-unsafety to your grammar instance; Self-managed 'flag' is a disaster. Theoretically

how to change the parser of a rule

帅比萌擦擦* 提交于 2019-12-13 03:19:36
问题 Is it possible to modify the parser of a rule at runtime? I am able to dynamically create parsers (classes that are derived from parser_base), but I dont know how I can assign the new parser to an existing rule. Basically my problem is that I want to define a parser for a line that consists of elements like numbers (lets call them constants in the sense that they are invariant over all my parser input) and symbols (the variants that I want to capture with a dynamic parser approach). Since the

parsing identifiers except keywords

a 夏天 提交于 2019-12-12 20:15:19
问题 I am struggeling writing a identifier parser, which parses a alphanum string which is not a keyword. the keywords are all in a table: struct keywords_t : x3::symbols<x3::unused_type> { keywords_t() { add("for", x3::unused) ("in", x3::unused) ("while", x3::unused); } } const keywords; and the parser for a identifier should be this: auto const identifier_def = x3::lexeme[ (x3::alpha | '_') >> *(x3::alnum | '_') ]; now i try to combine these so an identifier parser fails on parsing a keyword. I

Parsing pair of strings fails. Bad spirit x3 grammar

泪湿孤枕 提交于 2019-12-12 17:19:35
问题 I would like to parse key-value pairs, mapping strings to strings. Since i want to support blocks of { ... } for right hand sides i came up with a simple grammar to start with #include <boost/spirit/home/x3.hpp> #include <boost/fusion/include/std_pair.hpp> namespace grammar::map { using namespace boost::spirit::x3; auto expression = rule<class expression, std::pair<std::string, std::string>>{"expression"}; auto lhs = *(~char_('=')); auto rest = *(~char_(';')); auto block = '{' >> *expression

Passing each element of a parsed sequence to a function that returns a rule's attribute type

牧云@^-^@ 提交于 2019-12-12 14:16:31
问题 I want to parse CSS color functions (for simplicity, all of the arguments are numbers between 0 and 255) rgb(r,g,b) rgba(r,g,b,a) hsl(h,s,l) hsla(h,s,l,a) into struct color { color(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a) : red{r}, green{g}, blue{b}, alpha{a} {} static color hsl(std::uint8_t h, std::uint8_t s, std::uint8_t l, std::uint8_t a) { ... } std::uint8_t red; std::uint8_t green; std::uint8_t blue; std::uint8_t alpha; } I have a working hsl function

Avoid throwing expectation_failure when expectation parser fails

隐身守侯 提交于 2019-12-12 13:19:21
问题 How to avoid throwing an exception, when expectation parser fails? I have a rule "function" > (!x3::lexeme[keyword >> !(x3::alnum | '_')] >> symbol) > ('(' > -lvalue_list > ')') > statements > "end" to parse code like: function a() return one end keyword s is ( zero , one , function , return , end etc). If I feed the parser with function one() return zero end code, then in function expect_directive::parse exception thrown from here: if (!r) { boost::throw_exception( expectation_failure

how to improve performance of boost::spirit::x3 key-value parser

情到浓时终转凉″ 提交于 2019-12-12 08:59:53
问题 I am parsing key value pairs (similar to HTTP headers) using boost::spirit::x3 . When comparing the performance to my handwritten parser, boost::spirit::x3 is around 10% slower than that. I am using boost 1.61 and GCC 6.1: $ g++ -std=c++14 -O3 -I/tmp/boost_1_61_0/boost/ main.cpp && ./a.out phrase_parse 1.97432 microseconds parseHeader 1.75742 microseconds How can I improve the performance of the boost::spirit::x3 based parser? #include <iostream> #include <string> #include <map> #include

Unhelpful compiler errors in x3 grammar

筅森魡賤 提交于 2019-12-11 05:08:14
问题 The following Spirit x3 grammar for a simple robot command language generates compiler errors in Windows Visual Studio 17. For this project, I am required to compile with the warning level to 4 (/W4) and treat warnings as errors (/WX). Warning C4127 conditional expression is constant SpiritTest e:\data\boost\boost_1_65_1\boost\spirit\home\x3\char\detail\cast_char.hpp 29 Error C2039 'insert': is not a member of 'boost::spirit::x3::unused_type' SpiritTest e:\data\boost\boost_1_65_1\boost\spirit

X3, what is attr_gen?

我与影子孤独终老i 提交于 2019-12-11 03:01:45
问题 I end up getting these move errors a lot and am not quite sure why other than having something to do with the way I'm parsing strings. Remove everything having to do with 'dummy' and the errors come back. Someone mentioned using attr_gen (couldn't find this in the docs) and by doing so, I can get past these "traits::move_to" compile errors, but the parser still fails. I've marked the lines that I've added to get it to compile, but don't think are necessary with "<---". #define BOOST_SPIRIT_X3

Stop X3 symbols from matching substrings

假装没事ソ 提交于 2019-12-10 22:19:36
问题 How does one prevent X3 symbol parsers from matching partial tokens? In the example below, I want to match "foo", but not "foobar". I tried throwing the symbol parser in a lexeme directive as one would for an identifier, but then nothing matches. Thanks for any insights! #include <string> #include <iostream> #include <iomanip> #include <boost/spirit/home/x3.hpp> int main() { boost::spirit::x3::symbols<int> sym; sym.add("foo", 1); for (std::string const input : { "foo", "foobar", "barfoo" }) {