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