我嘗試使用Boost Spirit來爲一個小編程語言實現Lexer。我無法獲得令牌的字符串值
我必須得到一個代幣的價值,我也得到一個bad_get例外:
終止叫做拋出的 '助推:: bad_get' 的實例後
什麼()的boost :: bad_get:失敗值獲得使用boost ::得到這樣做的時候中止
我得到這個異常:
std::string contents = "void";
base_iterator_type first = contents.begin();
base_iterator_type last = contents.end();
SimpleLexer<lexer_type> lexer;
iter = lexer.begin(first, last);
end = lexer.end();
std::cout << "Value = " << boost::get<std::string>(iter->value()) << std::endl;
我升exer是這樣定義的:
typedef std::string::iterator base_iterator_type;
typedef boost::spirit::lex::lexertl::token<base_iterator_type, boost::mpl::vector<unsigned int, std::string>> Tok;
typedef lex::lexertl::actor_lexer<Tok> lexer_type;
template<typename L>
class SimpleLexer : public lex::lexer<L> {
private:
public:
SimpleLexer() {
keyword_for = "for";
keyword_while = "while";
keyword_if = "if";
keyword_else = "else";
keyword_false = "false";
keyword_true = "true";
keyword_from = "from";
keyword_to = "to";
keyword_foreach = "foreach";
word = "[a-zA-Z]+";
integer = "[0-9]+";
litteral = "...";
left_parenth = '(';
right_parenth = ')';
left_brace = '{';
right_brace = '}';
stop = ';';
comma = ',';
swap = "<>";
assign = '=';
addition = '+';
subtraction = '-';
multiplication = '*';
division = '/';
modulo = '%';
equals = "==";
not_equals = "!=";
greater = '>';
less = '<';
greater_equals = ">=";
less_equals = "<=";
whitespaces = "[ \\t\\n]+";
comments = "\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\/";
//Add keywords
this->self += keyword_for | keyword_while | keyword_true | keyword_false | keyword_if | keyword_else | keyword_from | keyword_to | keyword_foreach;
this->self += integer | litteral | word;
this->self += equals | not_equals | greater_equals | less_equals | greater | less ;
this->self += left_parenth | right_parenth | left_brace | right_brace;
this->self += comma | stop;
this->self += assign | swap | addition | subtraction | multiplication | division | modulo;
//Ignore whitespaces and comments
this->self += whitespaces [lex::_pass = lex::pass_flags::pass_ignore];
this->self += comments [lex::_pass = lex::pass_flags::pass_ignore];
}
lex::token_def<std::string> word, litteral, integer;
lex::token_def<lex::omit> left_parenth, right_parenth, left_brace, right_brace;
lex::token_def<lex::omit> stop, comma;
lex::token_def<lex::omit> assign, swap, addition, subtraction, multiplication, division, modulo;
lex::token_def<lex::omit> equals, not_equals, greater, less, greater_equals, less_equals;
//Keywords
lex::token_def<lex::omit> keyword_if, keyword_else, keyword_for, keyword_while, keyword_from, keyword_to, keyword_foreach;
lex::token_def<lex::omit> keyword_true, keyword_false;
//Ignored tokens
lex::token_def<lex::omit> whitespaces;
lex::token_def<lex::omit> comments;
};
是否有其他方法來獲取令牌的值?
再次閱讀,我注意到你指定'::法作爲omit'令牌屬性類型。這些令牌不會公開_any_值數據(甚至不包括迭代器對)。這可能是你的問題。否則,我衷心推薦在標記迭代器之上使用Qi進行解析:獲得兩全其美的好處。 – sehe
我證實並且可悲的是這不是問題。我只使用boost :: get在一個好類型的令牌上,它應該有這個值。 –