]>
Commit | Line | Data |
---|---|---|
3272a725 | 1 | /* |
fc51acdd | 2 | Copyright (C) 2008-2014 Free Software Foundation, Inc. |
3272a725 AD |
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 |
b51cf830 | 19 | %language "c++" |
5ab8c47b | 20 | %defines |
e36ec1f4 | 21 | %define api.token.constructor |
ae8880de | 22 | %define api.value.type variant |
72c12bfa | 23 | %define parse.assert |
882f02ed | 24 | %locations |
5ab8c47b AD |
25 | |
26 | %code requires // *.hh | |
27 | { | |
9718cfa9 | 28 | #include <list> |
5ab8c47b | 29 | #include <string> |
9718cfa9 | 30 | typedef 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& | |
b51cf830 | 51 | operator<< (std::ostream& o, const strings_type& ss) |
9718cfa9 | 52 | { |
b51cf830 AD |
53 | o << "(" << &ss << ") {"; |
54 | const char *sep = ""; | |
55 | for (strings_type::const_iterator i = ss.begin(), end = ss.end(); | |
56 | i != end; ++i) | |
57 | { | |
58 | o << sep << *i; | |
59 | sep = ", "; | |
60 | } | |
61 | return o << "}"; | |
9718cfa9 AD |
62 | } |
63 | } | |
64 | ||
65 | // Conversion to string. | |
66 | template <typename T> | |
67 | inline | |
68 | std::string | |
69 | string_cast (const T& t) | |
70 | { | |
71 | std::ostringstream o; | |
72 | o << t; | |
0078681b | 73 | return o.str (); |
9718cfa9 | 74 | } |
5ab8c47b AD |
75 | } |
76 | ||
cb823b6f | 77 | %token <::std::string> TEXT; |
9718cfa9 | 78 | %token <int> NUMBER; |
304b384e | 79 | %printer { yyoutput << $$; } <*>; |
9718cfa9 | 80 | %token END_OF_FILE 0; |
5ab8c47b | 81 | |
cb823b6f AD |
82 | %type <::std::string> item; |
83 | %type <::std::list<std::string>> list; | |
5ab8c47b AD |
84 | |
85 | %% | |
86 | ||
87 | result: | |
0078681b | 88 | list { std::cout << $1 << std::endl; } |
9718cfa9 AD |
89 | ; |
90 | ||
91 | list: | |
92 | /* nothing */ { /* Generates an empty string list */ } | |
93 | | list item { std::swap ($$, $1); $$.push_back ($2); } | |
5ab8c47b AD |
94 | ; |
95 | ||
9718cfa9 AD |
96 | item: |
97 | TEXT { std::swap ($$, $1); } | |
98 | | NUMBER { $$ = string_cast ($1); } | |
5ab8c47b AD |
99 | ; |
100 | %% | |
101 | ||
0b328702 | 102 | namespace yy |
5ab8c47b | 103 | { |
0b328702 AD |
104 | // The yylex function providing subsequent tokens: |
105 | // TEXT "I have three numbers for you." | |
106 | // NUMBER 1 | |
107 | // NUMBER 2 | |
108 | // NUMBER 3 | |
109 | // TEXT "And that's all!" | |
110 | // END_OF_FILE | |
111 | ||
112 | static | |
113 | parser::symbol_type | |
114 | yylex () | |
5ab8c47b | 115 | { |
0b328702 AD |
116 | static int stage = -1; |
117 | ++stage; | |
118 | parser::location_type loc(0, stage + 1, stage + 1); | |
119 | switch (stage) | |
120 | { | |
121 | case 0: | |
122 | return parser::make_TEXT ("I have three numbers for you.", loc); | |
123 | case 1: | |
124 | case 2: | |
125 | case 3: | |
126 | return parser::make_NUMBER (stage, loc); | |
127 | case 4: | |
128 | return parser::make_TEXT ("And that's all!", loc); | |
129 | default: | |
130 | return parser::make_END_OF_FILE (loc); | |
131 | } | |
5ab8c47b | 132 | } |
5ab8c47b | 133 | |
0b328702 AD |
134 | // Mandatory error function |
135 | void | |
136 | parser::error (const parser::location_type& loc, const std::string& msg) | |
137 | { | |
138 | std::cerr << loc << ": " << msg << std::endl; | |
139 | } | |
5ab8c47b AD |
140 | } |
141 | ||
142 | int | |
0078681b | 143 | main () |
5ab8c47b AD |
144 | { |
145 | yy::parser p; | |
9718cfa9 | 146 | p.set_debug_level (!!getenv ("YYDEBUG")); |
0078681b | 147 | return p.parse (); |
5ab8c47b AD |
148 | } |
149 | ||
150 | // Local Variables: | |
151 | // mode: C++ | |
152 | // End: |