1

I am using the Console as an input source. I looked for a way qi would parse a line and then it'd wait for the next line and continue parsing from that point. for example take the following grammar

start = MultiLine | Line;
Multiline = "{" *(Line) "}";
Line = *(char_("A-Za-z0-9"));

As an input

{ AAAA
Bbbb
lllll
lalala
}

Taking the whole thing from a file is easy. But what should i do if i need to have the input to come from the console instead? That i want to have it process what its given already and wait for the rest of the lines.

2 Answers2

1

You can just use an istream iterator which you construct from std::cin for this. If your grammar is templated against typename iterator like Hartmut does in all the examples, then it should work fine.

It would be easy to illustrate if you provide an SSCCE. The istream iterator is only a few lines of code to create.

Chris Beck
  • 15,614
  • 4
  • 51
  • 87
1

The most "highlevel" idiomatic way to do this in Qi is qi::match:

Live On Coliru

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_match.hpp>

using Line      = std::string;
using MultiLine = std::vector<Line>;

int main() {

    std::cin.unsetf(std::ios::skipws);

    MultiLine block;
    using namespace boost::spirit::qi;

    while (std::cin >> phrase_match('{' >> lexeme[*~char_("\r\n}")] % eol >> '}', blank, block))
    {
        std::cout << "Processing a block of " << block.size() << " lines\n";
        block.clear();
    }
}

Prints:

Processing a block of 4 lines
Processing a block of 7 lines

Where the second line appears after one second delay (due to the sleep 1 used)

As Chris Beck was hinting, this uses boost::spirit::istream_iterator under the hood. You can also use that explicitly, e.g. if you want to use recursively nested rules.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Your answer is pretty informative, and it pretty much solved all my problems. i will probably delve deeper into the istream_iterator that Chris mentioned later on. I actually was hoping for you to answer my question because of how informed into the subject you are from what i read of all answers to boost::spirit related questions. thank you for giving your time answering all our questions! – Jack SYCrash Cypher Sep 01 '15 at 12:48