#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;
enum token_ids
{
ID_CONSTANT = 1000,
ID_IF,
ID_ELSE,
ID_WHILE,
ID_IDENTIFIER
};
template <typename Lexer>
struct example6_tokens : lex::lexer<Lexer>
{
example6_tokens()
{
identifier = "[a-zA-Z_][a-zA-Z0-9_]*";
constant = "[0-9]+";
this->self = lex::token_def<>('(') | ')' | '{' | '}' | '=' | ';';
this->self.add
(constant, ID_CONSTANT)
("if", ID_IF)
("else", ID_ELSE)
("while", ID_WHILE)
(identifier, ID_IDENTIFIER)
;
this->self("WS")
= lex::token_def<>("[ \\t\\n]+")
| "\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/"
;
}
lex::token_def<std::string> identifier;
lex::token_def<unsigned int> constant;
};
template <typename Iterator, typename Lexer>
struct example6_grammar
: qi::grammar<Iterator, qi::in_state_skipper<Lexer> >
{
template <typename TokenDef>
example6_grammar(TokenDef const& tok)
: example6_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
= ( token(ID_IF) >> '(' >> expression >> ')' >> block
>> -(token(ID_ELSE) >> block)
)
[
std::cout << val("if expression: ")
<< _2 << "\n"
]
;
while_stmt
= (token(ID_WHILE) >> '(' >> expression >> ')' >> block)
[
std::cout << val("while expression: ")
<< _2 << "\n"
]
;
expression
= tok.identifier [ _val = _1 ]
| tok.constant [ _val = _1 ]
;
}
typedef boost::variant<unsigned int, std::string> expression_type;
qi::rule<Iterator, qi::in_state_skipper<Lexer> > program, block, statement;
qi::rule<Iterator, qi::in_state_skipper<Lexer> > assignment, if_stmt;
qi::rule<Iterator, qi::in_state_skipper<Lexer> > while_stmt;
qi::rule<Iterator, expression_type(), qi::in_state_skipper<Lexer> > expression;
};
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 example6_tokens<lexer_type> example6_tokens;
typedef example6_tokens::iterator_type iterator_type;
typedef example6_grammar<iterator_type, example6_tokens::lexer_def> example6_grammar;
example6_tokens tokens; example6_grammar calc(tokens);
std::string str (read_from_file("example6.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;
}