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