提升精神独特的关键字不能按预期工作

时间:2017-10-26 17:31:41

标签: boost boost-spirit

这是我的小样本..我有一种语言,在解析我有类似的东西

 foo()
 nextfoo()  <-- here an error appears because of the keyword "next" 

所以语法

 typedef boost::proto::result_of::deep_copy<BOOST_TYPEOF(ascii::no_caseqi::lit(std::wstring())])>::type nocaselit_return_type;

   nocaselit_return_type nocaselit(const std::wstring& keyword)
   {
     return boost::proto::deep_copy(ascii::no_case[qi::lit(keyword)]);
   }


keywords = nocaselit(L"next")
    | nocaselit(L"else")
    | nocaselit(L"if")
    | nocaselit(L"then")
    | nocaselit(L"for")
    | nocaselit(L"to")
    | nocaselit(L"dim")
    | nocaselit(L"true")
    | nocaselit(L"false")
    | nocaselit(L"as")
    | nocaselit(L"class")
    | nocaselit(L"end")
    | nocaselit(L"function")
    | nocaselit(L"new")
    | nocaselit(L"sub");


  name_valid =   !keywords>> lexeme[+(boost::spirit::standard_wide::alpha | '_') >> *(boost::spirit::standard_wide::alnum | '_')];

我从docu和goolge学到了我必须编写类似这样的东西,以使解析器与关键字一起工作

 name_valid =   distinct(Keywords)[ lexeme[+(boost::spirit::standard_wide::alpha | '_') >> *(boost::spirit::standard_wide::alnum | '_')] ];

但这不起作用..可以用sombody解释我的原因吗?

特殊问题..只要我使用上面的语法,我得到一个模板编译器错误,工作样本必须写成如下(关键字列表是内联而不是规则)。我认为这与规则的类型规范有关。但是正确的是什么?

 name_valid =   distinct(nocaselit(L"next")| nocaselit(L"else") | ... )
 [ lexeme[+(boost::spirit::standard_wide::alpha | '_') >> *(boost

谢谢

1 个答案:

答案 0 :(得分:1)

distinct指令将主题解析器取代[]块而不是()。在()内部指定在边界处禁止排除(通常是包含标识符字符的字符集)。

另请考虑使用qi::symbol,它与qi::no_case配合使用,但内部使用Trie,无需任何回溯。

当我接近电脑时,我会提供一个有效的例子。同时,请随时查看现有示例:How to parse reserved words correctly in boost spirit

演示

<强> Live On Coliru

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/repository/include/qi_distinct.hpp>
namespace qi = boost::spirit::qi;
namespace qr = boost::spirit::repository::qi;
namespace enc = boost::spirit::standard_wide;

template <typename It>
struct Grammar : qi::grammar<It> {

    Grammar() : Grammar::base_type(start) {
        using namespace qi;
        auto kw = qr::distinct(copy(enc::alnum | L'_'));

        start         = skip(enc::space) [function_call];
        function_call = identifier >> L'(' >> L')';
        identifier    = !keyword >> raw[(enc::alpha|L'_') >> *(enc::alnum|L'_')];

        keyword       = kw[ no_case[keywords] ];
        BOOST_SPIRIT_DEBUG_NODES((start)(function_call)(identifier)(keyword));
    }
  private:
    qi::rule<It> start;
    qi::rule<It, enc::space_type> function_call;

    // implicit lexemes
    struct keywords_t : qi::symbols<wchar_t> {
        keywords_t() { 
            this->add
                (L"as")(L"class")(L"dim")(L"else")(L"end")(L"false")
                (L"for")(L"function")(L"if")(L"new")(L"next")(L"sub")
                (L"then")(L"to")(L"true");
        }
    } keywords;
    qi::rule<It, std::string()> identifier, keyword;
};

int main() {
    using It = std::wstring::const_iterator;
    Grammar<It> const g;

    for (std::wstring input : {
            L"foo()",
            L"nextfoo()",
        })
    {
        It f=input.begin(), l=input.end();
        if (parse(f, l, g)) {
            std::wcout << L"Parse success\n";
        } else {
            std::wcout << L"Parse failed\n";
        }

        if (f!=l) {
            std::wcout << L"Remaining unparsed input: '" << std::wstring(f,l) << L"\n";
        }
    }
}

打印

Parse success
Parse success

正如预期的那样