strip_comments.input 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (c) 2001-2009 Hartmut Kaiser
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // This example is the equivalent to the following lex program:
  6. //
  7. // %{
  8. // /* INITIAL is the default start state. COMMENT is our new */
  9. // /* state where we remove comments. */
  10. // %}
  11. //
  12. // %s COMMENT
  13. // %%
  14. // <INITIAL>"//".* ;
  15. // <INITIAL>"/*" BEGIN COMMENT;
  16. // <INITIAL>. ECHO;
  17. // <INITIAL>[\n] ECHO;
  18. // <COMMENT>"*/" BEGIN INITIAL;
  19. // <COMMENT>. ;
  20. // <COMMENT>[\n] ;
  21. // %%
  22. //
  23. // main()
  24. // {
  25. // yylex();
  26. // }
  27. //
  28. // Its purpose is to strip comments out of C code.
  29. //
  30. // Additionally this example demonstrates the use of lexer states to structure
  31. // the lexer definition.
  32. // #define BOOST_SPIRIT_LEXERTL_DEBUG
  33. #include <boost/config/warning_disable.hpp>
  34. #include <boost/spirit/include/qi.hpp>
  35. #include <boost/spirit/include/lex_lexer_lexertl.hpp>
  36. #include <boost/spirit/include/phoenix_operator.hpp>
  37. #include <boost/spirit/include/phoenix_container.hpp>
  38. #include <iostream>
  39. #include <string>
  40. #include "example.hpp"
  41. using namespace boost::spirit;
  42. using namespace boost::spirit::qi;
  43. using namespace boost::spirit::lex;
  44. ///////////////////////////////////////////////////////////////////////////////
  45. // Token definition: We use the lexertl based lexer engine as the underlying
  46. // lexer type.
  47. ///////////////////////////////////////////////////////////////////////////////
  48. enum tokenids
  49. {
  50. IDANY = lex::min_token_id + 10
  51. };
  52. template <typename Lexer>
  53. struct strip_comments_tokens : lexer<Lexer>
  54. {
  55. strip_comments_tokens()
  56. {
  57. // define tokens and associate them with the lexer
  58. cppcomment = "//[^\n]*";
  59. ccomment = "/\\*";
  60. endcomment = "\\*/";
  61. // The following tokens are associated with the default lexer state
  62. // (the "INITIAL" state). Specifying 'INITIAL' as a lexer state is
  63. // strictly optional.
  64. this->self.add
  65. (cppcomment) // no explicit token id is associated
  66. (ccomment)
  67. (".", IDANY) // IDANY is the token id associated with this token
  68. // definition
  69. ;
  70. // The following tokens are associated with the lexer state "COMMENT".
  71. // We switch lexer states from inside the parsing process using the
  72. // in_state("COMMENT")[] parser component as shown below.
  73. this->self("COMMENT").add
  74. (endcomment)
  75. (".", IDANY)
  76. ;
  77. }
  78. token_def<> cppcomment, ccomment, endcomment;
  79. };
  80. ///////////////////////////////////////////////////////////////////////////////
  81. // Grammar definition
  82. ///////////////////////////////////////////////////////////////////////////////
  83. template <typename Iterator>
  84. struct strip_comments_grammar : grammar<Iterator>
  85. {
  86. template <typename TokenDef>
  87. strip_comments_grammar(TokenDef const& tok)
  88. : strip_comments_grammar::base_type(start)
  89. {
  90. // The in_state("COMMENT")[...] parser component switches the lexer
  91. // state to be 'COMMENT' during the matching of the embedded parser.
  92. start = *( tok.ccomment
  93. >> in_state("COMMENT")
  94. [
  95. // the lexer is in the 'COMMENT' state during
  96. // matching of the following parser components
  97. *token(IDANY) >> tok.endcomment
  98. ]
  99. | tok.cppcomment
  100. | token(IDANY) [ std::cout << _1 ]
  101. )
  102. ;
  103. }
  104. rule<Iterator> start;
  105. };
  106. ///////////////////////////////////////////////////////////////////////////////
  107. int main(int argc, char* argv[])
  108. {
  109. // iterator type used to expose the underlying input stream
  110. typedef std::string::iterator base_iterator_type;
  111. // lexer type
  112. typedef lexertl::lexer<lexertl::token<base_iterator_type> > lexer_type;
  113. // iterator type exposed by the lexer
  114. typedef strip_comments_tokens<lexer_type>::iterator_type iterator_type;
  115. // now we use the types defined above to create the lexer and grammar
  116. // object instances needed to invoke the parsing process
  117. strip_comments_tokens<lexer_type> strip_comments; // Our lexer
  118. strip_comments_grammar<iterator_type> g (strip_comments); // Our grammar
  119. // Parsing is done based on the token stream, not the character
  120. // stream read from the input.
  121. std::string str (read_from_file(1 == argc ? "strip_comments.input" : argv[1]));
  122. base_iterator_type first = str.begin();
  123. bool r = tokenize_and_parse(first, str.end(), strip_comments, g);
  124. if (r) {
  125. std::cout << "-------------------------\n";
  126. std::cout << "Parsing succeeded\n";
  127. std::cout << "-------------------------\n";
  128. }
  129. else {
  130. std::string rest(first, str.end());
  131. std::cout << "-------------------------\n";
  132. std::cout << "Parsing failed\n";
  133. std::cout << "stopped at: \"" << rest << "\"\n";
  134. std::cout << "-------------------------\n";
  135. }
  136. std::cout << "Bye... :-) \n\n";
  137. return 0;
  138. }