boost-spirit

PEG rule to identify function protoype

二次信任 提交于 2019-12-13 02:04:14
问题 I am trying to create a parser that can parse C code. My use case to parse a buffer that may contain a function prototype. I want to push this function name into a symbol table. I am new to Spirit and PEG and I am trying to figure out how I can write a rule that can identify function prototypes. This is my current implementation: auto nameRule = x3::alpha >> *x3::alnum; auto fcnPrototypeRule = nameRule >> *nameRule; auto fcnRule = fcnPrototypeRule >> space >> x3::char_('(') >> -(nameRule % ',

Problems with grammar

守給你的承諾、 提交于 2019-12-12 21:42:18
问题 I need to parse an expression and I'm using boost :: spirit, the expression must have the form (@anything but @ followed of the string .PV@), and I am using the following grammar P = S >> "." >> V; S = ch_p('@') >> +~ch_p('@'); V = str_p(".PV@"); but does not work me, could you tell me where is the error. I need do it with a grammar and I am using namespace boost::spirit 回答1: Update For completeness adding the regex approach (see at the bottom) In spirit V2 I'd suggest the simpler P = S >> V;

How to use Boost Spirit with variant with more than 20 types?

心不动则不痛 提交于 2019-12-12 21:12:50
问题 I'm parsing a quite complex grammar with Boost Spirit and I'm facing a problem with a variant that have more than 20 types (21 here): namespace eddic { namespace ast { typedef boost::mpl::vector< Integer, IntegerSuffix, Float, Litteral, VariableValue, DereferenceValue, Expression, Unary, Null, True, False, ArrayValue, FunctionCall, MemberFunctionCall, Cast, BuiltinOperator, Assignment, SuffixOperation, PrefixOperation, Ternary > types_initial; typedef boost::mpl::push_back<types_initial, New>

Iterative update of abstract syntax tree with boost spirit

旧时模样 提交于 2019-12-12 21:01:10
问题 I have a working boost spirit parser and was thinking if it is possible to do iterative update of an abstract syntax tree with boost spirit? I have a struct similar to: struct ast; typedef boost::variant< boost::recursive_wrapper<ast> > node; struct ast { std::vector<int> value; std::vector<node> children; }; Which is being parsed by use of: bool r = phrase_parse(begin, end, grammar, space, ast); Would it be possible to do iterative update of abstract syntax tree with boost spirit? I have not

boost spirit istream iterator giving false positives

耗尽温柔 提交于 2019-12-12 20:19:09
问题 So I'm trying to get spirit to parse the characters from this file as it's input. I'd rather not read the full string into memory if at all possible. This is my current relevant code, the Rosters_Grammar is a grammar file that I am using to specify my desired grammar. #include "StdAfx.h" #include "Interpreter.h" #include "Rosters_Grammar.h" #include <boost\spirit\include\qi.hpp> #include <fstream> bool Interpreter::invoke(std::string path) { //Define our parser type and iterator types.

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

Using Boost.Spirit.Qi with custom lexer

放肆的年华 提交于 2019-12-12 19:56:16
问题 I dug through the whole documentation and couldn't find an example. All the examples either parse character data or use Spirit.Lex. Forgive me if I missed something. Can someone give an example for, or point to a tutorial on, how to use Boost.Spirit.Qi with my custom lexer? E.g.: vector<MyTokenType> tokens = GetTokens(); // use boost spirit to work with MyTokenType on per-token granularity 回答1: You will have to do sevaral things: a) expose the token sequence as a range of iterators, which

Spirit Qi sequence parsing issues

社会主义新天地 提交于 2019-12-12 18:37:41
问题 I have some issues with parser writing with Spirit::Qi 2.4. I have a series of key-value pairs to parse in following format <key name>=<value> . Key name can be [a-zA-Z0-9] and is always followed by = sign with no white-space between key name and = sign. Key name is also always preceded by at least one space. Value can be almost any C expression (spaces are possible as well), with the exception of the expressions containing = char and code blocks { } . At the end of the sequence of the key

Boost.Spirit: Setup sub-grammar during parsing

十年热恋 提交于 2019-12-12 16:56:29
问题 To handle large compile times and reuse of grammars I've composed my grammar into several sub-grammars which are called in sequence. One of them (call it: SETUP grammar) offers some configuration of the parser (via symbols parser), so later sub grammars logically depend on that one (again via different symbols parsers). So, after SETUP is parsed, the symbols parsers of the following sub grammars need to be altered. My question is, how to approach this efficiently while preserving loose

Boost::spirit passing semantic actions in inherited attributes

纵然是瞬间 提交于 2019-12-12 15:08:43
问题 I'm trying to pass semantic action in a grammar's inherited argument. In the very basic example below the grammar parses two numbers and I pass semantic action (in a form of c++ lambda) into it and I'd like this action to be called on parsing of the first number. However it does not called but silently ignored and I'd like to know why is it so and what is the proper way to do such things. #include <iostream> #include <boost/spirit/include/qi.hpp> using namespace std; using namespace boost;