c++ - boost spirit expression grammar issue while creating AST -


i'm trying parse expression grammar (with variables) abstract syntax tree (ast) later on make use of ast , calculate values basing on expressions (they may part of function example, there no need store expressions rather calculate value right away).

to surprise, after handling loops , instructions (which require nested structures in ast well), got nothing seg faults after trying parse expression.. after hours of struggling decided ask here, because have no idea (maybe grammar)

this statement-loop part works well. struct 'loop' gets parameter number of repetitions - string far (later on want put expression here):

statement %= loop | inst; inst %= lexeme[+(char_ - (';'|char_('}'))  )] >> ';';  loop = "do("    >   lexeme[+(char_ - "){")] // parse number of loop repetitions         >   "){"         >   *statement > "}"         ; 

structures like:

typedef boost::variant<     boost::recursive_wrapper<s_loop>     , std::string>  s_statement;  struct s_loop {     std::string name;                           // tag name      //s_expression exp; // todo     std::vector<s_statement> children;        // children }; 

i use recursive wrapper, thought maybe because of "deep" wrapping in case of expression-term-factor, why can't it. loop-statement goes like: loop --(contains)--> statement (statement may loop!) , in case of expressions should implemented like: expression -> term -> factor (factor may expression!)

so, sure it's because of 'deep' wrapping, tried trivial grammar: expression -> factor (factor may expression)

ast structures copy-paste of above, quite similar and.... not work! :(

i quite sure must wrong grammar.. honest, not expert of spirit. here's grammar:

expression = factor > * ( (char_('+')|char_('-')) > factor ) ; factor %= uint_ | my_var | my_dat | my_rec_exp; //   factor %= uint_ | my_var | my_dat; //this works! i've made procedures traverse ast // strings , ints parsed , stored inside expression structure //  factor %= uint_ | my_rec_exp; // simple version (of course adjust stucture s_expression) doesn't work.. why? :( , it's less complex loop-statement my_rec_exp = '(' > expression > ')';  my_var %= char_('!') >> lexeme[+  (  char_ - ( ('+')|char_('-')|char_('*')|char_('/')|char_('(')|char_(')') )   ) ] ; my_dat %= char_('#') >> lexeme[+  (  char_ - ( ('+')|char_('-')|char_('*')|char_('/')|char_('(')|char_(')') )   ) ] ; 

structures here: struct s_expression;

typedef boost::variant<        boost::recursive_wrapper<s_expression>, //   s_expression,    std::string,        unsigned int > s_factor;  struct s_term{ // not use in simplified version     s_factor factor0;     std::vector<std::pair<char, s_factor> >      factors; };  struct s_expression{     s_factor term0;     std::vector<std::pair<char, s_factor> >      terms; }; 

i 1 more time without recursive expression works (parses en expression containing set of numers / strings connected operators + / - ). if add expression variant of factor crashes on exec.

thank advice / suggestion !


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -