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 8614121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:02:58+00:00 2026-06-12T05:02:58+00:00

I am about to write a parser for a mathematica-like language and have found

  • 0

I am about to write a parser for a mathematica-like language and have found out, that it would be nice to sometimes invoke my spirit grammar for subparts of the expression to parse.

i.e. If I am about to parse

a+b*c+d 

it would be handy to call parse() on the ‘b*c’ part while querying the ‘+’ sign.

Is this possible to do while using the same instance of my grammar? (The grammar parameter would be ‘*this’)

Though I am not yet fully convinced whether this is the best way to accomplish this particular task, I find the question rather interesting, since I could not find anything in the docs.

Obvously I should not depend on class-local or global variables if I used this technique. But I would like to know if it is principally allowed by the design of spirit.

EDIT:

My grammar instances look as follows:

class MyGrammar : public boost::spirit::qi::grammar<...>  
{  
    /* a few rules. Some with local and/or inherited attributes */  
    MyGrammar( void )  
    {
         /* assign all the rules, use a few 'on_error' statements */
         // In one or two rules I would like to invoke parse(...,*this,...)  
         // on a subrange of the expression
    }
}  

Thanks!

  • 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-12T05:02:59+00:00Added an answer on June 12, 2026 at 5:02 am

    Of course you can:

    // In one or two rules I would like to invoke parse(...,*this,...)  
    // on a subrange of the expression
    

    ^ That is not how rules are composed in a declarative grammar. You seem to think of this in procedural terms (which may indicate you could have previous experience writing recursive-descent parsers?).


    Off the top of my mind a simple expression grammar in spirit could look like this:

      literal     = +qi::int_;
      variable    = lexeme [ qi::alpha >> *qi::alnum ];
      term        =  literal 
                   | variable 
                   | (qi::lit('(') > expression >> ')');
    
      factor      = term >> *(qi::char_("+-") >> term);
      expression  = factor >> *(qi::char_("*/%") >> term);
    

    Note the recursion in the last branch of term: it parsers parenthesized expressions.

    This simplistic sample won’t actually result in a parse tree that reflects operator precedence. But the samples and tests in the Spirit library contain many examples that do.

    See also other answers of mine that show how this works in more detail (with full samples):

    • Boost::Spirit Expression Parser

      A fullblown example with links to documentation samples and explanations of improvements of the original code by the asker

    • Boolean expression (grammar) parser in c++

    • Compilation error with a boost::spirit parser yet another approach

    Hope that helps

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

Sidebar

Related Questions

I'm about to write a parser for a language that's supposed to have strict
I have to write some sort of parser that get a String and replace
I have written a parser-generator that has expressions like this: a = f:foo bar
I am about to write a game that turns barcodes into game items (like
I'm just about to write my first application in a duck typed language (Groovy).
I'm about to write a Scala command-line application that relies on a MySQL database.
I'm about to write a Wrapper for a COM interop IStream so that code
I'm about to write server side aplication(most probably it would be PHP but JAVA
I'm learning jQuery and am about to write some pages using intensively that library.
I have to write an argument parser, it is passed in as a string

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.