-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.y
executable file
·311 lines (260 loc) · 7.96 KB
/
parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
Chemical Compiler -> Parsing File
Author - Shalin Shah
Mentor - Manish K Gupta
Last Modified - 23rd April
Date Created - 15th March
*/
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "symbolTable.h"
/* prototypes */
nodeType *opr(int oper, int nops, ...);
nodeType *declareId(const char* i, int j);
nodeType *checkId(const char* i);
nodeType *con(int value);
void freeNode(nodeType *p);
void parseTree(nodeType *p);
int yylex(void);
void terminate();
char* genSpeciesCode(char* name, int amount);
void writeVarToXML(FILE* fp);
void yyerror(char *s);
symTableNode *symTable;
tempVarTableNode *tempVarTable;
speciesTableNode *speciesTable;
registers _localx, _localy, _localz;
const int FAST_REACT_PROPENSITY = 100;
const int SLOW_REACT_PROPENSITY = 1;
const int DECIMAL_VALUE = 10;
const int INTEGER_BUFFER_SIZE = 23; /* Largest unsigned int is 20 bytes + NULL */
const int START_AMOUNT = 10000;
extern int yylineno;
extern FILE *yyin; /* take input from a file not stdin*/
FILE *xmlFile; /* write output to a xml file*/
//FILE *xmlSpeciesFile, *xmlReacFile;
const char* const FILE_EXTENSION = "ccx";
char* FILE_NAME = "xmlFile.xml";
%}
%union
{
int iValue; /* integer value */
//char sIndex; /* symbol table index */
char* id;
nodeType *nPtr; /* node pointer */
};
%token <iValue> INTEGER
%token <id> VARIABLE
%token WHILE IF PRINT
%nonassoc IFX
%nonassoc ELSE
%left GE LE EQ NE '>' '<'
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
/*Define type for each non-terminal*/
%type <nPtr> stmt expr stmt_list
%%
program:
function { terminate(); }
;
function:
function stmt { parseTree($2); freeNode($2); }
| /* NULL */
;
stmt:
';' { $$ = opr(';', 2, NULL, NULL); }
| expr ';' { $$ = $1; }
| PRINT expr ';' { $$ = opr(PRINT, 1, $2); }
| VARIABLE '=' expr ';' { $$ = opr('=', 2, declareId($1, 0), $3); /*printf("\nvariable declared\n");*/}
| WHILE '(' expr ')' stmt { $$ = opr(WHILE, 2, $3, $5); }
| IF '(' expr ')' stmt %prec IFX { $$ = opr(IF, 2, $3, $5); }
| IF '(' expr ')' stmt ELSE stmt { $$ = opr(IF, 3, $3, $5, $7); }
| '{' stmt_list '}' { $$ = $2; }
;
stmt_list:
stmt { $$ = $1; }
| stmt_list stmt { $$ = opr(';', 2, $1, $2); }
;
expr:
INTEGER { $$ = con($1); }
| VARIABLE { $$ = checkId($1); /*printf("\nvariable used\n");*/}
| '-' expr %prec UMINUS { $$ = opr(UMINUS, 1, $2); }
| expr '+' expr { $$ = opr('+', 2, $1, $3); }
| expr '-' expr { $$ = opr('-', 2, $1, $3); }
| expr '*' expr { $$ = opr('*', 2, $1, $3); }
| expr '/' expr { $$ = opr('/', 2, $1, $3); }
| expr '<' expr { $$ = opr('<', 2, $1, $3); }
| expr '>' expr { $$ = opr('>', 2, $1, $3); }
| expr GE expr { $$ = opr(GE, 2, $1, $3); }
| expr LE expr { $$ = opr(LE, 2, $1, $3); }
| expr NE expr { $$ = opr(NE, 2, $1, $3); }
| expr EQ expr { $$ = opr(EQ, 2, $1, $3); }
| '(' expr ')' { $$ = $2; }
;
%%
nodeType *con(int value)
{
nodeType *p;
/* allocate node */
if ((p = malloc(sizeof(nodeType))) == NULL)
yyerror("out of memory");
/* copy information */
p->type = typeCon;
p->con.value = value;
return p;
}
nodeType *declareId(const char* name, int value)
{
nodeType* newTreeNode = NULL;
if(getSym(name) == NULL)
{
/* allocate node in AST */
if ((newTreeNode = malloc(sizeof(nodeType))) == NULL)
yyerror("out of memory");
/* copy information and put variable inside symbol table */
newTreeNode->type = typeId;
newTreeNode->id = putSym(name, value);
}
else
{
printf("\nSymbol is already declared. Updating..\n");
/* allocate node in AST */
if ((newTreeNode = malloc(sizeof(nodeType))) == NULL)
yyerror("out of memory");
/* copy information and put variable inside symbol table */
newTreeNode->type = typeId;
newTreeNode->id = getSym(name);
}
return newTreeNode;
}
nodeType *checkId(const char* name)
{
nodeType* newTreeNode = NULL;
symTableNode* tempNode = getSym(name);
if(tempNode != NULL)
{
/* allocate node in AST */
if ((newTreeNode = malloc(sizeof(nodeType))) == NULL)
yyerror("out of memory");
/* copy information and put variable inside symbol table */
newTreeNode->type = typeId;
newTreeNode->id = tempNode;
}
else
{
printf("\nError! No such symbol %s found.\n", name);
exit(0);
}
return newTreeNode;
}
nodeType *opr(int oper, int nops, ...)
{
va_list ap;
nodeType *p;
int i;
/* allocate node */
if ((p = malloc(sizeof(nodeType))) == NULL)
yyerror("out of memory");
if ((p->opr.op = malloc(nops * sizeof(nodeType *))) == NULL)
yyerror("out of memory");
/* copy information */
p->type = typeOpr;
p->opr.oper = oper;
p->opr.nops = nops;
va_start(ap, nops);
for (i = 0; i < nops; i++)
p->opr.op[i] = va_arg(ap, nodeType*);
va_end(ap);
return p;
}
void freeNode(nodeType *p)
{
int i;
if (!p) return;
if (p->type == typeOpr) {
for (i = 0; i < p->opr.nops; i++)
freeNode(p->opr.op[i]);
free (p->opr.op);
}
free (p);
}
void yyerror(char *s)
{
fprintf(stdout, "%s at/near line %d\n", s, yylineno - 1);
}
void terminate()
{
printf("Terminating\n");
FILE *xmlSpeciesFile, *xmlReacFile;
char ch;
symTableNode* tempNode = NULL;
xmlSpeciesFile = fopen("tmp/xmlSpeciesFile.xml", "r");
xmlReacFile = fopen("tmp/xmlReacFile.xml", "r");
xmlFile = fopen(FILE_NAME, "w");
while((ch = fgetc(xmlSpeciesFile) ) != EOF )
fputc(ch, xmlFile);
writeVarToXML(xmlFile);
fprintf(xmlFile, "%s", "</listOfSpecies>\n\n<listOfReactions>\n");
while((ch = fgetc(xmlReacFile) ) != EOF )
fputc(ch, xmlFile);
fprintf(xmlFile, "%s\n", "</listOfReactions>\n</model>\n</listOfModels>\n<listOfMethods>\n<method equilibrationTime=\"0.0\" startTime=\"0.0\" recordingTime=\"1.0\" id=\"2\" category=\"0\" numberOfBins=\"32\" multiplicity=\"4\" method=\"0\" timeDependence=\"0\" numberOfFrames=\"11\" options=\"0\"/>\n</listOfMethods>\n<random seed=\"2147483648\">\n</random>\n</cain>");
fclose(xmlFile);
fclose(xmlSpeciesFile);
fclose(xmlReacFile);
printf("Terminated\n");
}
/* Starting point of code */
int main(int argc, char *argv[])
{
/* Remove tmp files from last session */
remove("tmp/xmlSpeciesFile.xml");
remove("tmp/xmlReacFile.xml");
/* uthash lib requires to set symTable pointer be set to null */
symTable = NULL;
tempVarTable = NULL;
_localz.isEmpty = 1;
// Check the file extension and arguments
strtok(argv[1], ".");
if(argc == 1)
{
/* Take input from stdin */
printf("Chemical Compiler V-1.0 (Interpreter Mode)\n");
yyparse();
}
else if(argc == 2)
{
yyin = fopen(strcat(argv[1], ".ccx"), "r");
/* fopen returns 0, the NULL pointer, on failure */
if(!yyin)
{
printf("Sorry, error handling the file.\n\n");
}
else
{
yyparse();
}
}
else if(argc == 3)
{
FILE_NAME = strcat(argv[2], ".xml");
yyin = fopen(strcat(argv[1], ".ccx"), "r");
/* fopen returns 0, the NULL pointer, on failure */
if(!yyin)
{
printf("Sorry, error handling the file.\n\n");
}
else
{
yyparse();
}
}
else if(strcmp(strtok(NULL, "."), FILE_EXTENSION) != 0)
{
printf("Please enter a chemical copiler file name as argument.\n\n");
}
return 0;
}