]>
Commit | Line | Data |
---|---|---|
5ab8c47b AD |
1 | /* Test file for C++ parsers using variants. |
2 | Based on an example by Michiel De Wilde <mdewilde.agilent@gmail.com>. */ | |
5ab8c47b | 3 | %debug |
9718cfa9 | 4 | %skeleton "lalr1-fusion.cc" |
5ab8c47b AD |
5 | %defines |
6 | %define variant | |
7 | ||
8 | %code requires // *.hh | |
9 | { | |
9718cfa9 | 10 | #include <list> |
5ab8c47b | 11 | #include <string> |
9718cfa9 | 12 | typedef std::list<std::string> strings_type; |
5ab8c47b AD |
13 | } |
14 | ||
15 | %code // *.cc | |
16 | { | |
17 | #include <algorithm> | |
18 | #include <iostream> | |
9718cfa9 | 19 | #include <iterator> |
5ab8c47b AD |
20 | #include <sstream> |
21 | ||
9718cfa9 AD |
22 | // Prototype of the yylex function providing subsequent tokens. |
23 | static yy::parser::token_type yylex(yy::parser::semantic_type* yylval); | |
24 | ||
25 | // Printing a list of strings. | |
26 | // Koening look up will look into std, since that's an std::list. | |
27 | namespace std | |
28 | { | |
29 | std::ostream& | |
30 | operator<<(std::ostream& o, const strings_type& s) | |
31 | { | |
32 | std::copy(s.begin(), s.end(), | |
33 | std::ostream_iterator<strings_type::value_type>(o, "\n")); | |
34 | return o; | |
35 | } | |
36 | } | |
37 | ||
38 | // Conversion to string. | |
39 | template <typename T> | |
40 | inline | |
41 | std::string | |
42 | string_cast (const T& t) | |
43 | { | |
44 | std::ostringstream o; | |
45 | o << t; | |
46 | return o.str(); | |
47 | } | |
5ab8c47b AD |
48 | } |
49 | ||
9718cfa9 AD |
50 | %token <std::string> TEXT; |
51 | %token <int> NUMBER; | |
52 | %printer { debug_stream () << $$; } <int> <std::string> <strings_type>; | |
53 | %token END_OF_FILE 0; | |
5ab8c47b | 54 | |
9718cfa9 AD |
55 | %type <std::string> item; |
56 | %type <strings_type> list; | |
5ab8c47b AD |
57 | |
58 | %% | |
59 | ||
60 | result: | |
f6038cb8 | 61 | list { std::cout << $1 << std::endl; } |
9718cfa9 AD |
62 | ; |
63 | ||
64 | list: | |
65 | /* nothing */ { /* Generates an empty string list */ } | |
66 | | list item { std::swap ($$, $1); $$.push_back ($2); } | |
5ab8c47b AD |
67 | ; |
68 | ||
9718cfa9 AD |
69 | item: |
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 | ||
83 | static | |
84 | yy::parser::token_type | |
9718cfa9 | 85 | yylex (yy::parser::semantic_type* yylval) |
5ab8c47b AD |
86 | { |
87 | static int stage = 0; | |
88 | yy::parser::token_type result; | |
89 | ||
90 | switch (stage) | |
91 | { | |
92 | case 0: | |
9718cfa9 AD |
93 | yylval->build<std::string> () = |
94 | std::string ("I have three numbers for you."); | |
5ab8c47b AD |
95 | result = yy::parser::token::TEXT; |
96 | break; | |
97 | case 1: | |
98 | case 2: | |
99 | case 3: | |
9718cfa9 | 100 | yylval->build<int> () = stage; |
5ab8c47b AD |
101 | result = yy::parser::token::NUMBER; |
102 | break; | |
103 | case 4: | |
9718cfa9 | 104 | yylval->build<std::string> () = std::string ("And that's all!"); |
5ab8c47b AD |
105 | result = yy::parser::token::TEXT; |
106 | break; | |
107 | default: | |
108 | result = yy::parser::token::END_OF_FILE; | |
109 | break; | |
110 | } | |
111 | ||
112 | stage++; | |
113 | return result; | |
114 | } | |
115 | ||
116 | // Mandatory error function | |
117 | void | |
9718cfa9 AD |
118 | yy::parser::error (const yy::parser::location_type& yylloc, |
119 | const std::string& message) | |
5ab8c47b AD |
120 | { |
121 | std::cerr << yylloc << ": " << message << std::endl; | |
122 | } | |
123 | ||
124 | int | |
9718cfa9 | 125 | main (int argc, char *argv[]) |
5ab8c47b AD |
126 | { |
127 | yy::parser p; | |
9718cfa9 AD |
128 | p.set_debug_level (!!getenv ("YYDEBUG")); |
129 | p.parse (); | |
5ab8c47b AD |
130 | } |
131 | ||
132 | // Local Variables: | |
133 | // mode: C++ | |
134 | // End: |