Tôi cố gắng triển khai một Lexer cho một chút ngôn ngữ lập trình với Boost Spirit.Tôi không thể nhận được giá trị chuỗi của mã thông báo
tôi phải nhận được giá trị của một token và tôi nhận được một ngoại lệ bad_get:
chấm dứt gọi là sau khi ném một thể hiện của 'đẩy mạnh :: bad_get'
gì(): tăng :: bad_get: giá trị không được sử dụng tăng :: nhận Đã hủy bỏ
tôi có được ngoại lệ này khi thực hiện:
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;
l My exer được định nghĩa như sau:
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;
};
Có cách nào khác để nhận giá trị của mã thông báo không?
về đọc lại, tôi nhận thấy rằng bạn chỉ định 'lex :: omit' như các loại thuộc tính tượng trưng. Các mã thông báo này sẽ không hiển thị dữ liệu _any_ value (thậm chí không phải cặp lặp). Đây có thể là vấn đề của bạn. Nếu không, tôi chân thành khuyên bạn nên phân tích cú pháp bằng cách sử dụng Qi trên các trình vòng lặp mã thông báo: tận dụng tối đa cả hai thế giới. – sehe
Tôi đã xác minh và đáng tiếc đây không phải là vấn đề. Tôi chỉ sử dụng boost :: nhận được trên một mã thông báo của loại tốt và cần có giá trị. –