Created
June 29, 2014 08:33
-
-
Save KPCCoiL/c50ac0438171c2c6d799 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/spirit/include/qi.hpp> | |
#include <boost/spirit/include/phoenix.hpp> | |
using namespace boost::spirit; | |
using namespace boost; | |
template<class It> | |
struct bool_grammar : qi::grammar<It,bool(),ascii::space_type> { | |
qi::rule<It,bool(),ascii::space_type> constant,formula; | |
bool_grammar() : bool_grammar::base_type(formula) { | |
using qi::lit; | |
constant = (lit('T')[_val = true] | lit('F')[_val = false]); | |
formula = (constant [_val = _1] | | |
('(' >> formula >> 'v' >> formula >> ')')[_val = _1 || _2] | | |
('(' >> formula >> '^' >> formula >> ')')[_val = _1 && _2]); | |
} | |
}; | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
int main(){ | |
string buf; | |
bool result; | |
bool_grammar<string::const_iterator> grammar; | |
while (getline(cin,buf)) { | |
string::const_iterator first = buf.begin(), last = buf.end(); | |
bool success = qi::phrase_parse( | |
first,last, | |
grammar, | |
ascii::space, | |
result | |
); | |
if(success && first == last) | |
cout << " => " << (result ? 'T' : 'F') << endl; | |
else | |
cout << " => parse failed" << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment