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

  • SEARCH
  • Home
  • 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 8928957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:34:06+00:00 2026-06-15T08:34:06+00:00

I’m trying to get Boost.Spirit to parse MSVC mangled symbols. These take the form:

  • 0

I’m trying to get Boost.Spirit to parse MSVC mangled symbols. These take the form:


?myvolatileStaticMember@myclass@@2HC

which means “volatile int myclass::myvolatileStaticMember”.

The “key” to the parse is the double at symbol “@@”. Prior to the @@ is the name of the symbol which consists of a C++ identifier followed by zero or more “@” additional identifiers to fully represent the symbol in its absolute namespace hierarchy. After the @@ is the specification of what the identifier is (a variable, a function etc.)

Now, I can get Boost.Spirit to parse either the part preceding the @@ or the part after the @@. I haven’t figured out yet how to get Boost.Spirit to find the @@ and feed what comes before that to one custom parser and what comes after it to a different custom parser.

Here’s my parser for the part preceding the @@:

// This grammar is for a MSVC mangled identifier
template<typename iterator> struct msvc_name : grammar<iterator, SymbolType(), locals<SymbolType, string>>
{
    SymbolTypeDict &typedict;
    void name_writer(SymbolType &val, const string &i) const { val.name=i; }
    void dependent_writer(SymbolType &val, const string &i) const
    {
        SymbolTypeDict::const_iterator dt=typedict.find(i);
        if(dt==typedict.end())
        {
            auto _dt=typedict.emplace(make_pair(i, SymbolType(SymbolTypeQualifier::None, SymbolTypeType::Namespace, i)));
            dt=_dt.first;
        }
        val.dependents.push_back(&dt->second);
    }
    // These work by spreading the building of a templated type over multiple calls using local variables _a and _b
    // We accumulate template parameters into _a and accumulate mangled symbolness into _b
    void begin_template_dependent_writer(SymbolType &, SymbolType &a, string &b, const string &i) const
    {
        a=SymbolType(SymbolTypeQualifier::None, SymbolTypeType::Class, i);
        b=i;
    }
    void add_template_constant_dependent_writer(SymbolType &a, string &b, long long constant) const
    {
        string i("_c"+to_string(constant));
        SymbolTypeDict::const_iterator dt=typedict.find(i);
        if(dt==typedict.end())
        {
            auto _dt=typedict.emplace(make_pair(i, SymbolType(SymbolTypeQualifier::None, SymbolTypeType::Constant, to_string(constant))));
            dt=_dt.first;
        }
        a.templ_params.push_back(&dt->second);
        b.append(i);
    }
    void add_template_type_dependent_writer(SymbolType &a, string &b, SymbolTypeType type) const
    {
        string i("_t"+to_string(static_cast<int>(type)));
        SymbolTypeDict::const_iterator dt=typedict.find(i);
        if(dt==typedict.end())
        {
            auto _dt=typedict.emplace(make_pair(i, SymbolType(SymbolTypeQualifier::None, type)));
            dt=_dt.first;
        }
        a.templ_params.push_back(&dt->second);
        b.append(i);
    }
    void finish_template_dependent_writer(SymbolType &val, SymbolType &a, string &b) const
    {
        SymbolTypeDict::const_iterator dt=typedict.find(b);
        if(dt==typedict.end())
        {
            auto _dt=typedict.emplace(make_pair(b, a));
            dt=_dt.first;
        }
        val.dependents.push_back(&dt->second);
    }
    msvc_name(SymbolTypeDict &_typedict) : msvc_name::base_type(start), typedict(_typedict)
    {
        identifier=+(char_ - '@');
        identifier.name("identifier");
        template_dependent_identifier=+(char_ - '@');
        template_dependent_identifier.name("template_dependent_identifier");
        dependent_identifier=+(char_ - '@');
        dependent_identifier.name("dependent_identifier");
        start = identifier [ boost::phoenix::bind(&msvc_name::name_writer, this, _val, _1) ] >> *(
            lit("@@") >> eps
            | (("@?$" > template_dependent_identifier [ boost::phoenix::bind(&msvc_name::begin_template_dependent_writer, this, _val, _a, _b, _1) ])
                > "@" > +(( "$0" > constant [ boost::phoenix::bind(&msvc_name::add_template_constant_dependent_writer, this, _a, _b, _1) ])
                    | type [ boost::phoenix::bind(&msvc_name::add_template_type_dependent_writer, this, _a, _b, _1) ])
                >> eps [ boost::phoenix::bind(&msvc_name::finish_template_dependent_writer, this, _val, _a, _b) ])
            | ("@" > dependent_identifier [ boost::phoenix::bind(&msvc_name::dependent_writer, this, _val, _1) ]))
            ;
        BOOST_SPIRIT_DEBUG_NODE(start);
        start.name("msvc_name");
        on_error<boost::spirit::qi::fail, iterator>(start,
            cerr << boost::phoenix::val("Parsing error: Expected ") << _4 << boost::phoenix::val(" here: \"")
                << boost::phoenix::construct<string>(_3, _2) << boost::phoenix::val("\"") << endl);
    }

    rule<iterator, SymbolType(), locals<SymbolType, string>> start;
    rule<iterator, string()> identifier, template_dependent_identifier, dependent_identifier;
    msvc_type type;
    msvc_constant<iterator> constant;
};

You’ll note the “lit(“@@”) >> eps” where I’m trying to get it to stop matching once it sees a @@. Now here is the part which is supposed to match a full mangled symbol:

template<typename iterator> struct msvc_symbol : grammar<iterator, SymbolType()>
{
    SymbolTypeDict &typedict;
    /* The key to Microsoft symbol mangles is the operator '@@' which consists of a preamble
    and a postamble. Immediately following the '@@' operator is:
    Variable:
    3<type><storage class>
    Static member variable:
    2<type><storage class>
    Function:
    <near|far><calling conv>[<stor ret>]   <return type>[<parameter type>...]<term>Z
    <Y   |Z  ><A|E|G       >[<?A|?B|?C|?D>]<MangledToSymbolTypeType...>      <@>Z
    Member Function:
    <protection>[<const>]<calling conv>[<stor ret>]   <return type>[<parameter type>...]<term>Z
    <A-V       >[<A-D>  ]<A|E|G       >[<?A|?B|?C|?D>]<MangledToSymbolTypeType...>      <@>Z
    */
    msvc_symbol(SymbolTypeDict &_typedict) : msvc_symbol::base_type(start), typedict(_typedict), name(_typedict), variable(_typedict)
    {
        start="?" >> name >> ("@@" >> variable);
        BOOST_SPIRIT_DEBUG_NODE(start);
        on_error<boost::spirit::qi::fail, iterator>(start,
            cerr << boost::phoenix::val("Parsing error: Expected ") << _4 << boost::phoenix::val(" here: \"")
                << boost::phoenix::construct<string>(_3, _2) << boost::phoenix::val("\"") << endl);
    }

    rule<iterator, SymbolType()> start;
    msvc_name<iterator> name;
    msvc_variable<iterator> variable;
};

So, it matches the “?” easily enough ;). The problem is that it sends everything after the “?” to the msvc_name parser, so instead of the bit from @@ onwards going to msvc_variable and the remainder going to msvc_name, msvc_name consumes everything up to and including @@. This isn’t intuitive, as one would have thought that the brackets mean to do that thing first.

Therefore if I replace:

start="?" >> name >> ("@@" >> variable);

with

start="?" >> name >> variable;

… it all works fine.

However I’d really prefer not to do it this way. Ideally, I want Boost.Spirit to split at @@ cleanly in msvc_symbol and “do the right thing” as it were. I’m thinking I’m probably not thinking recursively enough? Either way I’m stumped.

Note: Yes I am aware I can break the string at @@ and run two separate parsers. That isn’t what I’m asking – rather, I’m asking how to configure Boost.Spirit to parse the end of a phrase before the beginning.

Note also: I’m aware a skipper could be used to make the @@ whitespace and do the split that way. The problem is that what comes before the @@ is very specific, as is what comes after the @@. Therefore it’s not really whitespace.

Many thanks in advance to anyone who can help. From searching Google and Stackoverflow for questions related to this one, overcoming Boost.Spirit’s “left-to-right greediness” is a problem for a lot of people.

Niall

  • 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-15T08:34:08+00:00Added an answer on June 15, 2026 at 8:34 am

    It would seem that you can do a number of things:

    1. explicitely disallow the double “@@” where you expect “@”. See also

      • http://www.boost.org/doc/libs/1_52_0/libs/spirit/doc/html/spirit/qi/reference/operator/difference.html
      • http://www.boost.org/doc/libs/1_52_0/libs/spirit/doc/html/spirit/qi/reference/operator/not_predicate.html
    2. tokenize first (use Spirit Lex?)

    Here I show you a working example of the first approach:

    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/karma.hpp>
    namespace qi = boost::spirit::qi;
    
    template<typename T> T reversed(T c) {  return T(c.rbegin(), c.rend()); }
    
    int main (int argc, char** argv)
    {
        const std::string input("?myvolatileStaticMember@myclass@@2HC");
    
        auto f = begin(input), l = end(input);
    
        auto identifier = +~qi::char_("@");
        auto delimit    = qi::lit("@") - "@@";
    
        std::vector<std::string> qualifiedName;
        std::string typeId;
    
        if (qi::parse(f,l,
                    '?' >> identifier % delimit >> "@@" >> +qi::char_,
                    qualifiedName,
                    typeId))
        {
            using namespace boost::spirit::karma;
            qualifiedName = reversed(qualifiedName);
            std::cout << "Qualified name: "  << format(auto_ % "::" << "\n", qualifiedName);
            std::cout << "Type indication: '" << typeId << "'\n";
        }
    }
    

    Output:

    Qualified name: myclass::myvolatileStaticMember
    Type indication: '2HC'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I have a text area in my form which accepts all possible characters from

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.