00001 %{
00003 #include "cuiksystem.h"
00004 #include "readcuiksystemtypes.h"
00005
00006 #include "boolean.h"
00007 #include "error.h"
00008 #include "interval.h"
00009 #include "monomial.h"
00010 #include "equations.h"
00011 #include "equation.h"
00012 #include "variable.h"
00013 #include "variable_set.h"
00014 #include "constants.h"
00015 #include "defines.h"
00016 #include "parameters.h"
00017
00018 #include <stdlib.h>
00019 #include <stdio.h>
00020 #include <string.h>
00021 #include <math.h>
00022
00023
00024 extern FILE *Readcuiksystemin;
00025
00026
00027 int Readcuiksystemlex(void);
00028
00029
00030 extern unsigned int RCSline;
00031
00032 Tparameters *rcs_parameters=NULL;
00033
00034
00035 TCuikSystem *rcs_cuiksystem=NULL;
00036
00037 Tequation rcs_equation;
00038 Tmonomial rcs_monomial;
00039
00040 unsigned int rcs_eq_type=SYSTEM_EQ;
00041 unsigned int rcs_var_type=SYSTEM_VAR;
00042
00043 Tconstants rcs_constants;
00044
00045 %}
00046
00047 %union
00048 {
00049 char *id;
00050 int int_number;
00051 double real_number;
00052 Texpr expr;
00053 }
00054
00055 %start problem
00056
00057 %token _CONSTANTS _SYSTEM_VARS _SECONDARY_VARS _DUMMY_VARS _CARTESIAN_VARS _SYSTEM_EQS _COORD_EQS _DUMMY_EQS _SEARCH _DEPTH _BREADTH _FIRST _MIN _EQU _LEQ _GEQ _ASSIGN _INF _PI _SIN _COS _SQRT
00058
00059 %token <id> _IDENTIFIER
00060 %token <int_number> _INTEGER
00061 %token <real_number> _REAL
00062
00063 %type <expr> expr
00064 %type <int_number> cmp
00065 %type <int_number> power
00066
00067 %left _MIN_PRECEDENCE
00068 %left '+' '-'
00069 %left '*' '/'
00070 %left '^'
00071 %right _MAX_PRECEDENCE
00072
00073 %%
00074
00075 problem: ct_defs var_defs system_eqs coord_eqs dummy_eqs search
00076 ;
00077
00078 ct_defs : _CONSTANTS ct_list
00079 |
00080 ;
00081
00082 ct_list : ct_definition ct_list
00083 |
00084 ;
00085
00086 ct_definition: _IDENTIFIER _ASSIGN expr
00087 {
00088 if (GetConstantWithName($1,&rcs_constants)!=NO_UINT)
00089 {
00090 char s[200];
00091
00092 sprintf(s,"Duplicated constant %s",$1);
00093 ReadcuiksystemSemanticError(s);
00094 }
00095
00096 AddConstant($1,$3.value,&rcs_constants);
00097 free($1);
00098 }
00099 ;
00100
00101
00102 expr : '+' expr %prec _MAX_PRECEDENCE
00103 {
00104 if ($2.id!=NULL)
00105 ReadcuiksystemSemanticError("No operation is allowed with variables");
00106
00107 $$.id=NULL;
00108 $$.value=$2.value;
00109 }
00110 | '-' expr %prec _MAX_PRECEDENCE
00111 {
00112 if ($2.id!=NULL)
00113 ReadcuiksystemSemanticError("No operation is allowed with variables");
00114
00115 $$.id=NULL;
00116 $$.value=-$2.value;
00117 }
00118 | expr '+' expr
00119 {
00120 if (($1.id!=NULL)||($3.id!=NULL))
00121 ReadcuiksystemSemanticError("No operation is allowed with variables");
00122
00123 $$.id=NULL;
00124 $$.value=$1.value+$3.value;
00125 }
00126 | expr '-' expr
00127 {
00128 if (($1.id!=NULL)||($3.id!=NULL))
00129 ReadcuiksystemSemanticError("No operation is allowed with variables");
00130
00131 $$.id=NULL;
00132 $$.value=$1.value-$3.value;
00133 }
00134 | expr '*' expr
00135 {
00136 if (($1.id!=NULL)||($3.id!=NULL))
00137 ReadcuiksystemSemanticError("No operation is allowed with variables");
00138
00139 $$.id=NULL;
00140 $$.value=$1.value*$3.value;
00141 }
00142 | expr '^' expr
00143 {
00144 if (($1.id!=NULL)||($3.id!=NULL))
00145 ReadcuiksystemSemanticError("No operation is allowed with variables");
00146
00147 $$.id=NULL;
00148 $$.value=pow($1.value,$3.value);
00149 }
00150 | expr '/' expr
00151 {
00152 if (($1.id!=NULL)||($3.id!=NULL))
00153 ReadcuiksystemSemanticError("No operation is allowed with variables");
00154
00155 if ($3.value==0.0)
00156 ReadcuiksystemSemanticError("Division by zero");
00157
00158 $$.id=NULL;
00159 $$.value=$1.value/$3.value;
00160 }
00161 | '(' expr ')'
00162 {
00163 $$.id=$2.id;
00164 $$.value=$2.value;
00165 }
00166 | _PI
00167 {
00168 $$.id=NULL;
00169 $$.value=M_PI;
00170 }
00171 | _INF
00172 {
00173 $$.id=NULL;
00174 $$.value=INF;
00175 }
00176 | _SIN '(' expr ')'
00177 {
00178 if ($3.id!=NULL)
00179 ReadcuiksystemSemanticError("No operation is allowed with variables");
00180
00181 $$.id=NULL;
00182 $$.value=sin($3.value);
00183 }
00184 | _COS '(' expr ')'
00185 {
00186 if ($3.id!=NULL)
00187 ReadcuiksystemSemanticError("No operation is allowed with variables");
00188
00189 $$.id=NULL;
00190 $$.value=cos($3.value);
00191 }
00192 | _SQRT '(' expr ')'
00193 {
00194 if ($3.id!=NULL)
00195 ReadcuiksystemSemanticError("No operation is allowed with variables");
00196
00197 $$.id=NULL;
00198 $$.value=sqrt($3.value);
00199 }
00200 | _IDENTIFIER
00201 {
00202 unsigned int nc;
00203
00204 nc=GetConstantWithName($1,&rcs_constants);
00205
00206 if (nc!=NO_UINT)
00207 {
00208 $$.id=NULL;
00209 $$.value=GetConstantValue(nc,&rcs_constants);
00210 }
00211 else
00212 {
00213 char ms[200];
00214 sprintf(ms,"Undefined constant: %s",$1);
00215 ReadcuiksystemSemanticError(ms);
00216 }
00217 free($1);
00218 }
00219 | _INTEGER
00220 {
00221 $$.id=NULL;
00222 $$.value=(double)$1;
00223 }
00224 | _REAL
00225 {
00226 $$.id=NULL;
00227 $$.value=$1;
00228 }
00229 ;
00230
00231 var_defs : var_block var_defs
00232 |
00233 ;
00234
00235 var_block : system_vars
00236 | secondary_vars
00237 | dummy_vars
00238 | cartesian_vars
00239 ;
00240
00241 system_vars: _SYSTEM_VARS
00242 {
00243 rcs_var_type=SYSTEM_VAR;
00244 }
00245 range_list
00246 ;
00247
00248 secondary_vars: _SECONDARY_VARS
00249 {
00250 rcs_var_type=SECONDARY_VAR;
00251 }
00252 range_list
00253 ;
00254
00255 dummy_vars: _DUMMY_VARS
00256 {
00257 rcs_var_type=DUMMY_VAR;
00258 }
00259 range_list
00260 ;
00261
00262 cartesian_vars: _CARTESIAN_VARS
00263 {
00264 rcs_var_type=CARTESIAN_VAR;
00265 }
00266 range_list
00267 ;
00268
00269 range_list: range_definition range_list
00270 |
00271 ;
00272
00273 range_definition : _IDENTIFIER ':' '[' expr ',' expr ']'
00274 {
00275 Tinterval i_user;
00276 Tvariable v;
00277
00278 if (GetCSVariableID($1,rcs_cuiksystem)!=NO_UINT)
00279 {
00280 char s[300];
00281
00282 sprintf(s,"Repeated range declaration for variable %s",$1);
00283 ReadcuiksystemSemanticError(s);
00284 }
00285
00286 NewInterval($4.value,$6.value,&i_user);
00287
00288 if (EmptyInterval(&i_user))
00289 ReadcuiksystemSemanticError("Empty Interval");
00290
00291 NewVariable(rcs_var_type,$1,&v);
00292 SetVariableInterval(&i_user,&v);
00293
00294 AddVariable2CS(&v,rcs_cuiksystem);
00295
00296 DeleteVariable(&v);
00297 free($1);
00298 }
00299 ;
00300
00301 system_eqs : _SYSTEM_EQS
00302 {
00303 rcs_eq_type=SYSTEM_EQ;
00304 }
00305 eq_list
00306 |
00307 ;
00308
00309 coord_eqs : _COORD_EQS
00310 {
00311 rcs_eq_type=COORD_EQ;
00312 }
00313 eq_list
00314 |
00315 ;
00316
00317 dummy_eqs : _DUMMY_EQS
00318 {
00319 rcs_eq_type=DUMMY_EQ;
00320 }
00321 eq_list
00322 |
00323 ;
00324
00325 search: _SEARCH
00326 search_type
00327 |
00328 {
00329 SetCSSearchMode(DEPTH_FIRST_SEARCH,NULL,rcs_cuiksystem);
00330 }
00331 ;
00332
00333 search_type: _DEPTH _FIRST
00334 {
00335 SetCSSearchMode(DEPTH_FIRST_SEARCH,NULL,rcs_cuiksystem);
00336 }
00337 | _BREADTH _FIRST
00338 {
00339 SetCSSearchMode(BREADTH_FIRST_SEARCH,NULL,rcs_cuiksystem);
00340 }
00341 | _MIN monomials
00342 {
00343 SetEquationType(SYSTEM_EQ,&rcs_equation);
00344 SetEquationCmp(EQU,&rcs_equation);
00345 SetCSSearchMode(MINIMIZATION_SEARCH,&(rcs_equation),rcs_cuiksystem);
00346 ResetEquation(&rcs_equation);
00347 }
00348 ;
00349
00350 eq_list: equation
00351 {
00352 AddEquation2CS(rcs_parameters,&rcs_equation,rcs_cuiksystem);
00353
00354
00355 ResetEquation(&rcs_equation);
00356 }
00357 eq_list
00358 |
00359 ;
00360
00361 equation : monomials cmp expr ';'
00362 {
00363 SetEquationType(rcs_eq_type,&rcs_equation);
00364 SetEquationCmp($2,&rcs_equation);
00365 SetEquationValue($3.value+GetEquationValue(&rcs_equation),&rcs_equation);
00366
00367 ResetMonomial(&rcs_monomial);
00368 }
00369 ;
00370
00371 monomials : opt_sign monomial
00372 {
00373 AddMonomial(&rcs_monomial,&rcs_equation);
00374 ResetMonomial(&rcs_monomial);
00375 }
00376 more_monomials
00377 ;
00378
00379 more_monomials: sign monomial
00380 {
00381 AddMonomial(&rcs_monomial,&rcs_equation);
00382 ResetMonomial(&rcs_monomial);
00383 }
00384 more_monomials
00385 |
00386 ;
00387
00388 opt_sign : sign
00389 |
00390 ;
00391
00392 sign : '+'
00393 | '-'
00394 {
00395 AddCt2Monomial(-1.0,&rcs_monomial);
00396 }
00397 ;
00398
00399 monomial : item '*' monomial
00400 | item
00401 ;
00402
00403 item : _REAL
00404 {
00405 AddCt2Monomial($1,&rcs_monomial);
00406 }
00407 | _INTEGER
00408 {
00409 AddCt2Monomial((double)$1,&rcs_monomial);
00410 }
00411 | _SIN '(' expr ')'
00412 {
00413 if ($3.id!=NULL)
00414 ReadcuiksystemSemanticError("sin() only works on constant expressions");
00415
00416 AddCt2Monomial(sin($3.value),&rcs_monomial);
00417 }
00418 | _COS '(' expr ')'
00419 {
00420 if ($3.id!=NULL)
00421 ReadcuiksystemSemanticError("cos() only works on constant expressions");
00422
00423 AddCt2Monomial(cos($3.value),&rcs_monomial);
00424 }
00425 | _IDENTIFIER power
00426 {
00427 unsigned int id;
00428 unsigned int nc;
00429
00430
00431 nc=GetConstantWithName($1,&rcs_constants);
00432
00433 if (nc!=NO_UINT)
00434 AddCt2Monomial(pow(GetConstantValue(nc,&rcs_constants),(double)($2)),&rcs_monomial);
00435 else
00436 {
00437 id=GetCSVariableID($1,rcs_cuiksystem);
00438 if (id!=NO_UINT)
00439 AddVariable2Monomial(id,$2,&rcs_monomial);
00440 else
00441 {
00442 char s[300];
00443
00444 sprintf(s,"Unknown variable or constant %s",$1);
00445 ReadcuiksystemSemanticError(s);
00446 }
00447 }
00448 free($1);
00449 }
00450 ;
00451
00452 power : '^' _INTEGER
00453 {
00454 $$=$2;
00455 }
00456 |
00457 {
00458 $$=1;
00459 }
00460 ;
00461
00462 cmp : _EQU
00463 {
00464 $$=EQU;
00465 }
00466 | _LEQ
00467 {
00468 $$=LEQ;
00469 }
00470 | _GEQ
00471 {
00472 $$=GEQ;
00473 }
00474 ;
00475
00476 %%
00478
00479
00480
00481
00482 void InitCuikSystemFromFile(Tparameters *p,char *filename,TCuikSystem *cs)
00483 {
00484 rcs_parameters=p;
00485
00486 Readcuiksystemin=fopen(filename,"r");
00487 if (!Readcuiksystemin)
00488 {
00489 char ErrorText[500];
00490
00491 sprintf(ErrorText,"File %s does not exists",filename);
00492 Error(ErrorText);
00493 }
00494
00495
00496 InitCuikSystem(cs);
00497
00498 InitConstants(&rcs_constants);
00499
00500
00501 RCSline=1;
00502
00503
00504 rcs_cuiksystem=cs;
00505
00506
00507 InitEquation(&rcs_equation);
00508 InitMonomial(&rcs_monomial);
00509
00510
00511 Readcuiksystemparse();
00512
00513 DeleteConstants(&rcs_constants);
00514 DeleteEquation(&rcs_equation);
00515 DeleteMonomial(&rcs_monomial);;
00516
00517 cs->updated=FALSE;
00518 fclose(Readcuiksystemin);
00519 }
00520