#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/phoenix/operator.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include "example.hpp"
using namespace boost::spirit;
using boost::phoenix::val;
template <typename Lexer>
struct example5_base_tokens : lex::lexer<Lexer>
{
protected:
example5_base_tokens() {}
public:
void init_token_definitions()
{
identifier = "[a-zA-Z_][a-zA-Z0-9_]*";
constant = "[0-9]+";
if_ = "if";
while_ = "while";
this->self += lex::token_def<>('(') | ')' | '{' | '}' | '=' | ';' | constant;
this->self += if_ | while_ | identifier;
this->self("WS")
= lex::token_def<>("[ \\t\\n]+")
| "\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/"
;
}
lex::token_def<lex::omit> if_, while_;
lex::token_def<std::string> identifier;
lex::token_def<unsigned int> constant;
};
template <typename Iterator, typename Lexer>
struct example5_base_grammar
: qi::grammar<Iterator, qi::in_state_skipper<Lexer> >
{
template <typename TokenDef>
example5_base_grammar(TokenDef const& tok)
: example5_base_grammar::base_type(program)
{
using boost::spirit::_val;
program
= +block
;
block
= '{' >> *statement >> '}'
;
statement
= assignment
| if_stmt
| while_stmt
;
assignment
= (tok.identifier >> '=' >> expression >> ';')
[
std::cout << val("assignment statement to: ") << _1 << "\n"
]
;
if_stmt
= (tok.if_ >> '(' >> expression >> ')' >> block)
[
std::cout << val("if expression: ") << _1 << "\n"
]
;
while_stmt
= (tok.while_ >> '(' >> expression >> ')' >> block)
[
std::cout << val("while expression: ") << _1 << "\n"
]
;
expression
= tok.identifier [ _val = _1 ]
| tok.constant [ _val = _1 ]
;
}
typedef qi::in_state_skipper<Lexer> skipper_type;
qi::rule<Iterator, skipper_type> program, block, statement;
qi::rule<Iterator, skipper_type> assignment, if_stmt;
qi::rule<Iterator, skipper_type> while_stmt;
typedef boost::variant<unsigned int, std::string> expression_type;
qi::rule<Iterator, expression_type(), skipper_type> expression;
};
template <typename Lexer>
struct example5_tokens : example5_base_tokens<Lexer>
{
typedef example5_base_tokens<Lexer> base_type;
example5_tokens()
{
else_ = "else";
this->self = else_;
this->base_type::init_token_definitions();
}
lex::token_def<lex::omit> else_;
};
template <typename Iterator, typename Lexer>
struct example5_grammar : example5_base_grammar<Iterator, Lexer>
{
template <typename TokenDef>
example5_grammar(TokenDef const& tok)
: example5_base_grammar<Iterator, Lexer>(tok)
{
this->if_stmt
= this->if_stmt.copy() >> -(tok.else_ >> this->block)
;
}
};
int main()
{
typedef std::string::iterator base_iterator_type;
typedef lex::lexertl::token<
base_iterator_type, boost::mpl::vector<unsigned int, std::string>
> token_type;
typedef lex::lexertl::lexer<token_type> lexer_type;
typedef example5_tokens<lexer_type> example5_tokens;
typedef example5_tokens::iterator_type iterator_type;
typedef example5_grammar<iterator_type, example5_tokens::lexer_def> example5_grammar;
example5_tokens tokens; example5_grammar calc(tokens);
std::string str (read_from_file("example5.input"));
std::string::iterator it = str.begin();
iterator_type iter = tokens.begin(it, str.end());
iterator_type end = tokens.end();
std::string ws("WS");
bool r = qi::phrase_parse(iter, end, calc, qi::in_state(ws)[tokens.self]);
if (r && iter == end)
{
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
std::cout << "-------------------------\n";
}
else
{
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "-------------------------\n";
}
std::cout << "Bye... :-) \n\n";
return 0;
}