boost-spirit

Expression grammar with exponentiation operator using Boost Spirit

戏子无情 提交于 2019-12-23 12:22:05
问题 I would like to add the exponentiation operator to the expression grammar provided in the Boost spirit samples. The BNF grammar is the following: (see this answer for example: "Unambiguous grammar for exponentiation operation" ) E -> E + T | E - T | T T -> T * F | T / F | X X -> X ^ Y | Y Y -> i | (E) which I translated to Boost spirit like this: template <typename Iterator> struct calculator : qi::grammar<Iterator, ascii::space_type> { calculator() : calculator::base_type(expression) { qi:

boost-spirit parser lex->qi : Getting the “undocumented” on_success mechanism to work

我们两清 提交于 2019-12-23 10:16:13
问题 edit : I have ripped out the lexer as it does not cleanly integrate with Qi and just obfuscates grammars (see here). on_success isn't well documented and I am trying to wire it up to my parser. The examples dealing with on_success deal with parsers just built on qi --i.e., no lex . This is how I am trying to introduce the construct : using namespace qi::labels; qi::on_success(event_entry_,std::cout << _val << _1); But it won't compile. I am dreading the problem being lex . Could someone tell

Searching/Iterating boost::spirit::qi::symbols

旧城冷巷雨未停 提交于 2019-12-23 09:52:10
问题 If I have a symbol table: struct MySymbols : symbols<char, MyEnum::Fruits> { MySymbols () : symbols<char, MyEnum::Fruits>(std::string("MySymbols")) { add("apple", MyEnum::Apple) ("orange", MyEnum::Orange); } }; I want to iterate over the table in order to search for a symbol by data value. I cannot use lambda expressions so I implemented a simple class: template<typename T> struct SymbolSearcher { SymbolSearcher::SymbolSearcher(T searchFor) : _sought(searchFor) { // do nothing } void operator

Spirit X3, semantic action makes compilation fails with: Attribute does not have the expected size

喜夏-厌秋 提交于 2019-12-23 07:47:35
问题 This code does not compiles (gcc 5.3.1 + boost 1.60): #include <boost/spirit/home/x3.hpp> namespace x3 = boost::spirit::x3; template <typename T> void parse(T begin, T end) { auto dest = x3::lit('[') >> x3::int_ >> ';' >> x3::int_ >> ']'; auto on_portal = [&](auto& ctx) {}; auto portal = (x3::char_('P') >> -dest)[on_portal]; auto tiles = +portal; x3::phrase_parse(begin, end, tiles, x3::eol); } int main() { std::string x; parse(x.begin(), x.end()); } It fails with a static assertion: error:

Boost Spirit grammar eol

拥有回忆 提交于 2019-12-23 03:05:45
问题 I am trying to parse files of the following form: // comment bla bla [sectionname] key = value key2=value2 // comment key = value [anothersection] ... using the following code. Unfortunately, it reports the last eol as an error although all eols at the end should be accepted by: (*qi::eol > -(sectionGrammar > *(+qi::eol > sectionGrammar)) > *qi::eol), Besides I really don't know how to parse comments properly without taking the eol which is required for the next key-value pair which is the

qi::rule with inherited attribute as inherited attribute

混江龙づ霸主 提交于 2019-12-22 18:43:20
问题 Let's say we have a rule1 qi::rule<std::string::iterator, int()> rule1 = qi::int_[qi::_val=qi::_1]; And we decide getting an int as attribute is not enough, we also want to get the raw data (boost::iterator_range). We may have a lot of rules with the same type as rule1. So it's better to have a generic solution for this. Therefore we could define another rule2. qi::rule< std::string::iterator, std::pair<int, boost::iterator_range<std::string::iterator>>( qi::rule<std::string::iterator, int()>

Boost spirit parsing string with leading and trailing whitespace

假装没事ソ 提交于 2019-12-22 18:03:38
问题 I am still new to Boost spirit. I am trying to parse a string with possible lead and trailing whitespace and intermediate whitespace. I want to do the following with the string Remove any trailing and leading whitespace Limit the in-between word spaces to one whitespace For example "( my test1 ) (my test2)" gets parsed as two terms - "my test1" "my test2" I have used the following logic using boost::spirit::qi; struct Parser : grammar<Iterator, attribType(), space_type> { public: Parser() :

Why does using a stream in boost spirit penalize performance so much?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 12:36:16
问题 I have prepared a small benchmark program for measuring different ways of parsing. The problem comes with the huge decrease in performance when using a stream and a custom function for storing a date as a time_t + double. The weird boost spirit trait for std::string is because seek backtracking fills the variable string with all the common parts of non-matching lines until a line that matches is found. Sorry for the source code quality (copy/paste, bad variable names, weak indentation...). I

parse typed csv file with boost::spirit::qi

点点圈 提交于 2019-12-22 10:59:05
问题 I want to parse a CSV-File with typed values. The type of every column is defined in the header, e.x.: int double double int unsigned 12 1.3 23445 1 42 45 46 47 48 49 The result data structure may be something like this 2-dimensional vector: using ColumnType = boost::variant< std::vector<int>, std::vector<unsigned>, std::vector<double> >; using ResultType = std::vector<ColumnType>; My working code: namespace phoenix = boost::phoenix; namespace qi = boost::spirit::qi; namespace ascii = boost:

Is there a way to match the content of a spirit::lex string token as a literal in a spirit::qi grammar

可紊 提交于 2019-12-22 10:10:40
问题 I'm writing a DSL and using a Boost Spirit lexer to tokenize my input. In my grammar, I want a rule similar to this (where tok is the lexer): header_block = tok.name >> ':' >> tok.stringval > ';' >> tok.description >> ':' >> tok.stringval > ';' ; Rather than specifying reserved words for the language (e.g. "name", "description") and deal with synchronizing these between the lexer and grammar, I want to just tokenize everything that matches [a-zA-Z_]\w* as a single token type (e.g. tok.symbol