[英]Boost spirit take away keyword and ignore skipper
這只是使用表達式的語法的一小部分。
prefix =
(lit(L"not") >> prefix)
|(lit('-') >> prefix)
| postfix
;
后綴內的某種方式我有name_pure采取一個標識符..
name_pure = lexeme[+(boost::spirit::standard_wide::alpha | '_') >> *(boost::spirit::standard_wide::alnum | '_')];
到目前為止,一切都很好。 可以寫像
a=not b
但是,如果我開始不使用作為這樣一個名稱前綴
a=not notvarname
我從AST得到一個解析器輸出,看起來像這樣
a=not not varname
這意味着not不能用作前綴規則,而不能用作Name
與規則Name_pure的規則。
從我的角度來看,似乎船長未正確參與。
這是我的船長
template<typename Iterator>
struct eol_skipper : public qi::grammar<Iterator> {
eol_skipper() : eol_skipper::base_type(skip)
{
using qi::eol;
using qi::lit;
using qi::char_;
skip = ascii::space -eol;
}
qi::rule<Iterator> skip;
};
像上次一樣,我認為船長不是您的問題。
關於船長所做的假設,也許。
space - eol
只是blank
。 "not"
,則需要確保您在單詞邊界上: 防止Boost Spirit Symbol解析器過早接受關鍵字,或者如何 在Boost Spirit中 正確解析保留的單詞 我會寫一些更具描述性的規則(例如eol_skipper
建議它跳過eol
,但這恰恰是它不跳過嗎?)。
using Skipper = qi::blank_type;
然后,通過從聲明中刪除船長,使identifier
規則(pure_name?)成為隱式lexeme
:
private:
qi::rule<Iterator, Ast::AssignmentStatement()> start;
qi::rule<Iterator, Ast::AssignmentStatement(), Skipper> assignment;
qi::rule<Iterator, Ast::Expr(), Skipper> expr;
qi::rule<Iterator, Ast::Negated(), Skipper> negation;
// implicit lexemes
qi::rule<Iterator, Ast::Identifier()> identifier;
最后,使用!p
parser指令來斷言在關鍵字/標識符邊界上not
匹配:
negation
= lexeme [(lit("not") | '0') >> !(alnum|'_')] >> expr
;
#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iostream>
namespace qi = boost::spirit::qi;
namespace Ast {
using Identifier = std::string;
struct Negated;
using Expr = boost::variant<Identifier, boost::recursive_wrapper<Negated> >;
struct Negated {
Expr expr;
};
struct AssignmentStatement {
Identifier lhs;
Expr rhs;
};
}
BOOST_FUSION_ADAPT_STRUCT(Ast::Negated, expr)
BOOST_FUSION_ADAPT_STRUCT(Ast::AssignmentStatement, lhs, rhs)
template <typename Iterator> struct parser : qi::grammar<Iterator, Ast::AssignmentStatement()> {
using Skipper = qi::blank_type;
parser() : parser::base_type(start) {
using namespace qi;
start = skip(blank) [ assignment ];
assignment = identifier >> '=' >> expr;
expr = negation | identifier;
negation
= lexeme [(lit("not") | '0') >> !(alnum|'_')] >> expr
;
identifier = char_("a-zA-Z_") >> *char_("a-zA-Z0-9_");
// or:
identifier = raw [ +(alpha | '_') >> *(alnum | '_') ];
BOOST_SPIRIT_DEBUG_NODES((start)(expr)(assignment)(identifier)(negation))
}
private:
qi::rule<Iterator, Ast::AssignmentStatement()> start;
qi::rule<Iterator, Ast::AssignmentStatement(), Skipper> assignment;
qi::rule<Iterator, Ast::Expr(), Skipper> expr;
qi::rule<Iterator, Ast::Negated(), Skipper> negation;
// implicit lexemes
qi::rule<Iterator, Ast::Identifier()> identifier;
};
namespace Ast {
std::ostream& operator<<(std::ostream& os, Negated const& o) { return os << "NOT[" << o.expr << "]"; }
std::ostream& operator<<(std::ostream& os, AssignmentStatement const& a) { return os << a.lhs << " = " << a.rhs; }
}
int main() {
using It = std::string::const_iterator;
for (std::string const input : {
"a=not _b",
"a=not not_var_name",
})
{
It f = input.begin(), l = input.end();
Ast::AssignmentStatement assignment;
if (parse(f, l, parser<It>{}, assignment))
std::cout << "Parsed " << assignment << "\n";
else
std::cout << "Parse failed\n";
if (f!=l)
std::cout << "Remaining unparsed input: '" << std::string(f,l) << "'\n";
}
}
打印
Parsed a = NOT[_b]
Parsed a = NOT[not_var_name]
注意 ,下次定義
BOOST_SPIRIT_DEBUG
如何為您提供調試輸出,以防您想對規則進行故障排除:
<start>
<try>a=not b</try>
<assignment>
<try>a=not b</try>
<identifier>
<try>a=not b</try>
<success>=not b</success>
<attributes>[[a]]</attributes>
</identifier>
<expr>
<try>not b</try>
<negation>
<try>not b</try>
<expr>
<try> b</try>
<negation>
<try> b</try>
<fail/>
</negation>
<identifier>
<try>b</try>
<success></success>
<attributes>[[b]]</attributes>
</identifier>
<success></success>
<attributes>[[b]]</attributes>
</expr>
<success></success>
<attributes>[[[b]]]</attributes>
</negation>
<success></success>
<attributes>[[[b]]]</attributes>
</expr>
<success></success>
<attributes>[[[a], [[b]]]]</attributes>
</assignment>
<success></success>
<attributes>[[[a], [[b]]]]</attributes>
</start>
Parsed a = NOT[b]
<start>
<try>a=not notvarname</try>
<assignment>
<try>a=not notvarname</try>
<identifier>
<try>a=not notvarname</try>
<success>=not notvarname</success>
<attributes>[[a]]</attributes>
</identifier>
<expr>
<try>not notvarname</try>
<negation>
<try>not notvarname</try>
<expr>
<try> notvarname</try>
<negation>
<try> notvarname</try>
<fail/>
</negation>
<identifier>
<try>notvarname</try>
<success></success>
<attributes>[[n, o, t, v, a, r, n, a, m, e]]</attributes>
</identifier>
<success></success>
<attributes>[[n, o, t, v, a, r, n, a, m, e]]</attributes>
</expr>
<success></success>
<attributes>[[[n, o, t, v, a, r, n, a, m, e]]]</attributes>
</negation>
<success></success>
<attributes>[[[n, o, t, v, a, r, n, a, m, e]]]</attributes>
</expr>
<success></success>
<attributes>[[[a], [[n, o, t, v, a, r, n, a, m, e]]]]</attributes>
</assignment>
<success></success>
<attributes>[[[a], [[n, o, t, v, a, r, n, a, m, e]]]]</attributes>
</start>
Parsed a = NOT[notvarname]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.