Question

I've text which contains "equations" like :

-- This is comment
ABC:= 121-XY1/7 > 45 OR SS >= 3
    ZY2 AND -- This is another comment
    (JKL * PQR) < 75;

JKL:= PP1 OR 
      PP2/2 XOR PP3;

ZZ_1:=A-B > 0 XOR (B2 % GH == 6 AND 
   SP == 4
-- Again a comment
    NOT AX > GF < 2 OR C*AS2 >= 5);

I decided to use boost spirit to parse this text , as of now I just need to know my operands and operators.

I've referred this nice answer (thanks to sehe :) ) to write my expression grammar (relational operators not written yet)

However I cannot strip my comments with :-

     qi::phrase_parse(input.begin()
     ,input.end()
     ,p >> ';' // parser object
     ,qi::space | "--" >> *(qi::char_ - qi::eol) >> qi::eol
     ,result //expression object,  (boost::variant with boost::recursive_wrapper)
     ); 

because it gives several errors, some of the post says to tweak a boost header file.

So I'm using another grammar to strip off comment first using :

     qi::phrase_parse(input.begin()
     ,input.end()
     ,qi::char_ >> *qi::char_ 
     , qi::space | "--" >> *(qi::char_ - qi::eol) >> qi::eol
     ,stripped // std::string 
     );

But this gives me a text with all space and comment removed :

ABC:=121-XY1/7>45ORSS>=3ZY2AND(JKL*PQR)<75;JKL:=PP1ORPP2/2XORPP3;ZZ_1:=A-B>0XOR(B2%GH==6ANDSP==4NOTAX>GF<2ORC*AS2>=5);

So, the question is how can I strip just the comments, preserving space and newlines ?

Using : Boost Version 1.55.0

Was it helpful?

Solution

Your exact code sample is missing. Allow me to add a sample skipper to that "boolean expression grammar" you linked to:

std::string const input = 
        "a and\n"
        "-- abacadabra\n"
        "b;";

typedef std::string::const_iterator It;

// ADDED: allow comments
qi::rule<It> skip_ws_and_comments 
    = qi::space 
    | "--" >> *(qi::char_-qi::eol) >> qi::eol
    ;

parser<It, qi::rule<It> > p;

That's all the changes required. Output:

result: (a & b)

See it Live On Coliru

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top