KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am trying to parse a C-function like tree expressions like the following (using the Spirit Parser Framework ): F( A() , B( GREAT( SOME , NOT ) ) , C( YES ) ) For this I am trying to use the three rules on the following grammar: template< typename Iterator , typename ExpressionAST > struct InputGrammar : qi::grammar<Iterator, ExpressionAST(), space_type> { InputGrammar() : InputGrammar::base_type( ) { tag = ( qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9") )[ push_back( at_c<0>(qi::_val) , qi::_1 ) ]; command = tag [ at_c<0>(qi::_val) = at_c<0>(qi::_1) ] >> "(" >> (*instruction >> ",") [ push_back( at_c<1>(qi::_val) , qi::_1 ) ] >> ")"; instruction = ( command | tag ) [qi::_val = qi::_1]; } qi::rule< Iterator , ExpressionAST() , space_type > tag; qi::rule< Iterator , ExpressionAST() , space_type > command; qi::rule< Iterator , ExpressionAST() , space_type > instruction; }; Notice that my tag rule just tries to capture the identifiers used in the expressions (the 'function' names). Also notice that the signature of the tag rule returns a ExpressionAST instead of a std::string , like in most examples. The reason I want to do it like this is actually pretty simple: I hate using variants and I will avoid them if possible. It would be great to keep the cake and eat it too I guess. A command should start with a tag (the name of the current node, first string field of the AST node) and a variable number of arguments enclosed by parentheses, and each of the arguments can be a tag itself or another command. However, this example does not work at all. It compiles and everything, but at run time it fails to parse all
Tags (comma-separated)
Save Edits
Cancel