]> git.saurik.com Git - bison.git/blame - examples/variant.yy
diagnostics: improve -fcaret for list of accepted values
[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
e36ec1f4 21%define api.token.constructor
72c12bfa 22%define parse.assert
5ab8c47b 23%define variant
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.
0b328702
AD
41 namespace yy
42 {
43 static parser::symbol_type yylex ();
44 }
9718cfa9
AD
45
46 // Printing a list of strings.
47 // Koening look up will look into std, since that's an std::list.
48 namespace std
49 {
50 std::ostream&
0078681b 51 operator<< (std::ostream& o, const strings_type& s)
9718cfa9 52 {
0078681b
AD
53 std::copy (s.begin (), s.end (),
54 std::ostream_iterator<strings_type::value_type> (o, "\n"));
9718cfa9
AD
55 return o;
56 }
57 }
58
59 // Conversion to string.
60 template <typename T>
61 inline
62 std::string
63 string_cast (const T& t)
64 {
65 std::ostringstream o;
66 o << t;
0078681b 67 return o.str ();
9718cfa9 68 }
5ab8c47b
AD
69}
70
cb823b6f 71%token <::std::string> TEXT;
9718cfa9 72%token <int> NUMBER;
cb823b6f
AD
73%printer { debug_stream () << $$; }
74 <int> <::std::string> <::std::list<std::string>>;
9718cfa9 75%token END_OF_FILE 0;
5ab8c47b 76
cb823b6f
AD
77%type <::std::string> item;
78%type <::std::list<std::string>> list;
5ab8c47b
AD
79
80%%
81
82result:
0078681b 83 list { std::cout << $1 << std::endl; }
9718cfa9
AD
84;
85
86list:
87 /* nothing */ { /* Generates an empty string list */ }
88| list item { std::swap ($$, $1); $$.push_back ($2); }
5ab8c47b
AD
89;
90
9718cfa9
AD
91item:
92 TEXT { std::swap ($$, $1); }
93| NUMBER { $$ = string_cast ($1); }
5ab8c47b
AD
94;
95%%
96
0b328702 97namespace yy
5ab8c47b 98{
0b328702
AD
99 // The yylex function providing subsequent tokens:
100 // TEXT "I have three numbers for you."
101 // NUMBER 1
102 // NUMBER 2
103 // NUMBER 3
104 // TEXT "And that's all!"
105 // END_OF_FILE
106
107 static
108 parser::symbol_type
109 yylex ()
5ab8c47b 110 {
0b328702
AD
111 static int stage = -1;
112 ++stage;
113 parser::location_type loc(0, stage + 1, stage + 1);
114 switch (stage)
115 {
116 case 0:
117 return parser::make_TEXT ("I have three numbers for you.", loc);
118 case 1:
119 case 2:
120 case 3:
121 return parser::make_NUMBER (stage, loc);
122 case 4:
123 return parser::make_TEXT ("And that's all!", loc);
124 default:
125 return parser::make_END_OF_FILE (loc);
126 }
5ab8c47b 127 }
5ab8c47b 128
0b328702
AD
129 // Mandatory error function
130 void
131 parser::error (const parser::location_type& loc, const std::string& msg)
132 {
133 std::cerr << loc << ": " << msg << std::endl;
134 }
5ab8c47b
AD
135}
136
137int
0078681b 138main ()
5ab8c47b
AD
139{
140 yy::parser p;
9718cfa9 141 p.set_debug_level (!!getenv ("YYDEBUG"));
0078681b 142 return p.parse ();
5ab8c47b
AD
143}
144
145// Local Variables:
146// mode: C++
147// End: