]>
Commit | Line | Data |
---|---|---|
1 | /* Test file for C++ parsers using variants. | |
2 | Based on an example by Michiel De Wilde <mdewilde.agilent@gmail.com>. */ | |
3 | %debug | |
4 | %skeleton "lalr1.cc" | |
5 | %defines | |
6 | %define variant | |
7 | ||
8 | %code requires // *.hh | |
9 | { | |
10 | #include <list> | |
11 | #include <string> | |
12 | typedef std::list<std::string> strings_type; | |
13 | } | |
14 | ||
15 | %code // *.cc | |
16 | { | |
17 | #include <algorithm> | |
18 | #include <iostream> | |
19 | #include <iterator> | |
20 | #include <sstream> | |
21 | ||
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 | } | |
48 | } | |
49 | ||
50 | %token <std::string> TEXT; | |
51 | %token <int> NUMBER; | |
52 | %printer { debug_stream () << $$; } <int> <std::string> <strings_type>; | |
53 | %token END_OF_FILE 0; | |
54 | ||
55 | %type <std::string> item; | |
56 | %type <strings_type> list; | |
57 | ||
58 | %% | |
59 | ||
60 | result: | |
61 | list { std::cout << $1 << std::endl; } | |
62 | ; | |
63 | ||
64 | list: | |
65 | /* nothing */ { /* Generates an empty string list */ } | |
66 | | list item { std::swap ($$, $1); $$.push_back ($2); } | |
67 | ; | |
68 | ||
69 | item: | |
70 | TEXT { std::swap ($$, $1); } | |
71 | | NUMBER { $$ = string_cast ($1); } | |
72 | ; | |
73 | %% | |
74 | ||
75 | // The yylex function providing subsequent tokens: | |
76 | // TEXT "I have three numbers for you:" | |
77 | // NUMBER 1 | |
78 | // NUMBER 2 | |
79 | // NUMBER 3 | |
80 | // TEXT " and that's all!" | |
81 | // END_OF_FILE | |
82 | ||
83 | static | |
84 | yy::parser::token_type | |
85 | yylex (yy::parser::semantic_type* yylval) | |
86 | { | |
87 | static int stage = 0; | |
88 | yy::parser::token_type result; | |
89 | ||
90 | switch (stage) | |
91 | { | |
92 | case 0: | |
93 | yylval->build<std::string> () = | |
94 | std::string ("I have three numbers for you."); | |
95 | result = yy::parser::token::TEXT; | |
96 | break; | |
97 | case 1: | |
98 | case 2: | |
99 | case 3: | |
100 | yylval->build<int> () = stage; | |
101 | result = yy::parser::token::NUMBER; | |
102 | break; | |
103 | case 4: | |
104 | yylval->build<std::string> () = std::string ("And that's all!"); | |
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 | |
118 | yy::parser::error (const yy::parser::location_type& yylloc, | |
119 | const std::string& message) | |
120 | { | |
121 | std::cerr << yylloc << ": " << message << std::endl; | |
122 | } | |
123 | ||
124 | int | |
125 | main (int argc, char *argv[]) | |
126 | { | |
127 | yy::parser p; | |
128 | p.set_debug_level (!!getenv ("YYDEBUG")); | |
129 | p.parse (); | |
130 | } | |
131 | ||
132 | // Local Variables: | |
133 | // mode: C++ | |
134 | // End: |