Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9058311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:41:40+00:00 2026-06-16T14:41:40+00:00

For some odd reason, I can’t get qi::as_string[] to work with repeat()[] . Parsing

  • 0

For some odd reason, I can’t get qi::as_string[] to work with repeat()[].

Parsing std::string str = { "{ +100S+++ ;\n }" };, I get the following OUTPUT

PLUS OR MINUS+
THREE PLUS OR MINUS
PARSED FINE
-------------------------
Parsing succeeded
-------------------------

which shows that parsing was ok, the first + was captured, but not the subsequent three +++.

NOTE I am just trying to get three_plus_or_minus to capture one to three pluses or minuses in a row as a string. Alternative solutions that don’t use as_string[] would be appreciated as well.

I apologize for the long listing, but I need to use both a lexer and a parser in my real code.

CODE

// --------------  Third Party  --------------
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>

// --------------  C++ stdlib   --------------
#include <iostream>
#include <fstream>
#include <string>

using namespace boost::spirit;
using boost::phoenix::val;

enum token_ids
{
    ID_CONSTANT = 1000,
        ID_INTEGER,
        ID_TAG,
    ID_IDENTIFIER
};

template <typename Lexer>
struct example6_tokens : lex::lexer<Lexer>
{
    example6_tokens()
    {
        identifier = "[a-zA-Z_][a-zA-Z0-9_]*";
        constant   = "[0-9]+";

        tag           = "sl|s|l|tl|SL|S|L|TSL|"    
                                    "z|r|i|Z|R|I|"              
                                    "<>|><|<<>>|>><<|><><|<><>";

        this->self = lex::token_def<>('(') | ')' | '{' | '}' 
            | '=' | ';' | ':' | '+' | '-';

        this->self.add
        (constant,        ID_CONSTANT       )
        (tag,             ID_TAG            )
        (identifier,      ID_IDENTIFIER     )
        ;

        this->self("WS")
            =   lex::token_def<>("[ \\t\\n]+")
                |   "\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/"
                |   "\\/\\/[^\n]*"
                ;
    }
    lex::token_def<std::string>   identifier, tag;
    lex::token_def<unsigned int>  constant;
};

// ----------------------------------------------------------------------------
template <typename Iterator, typename Lexer>
struct example6_grammar
        : qi::grammar<Iterator, qi::in_state_skipper<Lexer> >
{
    template <typename TokenDef>
    example6_grammar(TokenDef const& tok)
        : example6_grammar::base_type(program)
    {
        using boost::spirit::_val;

        program
            =  +block
               ;

        block
            =   '{' >> *slsltl_stmt >> '}'
                ;

        plus_or_minus
                %=   ( qi::as_string[ qi::lit( '+' ) ] | qi::as_string[ '-' ])
                    [
                 std::cout << val("PLUS OR MINUS") << val( _1 ) << "\n"
                    ]
                        ;

        three_plus_or_minus
                %=   ( qi::as_string[ repeat(1,3)['+'] ] | qi::as_string[ repeat(1,3)['-'] ] )
                    [
                 std::cout << val("THREE PLUS OR MINUS") << val( _1 ) << "\n"
                    ]
                        ;

        slsltl_stmt
        =  (  - plus_or_minus
                    >> token(ID_CONSTANT) 
                    >> token(ID_TAG)
                    >> three_plus_or_minus
                    >> ';'
                )
                        [
                    std::cout << val("PARSED FINE") << "\n"
                        ]
                ;

        expression
            =   tok.identifier [ _val = _1 ]
                |   tok.constant   [ _val = _1 ]
                ;
    }

    typedef boost::variant<unsigned int, std::string> expression_type;

    qi::rule<Iterator, qi::in_state_skipper<Lexer> > program, block;
    qi::rule<Iterator, std::string(), qi::in_state_skipper<Lexer> > 
        plus_or_minus, three_plus_or_minus;
    qi::rule<Iterator, std::string(), qi::in_state_skipper<Lexer> > slsltl_stmt;
    qi::rule<Iterator, expression_type(), qi::in_state_skipper<Lexer> >  expression;
};

int
main( int argv, char* argc[] )
{
    typedef std::string::iterator base_iterator_type;
    typedef lex::lexertl::token<
    base_iterator_type, boost::mpl::vector<unsigned int, std::string>
    > token_type;

    typedef lex::lexertl::lexer<token_type> lexer_type;
    typedef example6_tokens<lexer_type> example6_tokens;
    typedef example6_tokens::iterator_type iterator_type;
    typedef example6_grammar<iterator_type, example6_tokens::lexer_def> example6_grammar;

    example6_tokens tokens;                         // Our lexer
    example6_grammar calc(tokens);                  // Our parser
    std::string str = { "{ +100S+++ ;\n }" };

    std::string::iterator it = str.begin();
    iterator_type iter = tokens.begin(it, str.end());
    iterator_type end = tokens.end();

    std::string ws("WS");
    bool r = qi::phrase_parse(iter, end, calc, qi::in_state(ws)[tokens.self]);
    if (r && iter == end)
    {
        std::cout << "-------------------------\n";
        std::cout << "Parsing succeeded\n";
        std::cout << "-------------------------\n";
    }
    else
    {
        std::cout << "-------------------------\n";
        std::cout << "Parsing failed\n";
        std::cout << "-------------------------\n";
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T14:41:41+00:00Added an answer on June 16, 2026 at 2:41 pm

    Try

        slsltl_stmt %=  /*....*/;
    

    Instead of slsltl_stmt =. The use of semantic actions disables automatic attribute propagation.

    (I haven’t looked at the rest of your code yet. There may be more places where this/others things need adapting)


    Edit

    I did some more testing and think this does what you expected:

    three_plus_or_minus
            =   (qi::as_string[ repeat(1,3)[qi::char_('+')] | repeat(1,3)[qi::char_('-')] ])
                [ std::cout << val("THREE PLUS OR MINUS") << _1 << "\n" ]
                    ;
    

    A question out-of-the-box though: why are you using a Lexer? Wouldn’t it make sense to have a token for +++?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason I can't get my SVG filters to work in Firefox. They
For some reason I can't get gdb to recognize the files in my project
For some odd reason I can't seem to communicate w/ my span or div
OK, I'm not super new to java but for some odd reason I can't
I have a very odd problem with the following code. For some reason I
For some odd reason I can no longer access my images in my image
I realized that for some odd reason you can't chain the .Where() after the
For some odd reason, I can't use breakpoints in my eclipse project. All breakpoints
For some odd reason if a user tries to send an email to themselves,
For some odd reason the part where objects are shown and hidden in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.