boost-spirit

Boost spirit floating number parser precision

坚强是说给别人听的谎言 提交于 2019-12-03 21:11:55
问题 There is something strange I noticed when comparing boost::lexical_cast and boost spirit parsing. I'm trying to parse a string into float. for some reason spirit gives very imprecise result. for example: when parsing string "219721.03839999999" using lexical_cast i get 219721.03 which is more or less OK. but when I use spirit (see code below) I get "219721.11" which is far from bein OK. Any idea why it happens? template<> inline float LexicalCastWithTag(const std::string& arg) { float result

How to parse a mathematical expression with boost::spirit and bind it to a function

こ雲淡風輕ζ 提交于 2019-12-03 17:28:24
I would like to define a function taking 2 arguments double func(double t, double x); where the actual implementation is read from an external text file. For example, specifying in the text file function = x*t; the function should implement the multiplication between x and t , so that it could be called at a later stage. I'm trying to parse the function using boost::spirit. But I do not know how to actually achieve it. Below, I created a simple function that implements the multiplication. I bind it to a boost function and I can use it. I also created a simple grammar, which parse the

Simple expression parser example using Boost::Spirit?

耗尽温柔 提交于 2019-12-03 17:09:41
问题 Is anyone aware of an online resource where I can find out how to write a simple expression parser using Boost::Spirit?. I do not necessarily need to evaluate the expression, but I need to parse it and be able to return a boolean to indicate whether the expression is parsable or not (e.g. brackets not matching etc). I need the parser to be able recognise function names (e.g. foo and foobar), so this would also be a useful example to help me learn writing BNF notation. The expressions will be

How to put results into a STL map by using boost-spirit?

痴心易碎 提交于 2019-12-03 17:07:02
#include <QtCore/QCoreApplication> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/phoenix.hpp> #include <iostream> #include <string> #include <list> #include <map> #define CODE_CPP_KEYWORD_ENUM "enum" namespace haha { //简单表示c++的enum的类(A structure use to simply description C++ enum) struct CPPCodeEnum { //enum的名称(enum Name) ::std::string enumName; //成员的名称(enum Members‘name) ::std::list<::std::string> enumMembers; }; } namespace haha { namespace fusion = boost::fusion; namespace phoenix = boost::phoenix; namespace qi = boost::spirit::qi; namespace ascii = boost::spirit:

Why does boost::spirit::qi::parse() not set this boost::variant's value?

喜你入骨 提交于 2019-12-03 16:00:35
When trying to parse text into a boost::variant, the variant's value does not get changed. The parsers by themselves appear to work fine, so my assumption is that I'm doing something wrong with the variant code. I'm using boost 1.46.1 and the following code compiles in Visual Studio 2008. 1st Update hkaiser noted that the rule and grammar template arguments must not be Variant but Variant() . This got a bit "further" as I now have a compilation error in boost_1_46_1\boost\variant\variant.hpp(1304) . The comment says: // NOTE TO USER : // Compile error here indicates that the given type is not

Parsing nested key value pairs in Boost Spirit

一世执手 提交于 2019-12-03 15:22:02
I am having trouble writing what I think should be a simple parser using Boost::Spirit. (I'm using Spirit instead of just using string functions as this is partly a learning exercise for me). Data The data to parse takes the form of key value pairs, where a value can itself be a key value pair. Keys are alphanumeric (with underscores and no digit as first character); values are alphanumeric plus .-_ - the values can be dates in the format DD-MMM-YYYY e.g. 01-Jan-2015 and floating point numbers like 3.1415 in addition to plain old alphanumeric strings. Keys and values are separated with = ;

Is it possible to re-use boost::spirit::qi grammar in another grammar definition?

浪尽此生 提交于 2019-12-03 14:57:02
问题 Is it possible to reuse boost::spirit:qi grammar in another grammar (as a rule for example)? For example if I define a grammar to parse line of text into a structure holding street address. template< typename iter > struct address_grammar : qi::grammar< iter, address() > { ... qi::rule< iter, std::string() > street_name; qi::rule< iter, std::string() > street_number; qi::rule< iter, address() > address_; } I might want to reuse that grammar in two other grammars, for example one might be for

boost::spirit::qi and out-of-sequence variables

假装没事ソ 提交于 2019-12-03 14:52:16
I'm writing a lexigraphical analyser. It takes an English string, and converts it into a set of latitude/longitude co-ordinates. It's a bit like Google Earth. Anyway, I've written my symbol tables and grammar, and it's happily parsing formatted data. struct LatLongDegrees { std::string dirLat_; double degLat_; std::string dirLong_; double degLong_; } For example: {"North", 23.59, "East", -30.82} Here is my grammar: basic =(latitude >> ' ' >> double_ >> ' ' >> longitude >> ' ' >> double_); Where latitude and longitude are symbol tables that map from shorthand compass directions to strings (eg

Parsing SQL Queries in C++ using Boost.Spirit

此生再无相见时 提交于 2019-12-03 14:02:59
问题 I have created a database engine in which I can create and modify tables, and add them to a database. For parsing the SQL queries, I have implemented the Boost.Spirit library using EBNF form. I have the parser setup properly and it successfully parses every rule. My problem is I now have no idea how to integrate the two. The Boost.Spirit parser only validates that input is correct, however I need it to actually do something. I looked up semantic actions but they don't seem to handle what I'm

I can't get the string value of a token

不问归期 提交于 2019-12-03 11:57:10
I try to implement a Lexer for a little programming language with Boost Spirit. I have to get the value of a token and I get a bad_get exception : terminate called after throwing an instance of 'boost::bad_get' what(): boost::bad_get: failed value get using boost::get Aborted I obtain this exception when doing : std::string contents = "void"; base_iterator_type first = contents.begin(); base_iterator_type last = contents.end(); SimpleLexer<lexer_type> lexer; iter = lexer.begin(first, last); end = lexer.end(); std::cout << "Value = " << boost::get<std::string>(iter->value()) << std::endl; My