boost-spirit

Boost::spirit how to parse and call c++ function-like expressions

廉价感情. 提交于 2019-11-27 21:44:31
I want to use boost spirit to parse an expression like function1(arg1, arg2, function2(arg1, arg2, arg3), function3(arg1,arg2)) and call corresponding c++ functions. What should be the grammar to parse above expression and call the corresponding c++ function by phoneix::bind()? I have 2 types of functions to call 1) string functions; wstring GetSubString(wstring stringToCut, int position, int length); wstring GetStringToken(wstring stringToTokenize, wstring seperators, int tokenNumber ); 2) Functions that return integer; int GetCount(); int GetId(wstring srcId, wstring srcType); Second Answer

How to benchmark Boost Spirit Parser?

拥有回忆 提交于 2019-11-27 21:21:12
I'm working on a compiler and I would like to improve its performances. I found that about 50% of the time is spent parsing the source files. As the source file are quite small and I do quite a lot of transformations after that, it seems to me that it is perfectible. My parser is a Boost Spirit parser with a lexer (with lexer::pos_iterator) and I have a middle sized grammar. I'm parsing the source into an AST. My problem is that I have no idea what takes the most time during the parsing: copies of AST nodes, lexer, parser rules or memory. I don't think that it is I/O problem since I'm working

Boost Spirit x3: parse into structs

梦想的初衷 提交于 2019-11-27 17:00:13
问题 From the Boost Spirit X3 tutorial: First, let's create a struct representing an employee: namespace client { namespace ast { struct employee { int age; std::string surname; std::string forename; double salary; }; }} Then, we need to tell Boost.Fusion about our employee struct to make it a first-class fusion citizen that the grammar can utilize. BOOST_FUSION_ADAPT_STRUCT( client::ast::employee, (int, age) (std::string, surname) (std::string, forename) (double, salary) )` [...] In fusion's view

Constraining the existing Boost.Spirit real_parser (with a policy)

人走茶凉 提交于 2019-11-27 15:52:26
I want to parse a float, but not allow NaN values, so I generate a policy which inherits from the default policy and create a real_parser with it: // using boost::spirit::qi::{real_parser,real_policies, // phrase_parse,double_,char_}; template <typename T> struct no_nan_policy : real_policies<T> { template <typename I, typename A> static bool parse_nan(I&, I const&, A&) { return false; } }; real_parser<double, no_nan_policy<double> > no_nan; // then I can use no_nan to parse, as in the following grammar bool ok = phrase_parse(first, last, no_nan[ref(valA) = _1] >> char_('@') >> double_[ref(b)

Troubles with boost::spirit::lex & whitespace

久未见 提交于 2019-11-27 15:42:27
I try learning to use boost::spirit. To do that, I wanted to create some simple lexer, combine them and then start parsing using spirit. I tried modifying the example, but it doesn't run as expected (the result r isn't true). Here's the lexer: #include <boost/spirit/include/lex_lexertl.hpp> namespace lex = boost::spirit::lex; template <typename Lexer> struct lexer_identifier : lex::lexer<Lexer> { lexer_identifier() : identifier("[a-zA-Z_][a-zA-Z0-9_]*") , white_space("[ \\t\\n]+") { using boost::spirit::lex::_start; using boost::spirit::lex::_end; this->self = identifier; this->self("WS") =

Parse int or double using boost spirit (longest_d)

邮差的信 提交于 2019-11-27 15:09:30
I'm looking for a way to parse a string as an int or a double, the parser should try both alternatives and choose the one matching the longest portion of the input stream. There is a deprecated directive (longest_d) that does exactly what I'm looking for: number = longest_d[ integer | real ]; ...since it's deprecated, there are any other alternatives? In case it's necessary to implement a semantic action to achieve the desired behavior, does anyone have a suggestion? Firstly, do switch to Spirit V2 - which has superseded classical spirit for years now. Second, you need to make sure an int gets

How to calculate boolean expression in Spirit

瘦欲@ 提交于 2019-11-27 14:32:58
I found a really good example about boolean translator, * Boolean expression (grammar) parser in c++ What I am thinking now is to do a further step, translate (!T|F)&T into F or 0 , so it is very convenient for calculating a very long boolean expression. Is there some examples about this using spirit? What I have done is making a calculator first, and then let it calculate '(T+!F*T)', which equal to (T||!F&&T) but when I type () , there is an error. How to modify it? Thanks a lot! #include <iostream> #include <stack> #include <boost/lexical_cast.hpp> #include <boost/config/warning_disable.hpp>

How to parse reserved words correctly in boost spirit

巧了我就是萌 提交于 2019-11-27 09:43:41
I'm trying to parse a sequence of the syntax: < direction > < type > < name >. For example: in float foo where the direction can be either in , out , or in_out . I've succeeded in parsing correct text by using a qi::symbols class to convert the direction keywords to an enum. However, the problem shows when I don't have correct text. Take the example: int foo The symbol table parser will except the 'in' part of the 'int' type and so the results will be: direction: in type: t name: foo And the error is not detected. What's the best way to be able to parse the in, out and in_out reserved words

Boost::Spirit Expression Parser

走远了吗. 提交于 2019-11-27 08:44:17
I have another problem with my boost::spirit parser. template<typename Iterator> struct expression: qi::grammar<Iterator, ast::expression(), ascii::space_type> { expression() : expression::base_type(expr) { number %= lexeme[double_]; varname %= lexeme[alpha >> *(alnum | '_')]; binop = (expr >> '+' >> expr)[_val = construct<ast::binary_op<ast::add>>(_1,_2)] | (expr >> '-' >> expr)[_val = construct<ast::binary_op<ast::sub>>(_1,_2)] | (expr >> '*' >> expr)[_val = construct<ast::binary_op<ast::mul>>(_1,_2)] | (expr >> '/' >> expr)[_val = construct<ast::binary_op<ast::div>>(_1,_2)] ; expr %= number

Boost spirit revert parsing

痞子三分冷 提交于 2019-11-27 08:37:22
问题 I want to parse a file containing the following structure: some garbage *&% section1 { section_content } section2 { section_content } The rule parsing section_name1 { ... } section_name2 { ... } is already defined: section_name_rule = lexeme[+char_("A-Za-z0-9_")]; section = section_name_rule > lit("{") > /*some complicated things*/... > lit("}"); sections %= +section; So I need to skip any garbage until the sections rule is met. Is there any way to accomplish this? I have tried seek[sections]