]> git.saurik.com Git - bison.git/blame - examples/variant.yy
maint: minor simplification
[bison.git] / examples / variant.yy
CommitLineData
3272a725
AD
1/*
2 Copyright (C) 2008-2012 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.
16*/
17
5ab8c47b 18%debug
5de9c593 19%skeleton "lalr1.cc"
5ab8c47b 20%defines
72c12bfa 21%define parse.assert
5ab8c47b 22%define variant
0078681b 23%define lex_symbol
882f02ed 24%locations
5ab8c47b
AD
25
26%code requires // *.hh
27{
9718cfa9 28#include <list>
5ab8c47b 29#include <string>
9718cfa9 30typedef std::list<std::string> strings_type;
5ab8c47b
AD
31}
32
33%code // *.cc
34{
35#include <algorithm>
36#include <iostream>
9718cfa9 37#include <iterator>
5ab8c47b
AD
38#include <sstream>
39
9718cfa9 40 // Prototype of the yylex function providing subsequent tokens.
0078681b 41 static yy::parser::symbol_type yylex ();
9718cfa9
AD
42
43 // Printing a list of strings.
44 // Koening look up will look into std, since that's an std::list.
45 namespace std
46 {
47 std::ostream&
0078681b 48 operator<< (std::ostream& o, const strings_type& s)
9718cfa9 49 {
0078681b
AD
50 std::copy (s.begin (), s.end (),
51 std::ostream_iterator<strings_type::value_type> (o, "\n"));
9718cfa9
AD
52 return o;
53 }
54 }
55
56 // Conversion to string.
57 template <typename T>
58 inline
59 std::string
60 string_cast (const T& t)
61 {
62 std::ostringstream o;
63 o << t;
0078681b 64 return o.str ();
9718cfa9 65 }
5ab8c47b
AD
66}
67
cb823b6f 68%token <::std::string> TEXT;
9718cfa9 69%token <int> NUMBER;
cb823b6f
AD
70%printer { debug_stream () << $$; }
71 <int> <::std::string> <::std::list<std::string>>;
9718cfa9 72%token END_OF_FILE 0;
5ab8c47b 73
cb823b6f
AD
74%type <::std::string> item;
75%type <::std::list<std::string>> list;
5ab8c47b
AD
76
77%%
78
79result:
0078681b 80 list { std::cout << $1 << std::endl; }
9718cfa9
AD
81;
82
83list:
84 /* nothing */ { /* Generates an empty string list */ }
85| list item { std::swap ($$, $1); $$.push_back ($2); }
5ab8c47b
AD
86;
87
9718cfa9
AD
88item:
89 TEXT { std::swap ($$, $1); }
90| NUMBER { $$ = string_cast ($1); }
5ab8c47b
AD
91;
92%%
93
94// The yylex function providing subsequent tokens:
9718cfa9
AD
95// TEXT "I have three numbers for you:"
96// NUMBER 1
97// NUMBER 2
98// NUMBER 3
99// TEXT " and that's all!"
5ab8c47b
AD
100// END_OF_FILE
101
102static
0078681b
AD
103yy::parser::symbol_type
104yylex ()
5ab8c47b 105{
0078681b 106 static int stage = -1;
882f02ed
AD
107 ++stage;
108 yy::parser::location_type loc(0, stage + 1, stage + 1);
109 switch (stage)
5ab8c47b
AD
110 {
111 case 0:
882f02ed 112 return yy::parser::make_TEXT ("I have three numbers for you.", loc);
5ab8c47b
AD
113 case 1:
114 case 2:
115 case 3:
882f02ed 116 return yy::parser::make_NUMBER (stage, loc);
5ab8c47b 117 case 4:
882f02ed 118 return yy::parser::make_TEXT ("And that's all!", loc);
5ab8c47b 119 default:
882f02ed 120 return yy::parser::make_END_OF_FILE (loc);
5ab8c47b 121 }
5ab8c47b
AD
122}
123
124// Mandatory error function
125void
882f02ed 126yy::parser::error (const yy::parser::location_type& loc, const std::string& msg)
5ab8c47b 127{
882f02ed 128 std::cerr << loc << ": " << msg << std::endl;
5ab8c47b
AD
129}
130
131int
0078681b 132main ()
5ab8c47b
AD
133{
134 yy::parser p;
9718cfa9 135 p.set_debug_level (!!getenv ("YYDEBUG"));
0078681b 136 return p.parse ();
5ab8c47b
AD
137}
138
139// Local Variables:
140// mode: C++
141// End: