Advertisement
Guest User

Attribute compatibility not disabled by semantic actions

a guest
Jul 16th, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <algorithm>
  2. #include <string>
  3. #include <boost/spirit/include/lex_lexertl.hpp>
  4. #include <boost/spirit/include/qi.hpp>
  5. #include <boost/spirit/include/support_utree.hpp>
  6. #include <boost/spirit/include/phoenix_function.hpp>
  7. #include <boost/spirit/include/phoenix_object.hpp>
  8. #include <boost/spirit/include/phoenix_stl.hpp>
  9.  
  10. namespace lex    = boost::spirit::lex;
  11. namespace qi     = boost::spirit::qi;
  12. namespace spirit = boost::spirit;
  13. namespace phx    = boost::phoenix;
  14.  
  15. using lex::token_def;
  16. using qi::rule;
  17. using qi::_1;
  18. using qi::_2;
  19. using qi::_val;
  20. using spirit::utree;
  21. using phx::construct;
  22.  
  23. // base iterator type
  24. typedef std::string::iterator BaseIteratorT;
  25.  
  26. // token type
  27. typedef lex::lexertl::token<BaseIteratorT, boost::mpl::vector<double, int, std::string> > TokenT;
  28.  
  29. // lexer type
  30. typedef lex::lexertl::actor_lexer<TokenT> LexerT;
  31.  
  32. template <typename LexerT>
  33. struct Tokens: public lex::lexer<LexerT>
  34. {
  35.    Tokens()
  36.    {
  37.       using lex::_pass;
  38.       using lex::pass_flags;
  39.  
  40.       // literals
  41.       symbol = "[a-zA-Z_?](\\w|\\?)*|@(\\w|\\?)+";
  42.       arrow = "->";
  43.       dot = '.';
  44.       assign = "=";
  45.  
  46.       // literal rules
  47.       this->self += symbol;
  48.       this->self += arrow;
  49.       this->self += dot;
  50.       this->self += assign;
  51.    }
  52.  
  53.    ~Tokens() {}
  54.  
  55.    // literal tokens
  56.    token_def<std::string> symbol;
  57.    token_def<> arrow, dot;
  58.    token_def<lex::omit> assign;
  59. };
  60.  
  61. struct ternaryImpl
  62. {
  63.    template <typename Expr1Type, typename Expr2Type>
  64.    struct result { typedef utree type; };
  65.  
  66.    template <typename Expr1Type, typename Expr2Type>
  67.    utree operator()(Expr1Type &vec, Expr2Type &operand) const {
  68.       utree ret;
  69.  
  70.       for (typename Expr1Type::iterator it = vec.begin(); it != vec.end(); ++it) {
  71.          // some code
  72.          ret = *it;
  73.          // more code
  74.       }
  75.  
  76.       // some code here
  77.  
  78.       return ret;
  79.    }
  80. };
  81.  
  82. phx::function<ternaryImpl> ternary = ternaryImpl();
  83.  
  84. template <typename Iterator>
  85. struct Parser: public qi::grammar<Iterator, utree()>
  86. {
  87.    template <typename Tokens>
  88.    Parser(const Tokens &toks):
  89.       Parser::base_type(expression)
  90.    {
  91.       expression =
  92.          factor
  93.        | (
  94.             +(
  95.                 (
  96.                    toks.symbol
  97.                    [
  98.                       _val = construct<utree::list_type>()
  99.                       // some code here
  100.                    ]
  101.                 |  (
  102.                       factor >>
  103.                      +(
  104.                          toks.arrow >> factor
  105.                       |  toks.dot >> factor
  106.                       )
  107.                    )
  108.                    [
  109.                       _val = construct<utree::list_type>()
  110.                       // some code here
  111.                    ]
  112.                 ) >> toks.assign
  113.              ) >> factor
  114.           ) [ _val = ternary(_1, _2) ]
  115.         ;
  116.    }
  117.  
  118.    rule<Iterator, utree()> expression, factor;
  119. };
  120.  
  121. int main()
  122. {
  123.    typedef Tokens<LexerT>::iterator_type IteratorT;
  124.  
  125.    Tokens<LexerT> l;
  126.    Parser<IteratorT> p(l);
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement