Thread: loop with spirit

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    5

    loop with spirit

    I want to write interpreter using spirit library but i have a little problem. I don't know how to write loop for example "for" with syntax "for 1 to 10 endfor". Can someone tell me how to use iterators with grammar to parse part of input 10 times, or maybe it is better to use tree?

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I am not sure of your question exactly. I know personally who wrote the boost spirit library. If you pose your question a little better, I could ask him.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    5
    I would like to know how to parse part of file multiple times within grammar structure.

    I need this for example to interpret file like this;

    $i = 1;
    for 1 to 10
    i += 1;
    endfor

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I want to write interpreter using spirit library but i have a little problem. I don't know how to write loop for example "for" with syntax "for 1 to 10 endfor".

    It may be a good idea to read up on the documentation at some point.

    >> I know personally who wrote the boost spirit library.

    Really - Joel de Guzman? I consider him to be at the top of the list of truly innovative and visionary software engineers. Amazing guy. Please give him my regards.
    Last edited by Sebastiani; 05-17-2009 at 01:56 AM. Reason: misquote
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    5
    Doumentation only shows in simple way how to use file iterators and position iterators, but i want to use this within grammar structure. I came across some problems with trying to do this.

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    What you want requires the context of the grammar. You could in the semantic actions of the grammar, recognize a for loop, analyze the loop variable, then in the c++, run a loop over that variable. However, I have never used boost spirit. I have just seen presentations on it. I have e-mailed one of the writers. I'll let you know when he gets back to me.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by linuxdude
    I have e-mailed one of the writers. I'll let you know when he gets back to me.
    grygus, you might find it simpler to just ask on the Boost users mailing list.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    5
    I managed to do this but I really dislike this solution;

    When i recognize for loop, I am making new grammar object and in c++ loop parse my for with this grammar. Somhow it is working correct.

    Thanks for really fast answers.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The grammar of the for-loop is:

    'for' expression 'to' expression statements 'endfor'

    What's so hard about writing this in spirit? Surely you already have an expression and a statements production.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    5
    I didn't have problem with parsing "for loop" syntax but with semantic action on it

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You generally really shouldn't interpret code as you parse it. You should generate some intermediate representation (an AST, or bytecode), and then interpret that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM