|
Boost-Commit : |
From: joel_at_[hidden]
Date: 2008-04-12 23:17:58
Author: djowel
Date: 2008-04-12 23:17:57 EDT (Sat, 12 Apr 2008)
New Revision: 44361
URL: http://svn.boost.org/trac/boost/changeset/44361
Log:
spirit2 ! :)
Added:
trunk/libs/spirit/example/lex/example1.input (contents, props changed)
trunk/libs/spirit/example/lex/example2.input (contents, props changed)
trunk/libs/spirit/example/lex/example3.input (contents, props changed)
trunk/libs/spirit/example/lex/example4.input (contents, props changed)
trunk/libs/spirit/example/lex/example5.input (contents, props changed)
trunk/libs/spirit/example/lex/example6.input (contents, props changed)
trunk/libs/spirit/example/lex/print_numbers.input (contents, props changed)
trunk/libs/spirit/example/lex/static_lexer/word_count.input (contents, props changed)
trunk/libs/spirit/example/lex/strip_comments.input (contents, props changed)
trunk/libs/spirit/example/lex/word_count.input (contents, props changed)
trunk/libs/spirit/example/lex/word_count_functor.flex (contents, props changed)
trunk/libs/spirit/example/qi/mini_c_samples/1.mini (contents, props changed)
trunk/libs/spirit/example/qi/mini_c_samples/2.mini (contents, props changed)
trunk/libs/spirit/example/qi/mini_c_samples/3.mini (contents, props changed)
Added: trunk/libs/spirit/example/lex/example1.input
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/lex/example1.input 2008-04-12 23:17:57 EDT (Sat, 12 Apr 2008)
@@ -0,0 +1,5 @@
+{
+ hello world,
+ hello world,
+ goodbye
+}
Added: trunk/libs/spirit/example/lex/example2.input
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/lex/example2.input 2008-04-12 23:17:57 EDT (Sat, 12 Apr 2008)
@@ -0,0 +1,7 @@
+Our hiking boots are ready. So, let's pack!
+
+Have you the plane tickets for there and back?
+
+I do, I do. We're all ready to go. Grab my hand and be my beau.
+
+
Added: trunk/libs/spirit/example/lex/example3.input
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/lex/example3.input 2008-04-12 23:17:57 EDT (Sat, 12 Apr 2008)
@@ -0,0 +1,5 @@
+/* the following are couplets */
+(1) (1 2) (1 2 3) ...
+((1)) ((1 2)(3 4)) (((1) (2 3) (1 2 (3) 4))) ...
+(((1))) ...
+
Added: trunk/libs/spirit/example/lex/example4.input
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/lex/example4.input 2008-04-12 23:17:57 EDT (Sat, 12 Apr 2008)
@@ -0,0 +1,17 @@
+/* example4.input */
+{
+
+ if (variable) { a = b ; }
+
+ while (10) {
+
+ if (2) { b = 10 ; }
+ if (x) { c = x ; } else { d = 10 ; }
+
+ }
+
+ variable = 42;
+
+}
+
+
Added: trunk/libs/spirit/example/lex/example5.input
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/lex/example5.input 2008-04-12 23:17:57 EDT (Sat, 12 Apr 2008)
@@ -0,0 +1,16 @@
+/* example5.input */
+{
+
+ if (variable) { a = b ; }
+
+ while (10) {
+
+ if (2) { b = 10 ; }
+ if (x) { c = x ; } else { d = 10 ; }
+
+ }
+
+ variable = 42;
+ if (variable) { a = b ; } else { }
+}
+
Added: trunk/libs/spirit/example/lex/example6.input
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/lex/example6.input 2008-04-12 23:17:57 EDT (Sat, 12 Apr 2008)
@@ -0,0 +1,17 @@
+/* example6.input */
+{
+
+ if (variable) { a = b ; }
+
+ while (10) {
+
+ if (2) { b = 10 ; }
+ if (x) { c = x ; } else { d = 10 ; }
+
+ }
+
+ variable = 42;
+
+}
+
+
Added: trunk/libs/spirit/example/lex/print_numbers.input
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/lex/print_numbers.input 2008-04-12 23:17:57 EDT (Sat, 12 Apr 2008)
@@ -0,0 +1,17 @@
+/* example6.input */
+{
+
+ if (variable) { a = b ; }
+
+ while (10) {
+
+ if (2) { b = 10 ; }
+ if (x) { c = x ; } else { d = 10 ; }
+
+ }
+
+ variable = 42;
+
+}
+
+
Added: trunk/libs/spirit/example/lex/static_lexer/word_count.input
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/lex/static_lexer/word_count.input 2008-04-12 23:17:57 EDT (Sat, 12 Apr 2008)
@@ -0,0 +1,7 @@
+Our hiking boots are ready. So, let's pack!
+
+Have you the plane tickets for there and back?
+
+I do, I do. We're all ready to go. Grab my hand and be my beau.
+
+
Added: trunk/libs/spirit/example/lex/strip_comments.input
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/lex/strip_comments.input 2008-04-12 23:17:57 EDT (Sat, 12 Apr 2008)
@@ -0,0 +1,144 @@
+// 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;
+}
+
+
+
Added: trunk/libs/spirit/example/lex/word_count.input
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/lex/word_count.input 2008-04-12 23:17:57 EDT (Sat, 12 Apr 2008)
@@ -0,0 +1,7 @@
+Our hiking boots are ready. So, let's pack!
+
+Have you the plane tickets for there and back?
+
+I do, I do. We're all ready to go. Grab my hand and be my beau.
+
+
Added: trunk/libs/spirit/example/lex/word_count_functor.flex
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/lex/word_count_functor.flex 2008-04-12 23:17:57 EDT (Sat, 12 Apr 2008)
@@ -0,0 +1,54 @@
+%{
+#include <boost/timer.hpp>
+#if defined(_WIN32)
+ #include <io.h>
+#endif
+ #define ID_WORD 1000
+ #define ID_EOL 1001
+ #define ID_CHAR 1002
+%}
+
+%%
+[^ \t\n]+ { return ID_WORD; }
+\n { return ID_EOL; }
+. { return ID_CHAR; }
+%%
+
+bool count(int tok, int* c, int* w, int* l)
+{
+ switch (tok) {
+ case ID_WORD: ++*w; *c += yyleng; break;
+ case ID_EOL: ++*l; ++*c; break;
+ case ID_CHAR: ++*c; break;
+ default:
+ return false;
+ }
+ return true;
+}
+
+void main(int argc, char* argv[])
+{
+ int tok = EOF;
+ int c = 0, w = 0, l = 0;
+ yyin = fopen(1 == argc ? "word_count.input" : argv[1], "r");
+ if (NULL == yyin) {
+ fprintf(stderr, "Couldn't open input file!\n");
+ exit(-1);
+ }
+
+ boost::timer tim;
+ do {
+ tok = yylex();
+ if (!count(tok, &c, &w, &l))
+ break;
+ } while (EOF != tok);
+ printf("lines: %d, words: %d, characters: %d\n", l, w, c);
+ printf("Time elapsed: %f", tim.elapsed());
+ fclose(yyin);
+}
+
+extern "C" int yywrap()
+{
+ return 1;
+}
+
Added: trunk/libs/spirit/example/qi/mini_c_samples/1.mini
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/mini_c_samples/1.mini 2008-04-12 23:17:57 EDT (Sat, 12 Apr 2008)
@@ -0,0 +1,19 @@
+/* My first mini program */
+
+int pow2(n)
+{
+ int a = 2;
+ int i = 1;
+ while (i < n)
+ {
+ a = a * 2;
+ i = i + 1;
+ }
+ return a;
+}
+
+int main()
+{
+ return pow2(10);
+}
+
Added: trunk/libs/spirit/example/qi/mini_c_samples/2.mini
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/mini_c_samples/2.mini 2008-04-12 23:17:57 EDT (Sat, 12 Apr 2008)
@@ -0,0 +1,15 @@
+/* The factorial */
+
+int factorial(n)
+{
+ if (n <= 0)
+ return 1;
+ else
+ return n * factorial(n-1);
+}
+
+int main(n)
+{
+ return factorial(n);
+}
+
Added: trunk/libs/spirit/example/qi/mini_c_samples/3.mini
==============================================================================
--- (empty file)
+++ trunk/libs/spirit/example/qi/mini_c_samples/3.mini 2008-04-12 23:17:57 EDT (Sat, 12 Apr 2008)
@@ -0,0 +1,17 @@
+/* mini program with syntax error */
+
+int foo(n)
+{
+ int a = 2;
+ if (n << 3) // we don't have << in mini_c
+ {
+ a = 3
+ }
+ return a;
+}
+
+int main()
+{
+ return foo(10);
+}
+
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk