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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:58:07+00:00 2026-05-30T21:58:07+00:00

I’m using Boost Spirit to parse source files in a little compiler project. If

  • 0

I’m using Boost Spirit to parse source files in a little compiler project.

If an error arise during the parsing itself, I can print the position of the error, but how do I do in later phase, typically when performing semantic checks ?

My source file is parsed into an Abstract Syntax Tree using auto rules. I want to add line and col informations into the AST nodes. Is there a easy way to achieve that during parsing ?

I’m using boost::spirit::classic::position_iterator2 in my Lexer and then use this lexer in my grammar.

Thank you

EDIT for sehe:

The lexer is defined like that:

typedef std::string::iterator base_iterator_type;
typedef boost::spirit::classic::position_iterator2<base_iterator_type> pos_iterator_type;
typedef boost::spirit::lex::lexertl::token<pos_iterator_type> Tok;
typedef boost::spirit::lex::lexertl::actor_lexer<Tok> lexer_type;

template<typename L>
class SpiritLexer : public lex::lexer<L> {
   //Token definitions
}

typedef lexer_type::iterator_type Iterator;
typedef SpiritLexer<lexer_type> Lexer;

The grammars are defined like that:

struct EddiGrammar : qi::grammar<lexer::Iterator, ast::SourceFile()> {
    EddiGrammar(const lexer::Lexer& lexer);

    //Token definitions
};

And finally, here is how I parse the source:

ast::SourceFile program

std::ifstream in(file.c_str());
in.unsetf(std::ios::skipws);

in.seekg(0, std::istream::end);
std::size_t size(static_cast<size_t>(in.tellg()));

in.seekg(0, std::istream::beg);

std::string contents(size, 0);
in.read(&contents[0], size);

pos_iterator_type position_begin(contents.begin(), contents.end(), file);
pos_iterator_type position_end;

Lexer lexer;
EddiGrammar grammar(lexer);

bool r = spirit::lex::tokenize_and_parse(position_begin, position_end, lexer, grammar, program);

In my grammar, I use the lexer by referring to some lexer tokens. For example:

else_ %=
        lexer.else_
    >>  lexer.left_brace
    >>  *(instruction)
    >>  lexer.right_brace;

All my AST nodes are constructed using auto rules.

  • 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-05-30T21:58:09+00:00Added an answer on May 30, 2026 at 9:58 pm

    I’ve found a way to fix this problem.

    I wrote a simple terminal parser that just get the current Position and add it to AST node.

    Here is the parser I wrote:

    namespace boost { namespace spirit {
        BOOST_SPIRIT_TERMINAL(position)
    
        template <>
        struct use_terminal<qi::domain, tag::position> : mpl::true_ {};
    }}
    
    namespace boost { namespace spirit { namespace qi
    {
        struct position : primitive_parser<position>
        {
            position(const eddic::lexer::pos_iterator_type& position_begin) : position_begin(position_begin) {}
    
            template <typename Context, typename Iterator>
            struct attribute {
                typedef eddic::ast::Position type;
            };
    
            template <typename Iterator, typename Context
                , typename Skipper, typename Attribute>
                bool parse(Iterator& first, Iterator const& last
                        , Context& /*context*/, Skipper const& skipper, Attribute& attr) const
            {
                qi::skip_over(first, last, skipper);
    
                auto& pos = position_begin.get_position();
    
                attr.theLine = position_begin.get_currentline();
                attr.file = pos.file;
                attr.column = pos.column;
                attr.line = pos.line;
    
                return true;
            }
    
            template <typename Context>
            info what(Context& context) const {
                return info("position");
            }
    
            const eddic::lexer::pos_iterator_type& position_begin;
        };
    
        template <typename Modifiers>
        struct make_primitive<tag::position, Modifiers> {
            typedef position result_type;
            result_type operator()(unused_type, eddic::lexer::Lexer const& lexer, unused_type) const
            {
                return result_type(lexer);
            }
        };
    }}}
    

    and the struct I use to store the information:

    struct Position {
        std::string file;
        std::string theLine;
        int line;
        int column;
    };
    

    It works well, but I have to pass the position iterator to the parser. If someone know a way to get the position_iterator2 iterator from the Iterator provided to the parse function, I will appreciate.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build

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.