3

私は DSL を書いており、Boost Spirit lexer を使用して入力をトークン化しています。私の文法では、これに似たルールが必要です (tokレクサーはどこにありますか):

header_block =
    tok.name >> ':' >> tok.stringval > ';' >>
    tok.description >> ':' >> tok.stringval > ';'
  ;

言語の予約語 (「名前」、「説明」など) を指定して、これらをレクサーと文法の間で同期させるのではなく、一致するものすべてを[a-zA-Z_]\w*単一のトークン タイプ (たとえばtok.symbol) としてトークン化し、文法をそれを整理する。レクサーを使用していない場合は、次のようにします。

stringval = lexeme['"' >> *(char_ - '"') >> '"'];
header_block =
    lit("name") >> ':' >> stringval > ';' >>
    lit("description") >> ':' >> stringval > ';'
  ;

レクサーを組み合わせて、次のルールをコンパイルできますが、もちろん、必要以上に一致します。特定のシンボル値「名前」と「説明」は気にしません。

header_block =
    tok.symbol >> ':' >> tok.stringval > ';' >>
    tok.symbol >> ':' >> tok.stringval > ';'
  ;

私が探しているのは次のようなものです:

header_block =
    specific_symbol_matcher("name") >> ':' >> tok.stringval > ';' >>
    specific_symbol_matcher("description") >> ':' >> tok.stringval > ';'
  ;

Qi は、specific_symbol_matcher手を振る代わりに使用できるものを提供してくれますか? 提供されているものを使用して近づくことができる場合は、独自のマッチャーを作成したくありません。独自のマッチャーを作成する必要がある場合、その方法を提案できる人はいますか?

4

1 に答える 1

5

トークンが std::string を公開している場合は、次のことができるはずです。

 statement =
      ( tok.keyword [ qi::_pass = (_1 == "if")   ] >> if_stmt )
    | ( tok.keyword [ qi::_pass = (_1 == "while) ] >> while_stmt );

私の理解が正しければ、これは多かれ少なかれ、あなたが求めていたものです。

あなたがそれに取り組んでいる間、 Nabialek Trickqi::symbol<>として知られている、特に気の利いたアプリケーションを見てください。


おまけ素材

既存の文法を字句解析器で機能させるのに苦労している場合のために、calc_utree_ast.cpp例を使って字句解析器で動作させるために行ったことを次に示します。

それが示している

  • 公開された属性を直接使用する方法
  • これらの文字リテラルが(匿名の)トークンとして登録されている限り、文字リテラルに基づいてどのように解析できるか
  • (単純な) 式のガンマーがどのように最小限に変更されたか
  • スキッピング動作がレクサーにどのように移動されたか


///////////////////////////////////////////////////////////////////////////////
//
//  Plain calculator example demonstrating the grammar. The parser is a
//  syntax checker only and does not do any semantic evaluation.
//
//  [ JDG May 10, 2002 ]        spirit1
//  [ JDG March 4, 2007 ]       spirit2
//  [ HK November 30, 2010 ]    spirit2/utree
//  [ SH July 17, 2012 ]        use a lexer
//
///////////////////////////////////////////////////////////////////////////////

#define BOOST_SPIRIT_DEBUG

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/lex_lexertl.hpp>
#include <boost/spirit/include/support_utree.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_function.hpp>

#include <iostream>
#include <string>

namespace lex    = boost::spirit::lex;
namespace qi     = boost::spirit::qi;
namespace spirit = boost::spirit;
namespace phx    = boost::phoenix;

// base iterator type
typedef std::string::const_iterator BaseIteratorT;

// token type
typedef lex::lexertl::token<BaseIteratorT, boost::mpl::vector<char, uint32_t> > TokenT;

// lexer type
typedef lex::lexertl::actor_lexer<TokenT> LexerT;

template <typename LexerT_>
struct Tokens: public lex::lexer<LexerT_> {
   Tokens() {
      // literals
      uint_ = "[0-9]+";
      space = " \t\r\n";

      // literal rules
      this->self += uint_;
      this->self += '+';
      this->self += '-';
      this->self += '*';
      this->self += '/';
      this->self += '(';
      this->self += ')';

      using lex::_pass;
      using lex::pass_flags;
      this->self += space [ _pass = pass_flags::pass_ignore ];
   }

   lex::token_def<uint32_t> uint_;
   lex::token_def<lex::omit> space;
};

namespace client
{
    namespace qi     = boost::spirit::qi;
    namespace ascii  = boost::spirit::ascii;
    namespace spirit = boost::spirit;

    struct expr
    {
        template <typename T1, typename T2 = void>
        struct result { typedef void type; };

        expr(char op) : op(op) {}

        void operator()(spirit::utree& expr, spirit::utree const& rhs) const
        {
            spirit::utree lhs;
            lhs.swap(expr);
            expr.push_back(spirit::utf8_symbol_range_type(&op, &op+1));
            expr.push_back(lhs);
            expr.push_back(rhs);
        }

        char const op;
    };
    boost::phoenix::function<expr> const plus   = expr('+');
    boost::phoenix::function<expr> const minus  = expr('-');
    boost::phoenix::function<expr> const times  = expr('*');
    boost::phoenix::function<expr> const divide = expr('/');

    struct negate_expr
    {
        template <typename T1, typename T2 = void>
        struct result { typedef void type; };

        void operator()(spirit::utree& expr, spirit::utree const& rhs) const
        {
            char const op = '-';
            expr.clear();
            expr.push_back(spirit::utf8_symbol_range_type(&op, &op+1));
            expr.push_back(rhs);
        }
    };
    boost::phoenix::function<negate_expr> neg;

    ///////////////////////////////////////////////////////////////////////////////
    //  Our calculator grammar
    ///////////////////////////////////////////////////////////////////////////////
    template <typename Iterator>
    struct calculator : qi::grammar<Iterator, spirit::utree()>
    {
        template <typename Tokens>
        calculator(Tokens const& toks) : calculator::base_type(expression)
        {
            using qi::_val;
            using qi::_1;

            expression =
                term                            [_val = _1]
                >> *(   ('+' >> term            [plus(_val, _1)])
                    |   ('-' >> term            [minus(_val, _1)])
                    )
                ;

            term =
                factor                          [_val = _1]
                >> *(   ('*' >> factor          [times(_val, _1)])
                    |   ('/' >> factor          [divide(_val, _1)])
                    )
                ;

            factor =
                    toks.uint_                  [_val = _1]
                |   '(' >> expression           [_val = _1] >> ')'
                |   ('-' >> factor              [neg(_val, _1)])
                |   ('+' >> factor              [_val = _1])
                ;

            BOOST_SPIRIT_DEBUG_NODE(expression);
            BOOST_SPIRIT_DEBUG_NODE(term);
            BOOST_SPIRIT_DEBUG_NODE(factor);
        }

        qi::rule<Iterator, spirit::utree()> expression, term, factor;
    };
}

///////////////////////////////////////////////////////////////////////////////
//  Main program
///////////////////////////////////////////////////////////////////////////////
int main()
{
    std::cout << "/////////////////////////////////////////////////////////\n\n";
    std::cout << "Expression parser...\n\n";
    std::cout << "/////////////////////////////////////////////////////////\n\n";
    std::cout << "Type an expression...or [q or Q] to quit\n\n";

    using boost::spirit::utree;
    typedef std::string::const_iterator iterator_type;
    typedef Tokens<LexerT>::iterator_type IteratorT;
    typedef client::calculator<IteratorT> calculator;

    Tokens<LexerT> l;
    calculator calc(l); // Our grammar

    std::string str;
    while (std::getline(std::cin, str))
    {
        if (str.empty() || str[0] == 'q' || str[0] == 'Q')
            break;

        std::string::const_iterator iter = str.begin();
        std::string::const_iterator end  = str.end();
        utree ut;
        bool r = lex::tokenize_and_parse(iter, end, l, calc, ut);

        if (r && iter == end)
        {
            std::cout << "-------------------------\n";
            std::cout << "Parsing succeeded: " << ut << "\n";
            std::cout << "-------------------------\n";
        }
        else
        {
            std::string rest(iter, end);
            std::cout << "-------------------------\n";
            std::cout << "Parsing failed\n";
            std::cout << "stopped at: \"" << rest << "\"\n";
            std::cout << "-------------------------\n";
        }
    }

    std::cout << "Bye... :-) \n\n";
    return 0;
}

入力用

8*12312*(4+5)

印刷されます(デバッグ情報なし)

Parsing succeeded: ( * ( * 8 12312 ) ( + 4 5 ) )
于 2012-07-17T00:11:01.443 に答える