glr2.cc: put glr_state_set and glr_stack_item in unnamed namespace
[bison.git] / examples / c++ / variant-11.yy
blobedbcff8709b6a32e5a651123f23515c81598933f
1 /*
2   Copyright (C) 2008-2015, 2018-2021 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 <https://www.gnu.org/licenses/>.
18 %require "3.2"
19 %debug
20 %language "c++"
21 %define api.token.constructor
22 %define api.value.type variant
23 %define api.value.automove
24 %define api.location.file none
25 %define parse.assert
26 %locations
28 %code requires // *.hh
30 #include <memory> // std::unique_ptr
31 #include <string>
32 #include <vector>
34   using string_uptr = std::unique_ptr<std::string>;
35   using string_uptrs = std::vector<string_uptr>;
38 %code // *.cc
40 #include <climits>  // INT_MIN, INT_MAX
41 #include <iostream>
42 #include <sstream>
44   namespace yy
45   {
46     // Prototype of the yylex function providing subsequent tokens.
47     static parser::symbol_type yylex ();
49     // Print a vector of strings.
50     std::ostream&
51     operator<< (std::ostream& o, const string_uptrs& ss)
52     {
53       o << '{';
54       const char *sep = "";
55       for (const auto& s: ss)
56         {
57           o << sep << *s;
58           sep = ", ";
59         }
60       return o << '}';
61     }
62   }
64   template <typename... Args>
65   string_uptr
66   make_string_uptr (Args&&... args)
67   {
68     // std::make_unique is C++14.
69     return string_uptr (new std::string{std::forward<Args> (args)...});
70   }
73 %token <string_uptr> TEXT;
74 %token <int> NUMBER;
75 %printer { yyo << '(' << &$$ << ") " << $$; } <*>;
76 %printer { yyo << *$$; } <string_uptr>;
77 %token END_OF_FILE 0;
79 %type <string_uptr> item;
80 %type <string_uptrs> list;
84 result:
85   list  { std::cout << $1 << '\n'; }
88 list:
89   %empty     { /* Generates an empty string list */ }
90 | list item  { $$ = $1; $$.emplace_back ($2); }
93 item:
94   TEXT
95 | NUMBER  { $$ = make_string_uptr (std::to_string ($1)); }
99 // The last number return by the scanner is max - 1.
100 int max = 4;
102 namespace yy
104   // The yylex function providing subsequent tokens:
105   // TEXT         "I have three numbers for you."
106   // NUMBER       1
107   // NUMBER       2
108   // NUMBER       ...
109   // NUMBER       max - 1
110   // TEXT         "And that's all!"
111   // END_OF_FILE
113   static
114   parser::symbol_type
115   yylex ()
116   {
117     static int count = 0;
118     const int stage = count;
119     ++count;
120     auto loc = parser::location_type{nullptr, stage + 1, stage + 1};
121     if (stage == 0)
122       return parser::make_TEXT (make_string_uptr ("I have numbers for you."), std::move (loc));
123     else if (stage < max)
124       return parser::make_NUMBER (stage, std::move (loc));
125     else if (stage == max)
126       return parser::make_TEXT (make_string_uptr ("And that's all!"), std::move (loc));
127     else
128       return parser::make_END_OF_FILE (std::move (loc));
129   }
131   // Mandatory error function
132   void
133   parser::error (const parser::location_type& loc, const std::string& msg)
134   {
135     std::cerr << loc << ": " << msg << '\n';
136   }
140 main (int argc, const char *argv[])
142   if (2 <= argc && isdigit (static_cast<unsigned char> (*argv[1])))
143     {
144       auto maxl = strtol (argv[1], nullptr, 10);
145       max = INT_MIN <= maxl && maxl <= INT_MAX ? int(maxl) : 4;
146     }
147   auto&& p = yy::parser{};
148   p.set_debug_level (!!getenv ("YYDEBUG"));
149   return p.parse ();
152 // Local Variables:
153 // mode: C++
154 // End: