00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 %skeleton "lalr1.cc"
00066 %require "2.3b"
00067 %defines
00068 %define "parser_class_name" "parser"
00069
00070
00071 %code requires{
00072 #include <vector>
00073 #include <utility>
00074
00075 #include "rational.hh"
00076 #include "field.hh"
00077 #include "constraint.hh"
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 class driver;
00090 }
00091
00092
00093
00094 %parse-param { driver& theDriver };
00095
00096
00097
00098 %locations
00099 %initial-action { @$.initialize(&theDriver.file); };
00100
00101
00102
00103 %error-verbose
00104 %debug
00105
00106
00107
00108
00109
00110
00111 %union {
00112 std::string *s;
00113 std::vector<std::string> *sl;
00114 constraint *c;
00115 std::vector<constraint> *cl;
00116 field *f;
00117 std::vector<field> *fl;
00118 rational *r;
00119 std::vector<rational> *rl;
00120 int i;
00121 };
00122
00123
00124 %code {
00125 #include "driver.hh"
00126
00127
00128 #define yylex theDriver.scan
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 template<class T> inline std::vector<T>*
00140 append(std::vector<T>* sequence, T* item) {
00141 std::vector<T>* result = sequence;
00142 if (item) {
00143 result->push_back(T());
00144 std::swap(result->back(), *item);
00145 delete item;
00146 }
00147 return result;
00148 }
00149
00150
00151
00152
00153
00154
00155
00156 template<class T> inline std::vector<T>*
00157 singleton(T* item) {
00158 return append(new std::vector<T>, item);
00159 }
00160
00161 }
00162
00163
00164
00165 %token END 0 "end of file"
00166 %token <s> WORD "word"
00167 %token <s> QSTR "quoted string"
00168 %token <i> INT "integer"
00169
00170
00171 %type <s> name string qn
00172 %type <sl> qnl
00173 %type <c> cs
00174 %type <cl> csl
00175 %type <f> vc
00176 %type <fl> vcl
00177 %type <r> rational
00178 %type <rl> rationals
00179 %type <i> sign denom
00180
00181
00182 %printer { debug_stream() << *$$; } WORD QSTR name string qn rational
00183 %printer { debug_stream() << $$; } INT denom
00184 %destructor { delete $$; } WORD QSTR name string qn qnl cs csl vc vcl rational
00185
00186 %%
00187
00188
00189
00190 %start instance;
00191
00192 instance: name '{' qns ',' css ',' vcs '}' { theDriver.result(@$, $1); }
00193 ;
00194
00195 name: string '=' { $$ = $1; }
00196 | { $$ = new std::string("unnamed"); }
00197 ;
00198
00199 string: WORD { $$ = $1; }
00200 | QSTR { $$ = $1; }
00201 | '*' { $$ = new std::string("*"); }
00202 ;
00203
00204
00205
00206 qns: '{' qnl '}' { theDriver.set_names(@$, $2); }
00207 ;
00208
00209 qnl: qnl ',' qn { $$ = append($1,$3); }
00210 | qn { $$ = singleton($1); }
00211 ;
00212
00213 qn: string { $$ = $1; }
00214 ;
00215
00216
00217
00218 css: '{' csl '}' { theDriver.set_constraints(@$, $2); }
00219 ;
00220
00221 csl: csl ',' cs { $$ = append($1,$3); }
00222 | cs { $$ = singleton($1); }
00223 ;
00224
00225 cs: '{' string ',' rational ',' INT '}' {
00226 $$ = new constraint($2, $4, $6);
00227 }
00228 | error { $$ = 0; }
00229 ;
00230
00231
00232
00233 vcs: '{' vcl '}' { theDriver.set_fields(@$, $2); }
00234 ;
00235
00236 vcl: vcl ',' vc { $$ = append($1,$3); }
00237 | vc { $$ = singleton($1); }
00238 ;
00239
00240 vc: '{' string ',' rationals '}' { $$ = theDriver.checked_field(@$, $2, $4); }
00241 | error { $$ = 0; }
00242 ;
00243
00244
00245
00246 rationals: rationals ',' rational { $$ = append($1,$3); }
00247 | rational { $$ = singleton($1); }
00248 ;
00249
00250 rational: sign INT denom { $$ = new rational($1 * $2, $3); }
00251 | '*' { $$ = new rational(); }
00252 ;
00253
00254 sign: '+' { $$ = 1; }
00255 | '-' { $$ = -1; }
00256 | { $$ = 1; }
00257 ;
00258
00259 denom: '/' INT { $$ = $2; }
00260 | { $$ = 1; }
00261 ;
00262
00263 %%
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273 void
00274 yy::parser::error (const yy::parser::location_type& location,
00275 const std::string& message) {
00276 theDriver.error(location, message);
00277 }
00278
00279