2 Copyright (C) 2008-2012 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 %define api.token.constructor
26 %code requires // *.hh
30 typedef std::list<std::string> strings_type;
40 // Prototype of the yylex function providing subsequent tokens.
43 static parser::symbol_type yylex ();
46 // Printing a list of strings.
47 // Koening look up will look into std, since that's an std::list.
51 operator<< (std::ostream& o, const strings_type& s)
53 std::copy (s.begin (), s.end (),
54 std::ostream_iterator<strings_type::value_type> (o, "\n"));
59 // Conversion to string.
63 string_cast (const T& t)
71 %token <::std::string> TEXT;
73 %printer { debug_stream () << $$; }
74 <int> <::std::string> <::std::list<std::string>>;
77 %type <::std::string> item;
78 %type <::std::list<std::string>> list;
83 list { std::cout << $1 << std::endl; }
87 /* nothing */ { /* Generates an empty string list */ }
88 | list item { std::swap ($$, $1); $$.push_back ($2); }
92 TEXT { std::swap ($$, $1); }
93 | NUMBER { $$ = string_cast ($1); }
99 // The yylex function providing subsequent tokens:
100 // TEXT "I have three numbers for you."
104 // TEXT "And that's all!"
111 static int stage = -1;
113 parser::location_type loc(0, stage + 1, stage + 1);
117 return parser::make_TEXT ("I have three numbers for you.", loc);
121 return parser::make_NUMBER (stage, loc);
123 return parser::make_TEXT ("And that's all!", loc);
125 return parser::make_END_OF_FILE (loc);
129 // Mandatory error function
131 parser::error (const parser::location_type& loc, const std::string& msg)
133 std::cerr << loc << ": " << msg << std::endl;
141 p.set_debug_level (!!getenv ("YYDEBUG"));