1 | /****************************************************************************** |
---|
2 | |
---|
3 | KPP - The Kinetic PreProcessor |
---|
4 | Builds simulation code for chemical kinetic systems |
---|
5 | |
---|
6 | Copyright (C) -2020 996 Valeriu Damian and Adrian Sandu |
---|
7 | Copyright (C) -2020 005 Adrian Sandu |
---|
8 | |
---|
9 | KPP is free software; you can redistribute it and/or modify it under the |
---|
10 | terms of the GNU General Public License as published by the Free Software |
---|
11 | Foundation (http://www.gnu.org/copyleft/gpl.html); either version 2 of the |
---|
12 | License, or (at your option) any later version. |
---|
13 | |
---|
14 | KPP is distributed in the hope that it will be useful, but WITHOUT ANY |
---|
15 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
16 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
17 | details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License along |
---|
20 | with this program; if not, consult http://www.gnu.org/copyleft/gpl.html or |
---|
21 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
22 | Boston, MA 02111-1307, USA. |
---|
23 | |
---|
24 | Adrian Sandu |
---|
25 | Computer Science Department |
---|
26 | Virginia Polytechnic Institute and State University |
---|
27 | Blacksburg, VA 24060 |
---|
28 | E-mail: sandu@cs.vt.edu |
---|
29 | |
---|
30 | ******************************************************************************/ |
---|
31 | |
---|
32 | /****************************************************************************** |
---|
33 | bug fixes for upgrading from yacc to bison implemented by Rolf Sander, |
---|
34 | following suggestions from Jason Lander <jason@env.leeds.ac.uk> |
---|
35 | ******************************************************************************/ |
---|
36 | |
---|
37 | %{ |
---|
38 | #include <stdio.h> |
---|
39 | #include <stdlib.h> |
---|
40 | #include <malloc.h> |
---|
41 | #include <string.h> |
---|
42 | #include <unistd.h> |
---|
43 | #include "scan.h" |
---|
44 | |
---|
45 | #define __YYSCLASS |
---|
46 | |
---|
47 | #define YYDEBUG 1 |
---|
48 | extern char yytext[]; |
---|
49 | extern FILE * yyin; |
---|
50 | |
---|
51 | int nError = 0; |
---|
52 | int nWarning = 0; |
---|
53 | |
---|
54 | int crt_section; |
---|
55 | int eqState; |
---|
56 | int isPhoto = 0; |
---|
57 | |
---|
58 | char crt_term[ 30 ]; |
---|
59 | char crt_coef[ 30 ]; |
---|
60 | |
---|
61 | char * InlineBuf; |
---|
62 | int InlineLen; |
---|
63 | |
---|
64 | void SemicolonError(); |
---|
65 | int yyerrflag=0; |
---|
66 | |
---|
67 | void ParserErrorMessage(); |
---|
68 | void yyerror(char *); |
---|
69 | |
---|
70 | %} |
---|
71 | |
---|
72 | %union{ |
---|
73 | char str[80]; |
---|
74 | }; |
---|
75 | |
---|
76 | %token JACOBIAN DOUBLE FUNCTION DEFVAR DEFRAD DEFFIX SETVAR SETRAD SETFIX |
---|
77 | %token HESSIAN STOICMAT STOCHASTIC DECLARE |
---|
78 | %token INITVALUES EQUATIONS LUMP INIEQUAL EQNEQUAL EQNCOLON |
---|
79 | %token LMPCOLON LMPPLUS SPCPLUS SPCEQUAL ATOMDECL CHECK CHECKALL REORDER |
---|
80 | %token MEX DUMMYINDEX EQNTAGS |
---|
81 | %token LOOKAT LOOKATALL TRANSPORT TRANSPORTALL MONITOR USES SPARSEDATA |
---|
82 | %token WRITE_ATM WRITE_SPC WRITE_MAT WRITE_OPT INITIALIZE XGRID YGRID ZGRID |
---|
83 | %token USE LANGUAGE INTFILE DRIVER RUN INLINE ENDINLINE |
---|
84 | %token PARAMETER SPCSPC INISPC INIVALUE EQNSPC EQNSIGN EQNCOEF |
---|
85 | %type <str> PARAMETER SPCSPC INISPC INIVALUE EQNSPC EQNSIGN EQNCOEF |
---|
86 | %token RATE LMPSPC SPCNR ATOMID LKTID MNIID INLCTX INCODE SSPID |
---|
87 | %type <str> RATE LMPSPC SPCNR ATOMID LKTID MNIID INLCTX INCODE SSPID |
---|
88 | %token EQNLESS EQNTAG EQNGREATER |
---|
89 | %type <str> EQNLESS EQNTAG EQNGREATER |
---|
90 | %token TPTID USEID |
---|
91 | %type <str> TPTID USEID |
---|
92 | %type <str> rate eqntag |
---|
93 | |
---|
94 | %% |
---|
95 | |
---|
96 | program : section |
---|
97 | | section program |
---|
98 | ; |
---|
99 | section : JACOBIAN PARAMETER |
---|
100 | { CmdJacobian( $2 ); |
---|
101 | } |
---|
102 | | HESSIAN PARAMETER |
---|
103 | { CmdHessian( $2 ); |
---|
104 | } |
---|
105 | | DECLARE PARAMETER |
---|
106 | { CmdDeclareValues( $2 ); |
---|
107 | } |
---|
108 | | STOICMAT PARAMETER |
---|
109 | { CmdStoicmat( $2 ); |
---|
110 | } |
---|
111 | | DOUBLE PARAMETER |
---|
112 | { CmdDouble( $2 ); |
---|
113 | } |
---|
114 | | REORDER PARAMETER |
---|
115 | { CmdReorder( $2 ); |
---|
116 | } |
---|
117 | | MEX PARAMETER |
---|
118 | { CmdMex( $2 ); |
---|
119 | } |
---|
120 | | DUMMYINDEX PARAMETER |
---|
121 | { CmdDummyindex( $2 ); |
---|
122 | } |
---|
123 | | EQNTAGS PARAMETER |
---|
124 | { CmdEqntags( $2 ); |
---|
125 | } |
---|
126 | | FUNCTION PARAMETER |
---|
127 | { CmdFunction( $2 ); |
---|
128 | } |
---|
129 | | STOCHASTIC PARAMETER |
---|
130 | { CmdStochastic( $2 ); |
---|
131 | } |
---|
132 | | ATOMDECL atomlist |
---|
133 | {} |
---|
134 | | CHECK atomlist |
---|
135 | {} |
---|
136 | | DEFVAR species |
---|
137 | {} |
---|
138 | | DEFRAD species |
---|
139 | {} |
---|
140 | | DEFFIX species |
---|
141 | {} |
---|
142 | | SETVAR setspclist |
---|
143 | {} |
---|
144 | | SETRAD setspclist |
---|
145 | {} |
---|
146 | | SETFIX setspclist |
---|
147 | {} |
---|
148 | | INITVALUES initvalues |
---|
149 | {} |
---|
150 | | EQUATIONS equations |
---|
151 | {} |
---|
152 | | LUMP lumps |
---|
153 | {} |
---|
154 | | LOOKAT lookatlist |
---|
155 | {} |
---|
156 | | MONITOR monitorlist |
---|
157 | {} |
---|
158 | | TRANSPORT translist |
---|
159 | {} |
---|
160 | | CHECKALL |
---|
161 | { CheckAll(); } |
---|
162 | | LOOKATALL |
---|
163 | { LookAtAll(); } |
---|
164 | | TRANSPORTALL |
---|
165 | { TransportAll(); } |
---|
166 | | WRITE_ATM |
---|
167 | { WriteAtoms(); } |
---|
168 | | WRITE_SPC |
---|
169 | { WriteSpecies(); } |
---|
170 | | WRITE_MAT |
---|
171 | { WriteMatrices(); } |
---|
172 | | WRITE_OPT |
---|
173 | { WriteOptions(); } |
---|
174 | | USE PARAMETER |
---|
175 | { CmdUse( $2 ); } |
---|
176 | | LANGUAGE PARAMETER |
---|
177 | { CmdLanguage( $2 ); } |
---|
178 | | INITIALIZE PARAMETER |
---|
179 | { DefineInitializeNbr( $2 ); } |
---|
180 | | XGRID PARAMETER |
---|
181 | { DefineXGrid( $2 ); } |
---|
182 | | YGRID PARAMETER |
---|
183 | { DefineYGrid( $2 ); } |
---|
184 | | ZGRID PARAMETER |
---|
185 | { DefineZGrid( $2 ); } |
---|
186 | | INLINE INLCTX inlinecode ENDINLINE |
---|
187 | { |
---|
188 | AddInlineCode( $2, InlineBuf ); |
---|
189 | free( InlineBuf ); |
---|
190 | } |
---|
191 | | INLINE error |
---|
192 | { ParserErrorMessage(); } |
---|
193 | | INTFILE PARAMETER |
---|
194 | { CmdIntegrator( $2 ); } |
---|
195 | | DRIVER PARAMETER |
---|
196 | { CmdDriver( $2 ); } |
---|
197 | | RUN PARAMETER |
---|
198 | { CmdRun( $2 ); } |
---|
199 | | USES uselist |
---|
200 | {} |
---|
201 | | SPARSEDATA PARAMETER |
---|
202 | { SparseData( $2 ); |
---|
203 | } |
---|
204 | ; |
---|
205 | semicolon : semicolon ';' |
---|
206 | { ScanWarning("Unnecessary ';'"); |
---|
207 | } |
---|
208 | | ';' |
---|
209 | ; |
---|
210 | atomlist : atomlist atomdef semicolon |
---|
211 | | atomdef semicolon |
---|
212 | | error semicolon |
---|
213 | { ParserErrorMessage(); } |
---|
214 | ; |
---|
215 | atomdef : ATOMID |
---|
216 | { switch( crt_section ) { |
---|
217 | case ATOMDECL: DeclareAtom( $1 ); break; |
---|
218 | case CHECK: SetAtomType( $1, DO_CHECK ); break; |
---|
219 | } |
---|
220 | } |
---|
221 | ; |
---|
222 | lookatlist : lookatlist lookatspc semicolon |
---|
223 | | lookatspc semicolon |
---|
224 | | error semicolon |
---|
225 | { ParserErrorMessage(); } |
---|
226 | ; |
---|
227 | lookatspc : LKTID |
---|
228 | { AddLookAt( $1 ); |
---|
229 | } |
---|
230 | ; |
---|
231 | monitorlist : monitorlist monitorspc semicolon |
---|
232 | | monitorspc semicolon |
---|
233 | | error semicolon |
---|
234 | { ParserErrorMessage(); } |
---|
235 | ; |
---|
236 | monitorspc : MNIID |
---|
237 | { AddMonitor( $1 ); |
---|
238 | } |
---|
239 | ; |
---|
240 | translist : translist transspc semicolon |
---|
241 | | transspc semicolon |
---|
242 | | error semicolon |
---|
243 | { ParserErrorMessage(); } |
---|
244 | ; |
---|
245 | transspc : TPTID |
---|
246 | { AddTransport( $1 ); |
---|
247 | } |
---|
248 | ; |
---|
249 | uselist : uselist usefile semicolon |
---|
250 | | usefile semicolon |
---|
251 | | error semicolon |
---|
252 | { ParserErrorMessage(); } |
---|
253 | ; |
---|
254 | usefile : USEID |
---|
255 | { AddUseFile( $1 ); |
---|
256 | } |
---|
257 | ; |
---|
258 | setspclist : setspclist setspcspc semicolon |
---|
259 | | setspcspc semicolon |
---|
260 | | error semicolon |
---|
261 | { ParserErrorMessage(); } |
---|
262 | ; |
---|
263 | setspcspc : SSPID |
---|
264 | { switch( crt_section ) { |
---|
265 | case SETVAR: SetSpcType( VAR_SPC, $1 ); break; |
---|
266 | case SETRAD: SetSpcType( RAD_SPC, $1 ); break; |
---|
267 | case SETFIX: SetSpcType( FIX_SPC, $1 ); break; |
---|
268 | } |
---|
269 | } |
---|
270 | ; |
---|
271 | species : species spc semicolon |
---|
272 | | spc semicolon |
---|
273 | | error semicolon |
---|
274 | { ParserErrorMessage(); } |
---|
275 | ; |
---|
276 | spc : spcname |
---|
277 | | spcdef |
---|
278 | ; |
---|
279 | spcname : SPCSPC SPCEQUAL atoms |
---|
280 | { switch( crt_section ) { |
---|
281 | case DEFVAR: DeclareSpecies( VAR_SPC, $1 ); break; |
---|
282 | case DEFRAD: DeclareSpecies( RAD_SPC, $1 ); break; |
---|
283 | case DEFFIX: DeclareSpecies( FIX_SPC, $1 ); break; |
---|
284 | } |
---|
285 | } |
---|
286 | ; |
---|
287 | spcdef : SPCSPC |
---|
288 | { switch( crt_section ) { |
---|
289 | case DEFVAR: DeclareSpecies( VAR_SPC, $1 ); break; |
---|
290 | case DEFRAD: DeclareSpecies( RAD_SPC, $1 ); break; |
---|
291 | case DEFFIX: DeclareSpecies( FIX_SPC, $1 ); break; |
---|
292 | } |
---|
293 | } |
---|
294 | ; |
---|
295 | atoms : atoms SPCPLUS atom |
---|
296 | | atom |
---|
297 | ; |
---|
298 | atom : SPCNR SPCSPC |
---|
299 | { AddAtom( $2, $1 ); |
---|
300 | } |
---|
301 | | SPCSPC |
---|
302 | { AddAtom( $1, "1" ); |
---|
303 | } |
---|
304 | ; |
---|
305 | initvalues : initvalues assignment semicolon |
---|
306 | | assignment semicolon |
---|
307 | | error semicolon |
---|
308 | { ParserErrorMessage(); } |
---|
309 | ; |
---|
310 | assignment : INISPC INIEQUAL INIVALUE |
---|
311 | { AssignInitialValue( $1, $3 ); } |
---|
312 | ; |
---|
313 | equations : equations equation semicolon |
---|
314 | | equation semicolon |
---|
315 | | error semicolon |
---|
316 | { ParserErrorMessage(); |
---|
317 | eqState = LHS; |
---|
318 | } |
---|
319 | ; |
---|
320 | equation : eqntag lefths righths rate |
---|
321 | { eqState = LHS; |
---|
322 | StoreEquationRate( $4, $1 ); |
---|
323 | CheckEquation(); |
---|
324 | } |
---|
325 | | lefths righths rate |
---|
326 | { eqState = LHS; |
---|
327 | StoreEquationRate( $3, " " ); |
---|
328 | CheckEquation(); |
---|
329 | } |
---|
330 | rate : RATE rate |
---|
331 | { strcpy( $$, $1 ); |
---|
332 | strcat( $$, $2 ); |
---|
333 | } |
---|
334 | | RATE |
---|
335 | { strcpy( $$, $1 ); |
---|
336 | } |
---|
337 | ; |
---|
338 | eqntag : EQNLESS EQNTAG EQNGREATER |
---|
339 | { strcpy( $$, $2 ); |
---|
340 | } |
---|
341 | ; |
---|
342 | lefths : expresion EQNEQUAL |
---|
343 | { eqState = RHS; } |
---|
344 | ; |
---|
345 | righths : expresion EQNCOLON |
---|
346 | { eqState = RAT; } |
---|
347 | ; |
---|
348 | expresion : expresion EQNSIGN term |
---|
349 | { ProcessTerm( eqState, $2, crt_coef, crt_term ); |
---|
350 | } |
---|
351 | | EQNSIGN term |
---|
352 | { ProcessTerm( eqState, $1, crt_coef, crt_term ); |
---|
353 | } |
---|
354 | | term |
---|
355 | { ProcessTerm( eqState, "+", crt_coef, crt_term ); |
---|
356 | } |
---|
357 | ; |
---|
358 | term : EQNCOEF EQNSPC |
---|
359 | { strcpy( crt_term, $2 ); |
---|
360 | strcpy( crt_coef, $1 ); |
---|
361 | } |
---|
362 | | EQNSPC |
---|
363 | { strcpy( crt_term, $1 ); |
---|
364 | strcpy( crt_coef, "1" ); |
---|
365 | } |
---|
366 | ; |
---|
367 | lumps : lumps lump semicolon |
---|
368 | | lump semicolon |
---|
369 | | error semicolon |
---|
370 | { ParserErrorMessage(); } |
---|
371 | ; |
---|
372 | lump : LMPSPC LMPPLUS lump |
---|
373 | { AddLumpSpecies( $1 ); |
---|
374 | } |
---|
375 | | LMPSPC LMPCOLON LMPSPC |
---|
376 | { |
---|
377 | AddLumpSpecies( $1 ); |
---|
378 | CheckLump( $3 ); |
---|
379 | } |
---|
380 | inlinecode : inlinecode INCODE |
---|
381 | { |
---|
382 | InlineBuf = AppendString( InlineBuf, $2, &InlineLen, MAX_INLINE ); |
---|
383 | } |
---|
384 | | INCODE |
---|
385 | { |
---|
386 | InlineBuf = malloc( MAX_INLINE ); |
---|
387 | InlineLen = MAX_INLINE; |
---|
388 | strcpy( InlineBuf, $1); |
---|
389 | } |
---|
390 | ; |
---|
391 | %% |
---|
392 | |
---|
393 | void yyerror( char * str ) |
---|
394 | { |
---|
395 | } |
---|
396 | |
---|
397 | void ParserErrorMessage() |
---|
398 | { |
---|
399 | /* yyerrok; */ |
---|
400 | /* |
---|
401 | Message("[%d,%s] -> [%d,%s]", crtTokType, crtToken, nextTokType, nextToken ); |
---|
402 | */ |
---|
403 | if( crtToken[0] == ';' ) { |
---|
404 | ParserError("Misplaced ';'"); |
---|
405 | return; |
---|
406 | } |
---|
407 | switch( crtTokType ) { |
---|
408 | case ATOMID: |
---|
409 | ParserError("Missing ';' after '%s'", crtToken ); |
---|
410 | break; |
---|
411 | |
---|
412 | case SPCSPC: |
---|
413 | ParserError("Missing ';' or '+' after '%s'", crtToken ); |
---|
414 | break; |
---|
415 | case SPCNR: |
---|
416 | ParserError("Missing species after '%s'", crtToken ); |
---|
417 | break; |
---|
418 | case SPCPLUS: |
---|
419 | ParserError("Missing atom after '%s'", crtToken ); |
---|
420 | break; |
---|
421 | case SPCEQUAL: |
---|
422 | ParserError("Invalid '=' after '%s'", crtToken ); |
---|
423 | break; |
---|
424 | |
---|
425 | case INISPC: |
---|
426 | ParserError("Missing '=' after '%s'", crtToken ); |
---|
427 | break; |
---|
428 | case INIEQUAL: |
---|
429 | ParserError("Missing value after '%s'", crtToken ); |
---|
430 | break; |
---|
431 | case INIVALUE: |
---|
432 | ParserError("Missing ';' after '%s'", crtToken ); |
---|
433 | break; |
---|
434 | |
---|
435 | case EQNSPC: |
---|
436 | ParserError("Missing '+' or '=' after '%s'", crtToken ); |
---|
437 | break; |
---|
438 | case EQNEQUAL: |
---|
439 | ParserError("Invalid right hand side of equation"); |
---|
440 | break; |
---|
441 | case EQNCOLON: |
---|
442 | ParserError("Missing rate after '%s'", crtToken ); |
---|
443 | break; |
---|
444 | case EQNSIGN: |
---|
445 | ParserError("Missing coeficient after '%s'", crtToken ); |
---|
446 | break; |
---|
447 | case EQNCOEF: |
---|
448 | ParserError("Missing species after '%s'", crtToken ); |
---|
449 | break; |
---|
450 | case RATE: |
---|
451 | ParserError("Missing ';' after '%s'", crtToken ); |
---|
452 | break; |
---|
453 | |
---|
454 | case LMPSPC: |
---|
455 | ParserError("Missing '+' or ':' or ';' after '%s'", crtToken ); |
---|
456 | break; |
---|
457 | case LMPPLUS: |
---|
458 | ParserError("Missing species after '%s'", crtToken ); |
---|
459 | break; |
---|
460 | case LMPCOLON: |
---|
461 | ParserError("Missing species after '%s'", crtToken ); |
---|
462 | break; |
---|
463 | case INLINE: |
---|
464 | ParserError("Missing inline option after '%s'", crtToken ); |
---|
465 | break; |
---|
466 | |
---|
467 | default: |
---|
468 | ParserError("Syntax error after '%s'", crtToken ); |
---|
469 | } |
---|
470 | } |
---|
471 | |
---|
472 | |
---|
473 | int Parser( char * filename ) |
---|
474 | { |
---|
475 | extern int yydebug; |
---|
476 | FILE *f; |
---|
477 | |
---|
478 | crt_filename = filename; |
---|
479 | |
---|
480 | f = fopen( crt_filename, "r" ); |
---|
481 | if( f == 0 ) { |
---|
482 | FatalError(7,"%s: File not found", crt_filename); |
---|
483 | } |
---|
484 | |
---|
485 | yyin = f; |
---|
486 | nError = 0; |
---|
487 | nWarning = 0; |
---|
488 | yydebug = 0; |
---|
489 | |
---|
490 | yyparse(); |
---|
491 | |
---|
492 | fclose( f ); |
---|
493 | |
---|
494 | return nError; |
---|
495 | } |
---|
496 | |
---|