📄 strip_comments.input
字号:
// Copyright (c) 2001-2007 Hartmut Kaiser// // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)// This example is the equivalent to the following lex program://// %{// /* INITIAL is the default start state. COMMENT is our new */// /* state where we remove comments. */// %}// // %s COMMENT// %%// <INITIAL>"//".* ;// <INITIAL>"/*" BEGIN COMMENT; // <INITIAL>. ECHO;// <INITIAL>[\n] ECHO;// <COMMENT>"*/" BEGIN INITIAL;// <COMMENT>. ;// <COMMENT>[\n] ;// %%// // main() // {// yylex();// }//// Its purpose is to strip comments out of C code.// #define BOOST_SPIRIT_LEXERTL_DEBUG#include <boost/spirit/qi.hpp>#include <boost/spirit/lex/lexer_lexertl.hpp>#include <boost/spirit/phoenix/operator.hpp>#include <boost/spirit/phoenix/container.hpp>#include <iostream>#include <string>#include "example.hpp"using namespace boost::spirit;using namespace boost::spirit::qi;using namespace boost::spirit::lex;using namespace boost::spirit::arg_names;///////////////////////////////////////////////////////////////////////////////// Token definition: We use the lexertl based lexer engine as the underlying // lexer type.///////////////////////////////////////////////////////////////////////////////enum { IDANY = lex::min_token_id + 10};template <typename BaseIterator>struct strip_comments_tokens : lexer_def<lexertl_lexer<BaseIterator> >{ template <typename Self> void def (Self& self) { // define tokens and associate them with the lexer cppcomment = "//.*\n"; ccomment = "/\\*"; endcomment = "\\*/"; self.add (cppcomment) (ccomment) (".", IDANY) ; self("COMMENT").add (endcomment) (".", IDANY) ; } token_def<> cppcomment, ccomment, endcomment;};///////////////////////////////////////////////////////////////////////////////// Grammar definition///////////////////////////////////////////////////////////////////////////////template <typename Iterator>struct strip_comments_grammar : grammar_def<Iterator>{ template <typename TokenDef> strip_comments_grammar(TokenDef const& tok) { start = *( tok.ccomment >> in_state("COMMENT") [ *token(IDANY) >> tok.endcomment ] | tok.cppcomment | token(IDANY) ) ; } rule<Iterator> start;};///////////////////////////////////////////////////////////////////////////////int main(int argc, char* argv[]){ // iterator type used to expose the underlying input stream typedef std::string::const_iterator base_iterator_type; // iterator type exposed by the lexer typedef lexer_iterator<strip_comments_tokens<base_iterator_type> >::type iterator_type; // now we use the types defined above to create the lexer and grammar // object instances needed to invoke the parsing process strip_comments_tokens<base_iterator_type> strip_comments; // Our token definition strip_comments_grammar<iterator_type> def (strip_comments); // Our grammar definition // Parsing is done based on the the token stream, not the character // stream read from the input. std::string str (read_from_file(1 == argc ? "strip_comments.input" : argv[1])); base_iterator_type first = str.begin(); base_iterator_type end = str.end(); bool r = lex_and_parse(first, end, strip_comments, make_grammar(def)); if (r) { std::cout << "-------------------------\n"; std::cout << "Parsing succeeded\n"; std::cout << "-------------------------\n"; } else { std::string rest(first, 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;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -