/
LexerGenerator.mo
450 lines (393 loc) · 13.8 KB
/
LexerGenerator.mo
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package LexerGenerator
import System;
constant Boolean debug = true;
import OMCC.copyright;
function genLexer
input String flexFile;
input String grammarFile;
input String outFileName;
input String tokens;
output String result;
protected
String flexCode,re,ar1,rest,lexTable;
Boolean res2,res3,res4;
list<String> resultRegex,resTable,chars;
algorithm
//open flex file and validate
if (outFileName<>"" and stringLength(outFileName)<15) then
if (debug==true) then
print("Generating Lexer from " + flexFile + "\n");
end if;
flexCode := System.readFile(flexFile);
print("Read FLEX grammar file " + flexFile + "\n");
lexTable := buildLexTable(flexCode);
if (debug==true) then
print("Build Lex Table ...\n");
end if;
buildLexerCode(flexCode,grammarFile,outFileName,lexTable=lexTable,tokens=tokens);
if (debug==true) then
print("Build LexerCode ...\n");
end if;
result := "Lexer Built";
else
result := "Invalid language grammar name";
end if;
end genLexer;
function readPrologEpilog
input String lexerCode;
input String grammarFileName;
output String lexerCodeIncluded;
protected
String grammarFile,epilog,prolog,re,ar1,astRootType;
Integer numMatches,pos1,pos2;
list<String> resultRegex;
algorithm
if (debug==true) then
print("\nRead epilogue and prologue");
end if;
grammarFile := System.readFile(grammarFileName);
//find prologue
pos1 := System.stringFind(grammarFile,"%{");
pos2 := System.stringFind(grammarFile,"%}");
ar1 := System.substring(grammarFile,pos1+3,pos2-1);
lexerCodeIncluded := System.stringReplace(lexerCode,"%prologue%",ar1);
//
/* ar1 := System.stringFindString(grammarFile,"AstTree");
pos1 := System.stringFind(ar1,"=");
pos2 := System.stringFind(ar1,";");
astRootType := System.substring(ar1,pos1+2,pos2);
astRootType := System.trim(astRootType," ");
parserCodeIncluded := System.stringReplace(parserCodeIncluded,"%astTree%",astRootType); */
//find epilogue
re := "%%";
ar1 := System.stringFindString(grammarFile,re);
ar1 := System.substring(ar1,3,stringLength(ar1));
ar1 := System.stringFindString(ar1,re);
ar1 := System.substring(ar1,3,stringLength(ar1));
lexerCodeIncluded := System.stringReplace(lexerCodeIncluded,"%epilogue%",ar1);
end readPrologEpilog;
function buildLexerCode
input String flexCode;
input String grammarFile;
input String outFileName;
input String lexTable;
input String tokens;
protected
list<String> resTable;
String lexCode,result,rest,stTime,cp,caseAction,re,tokenName;
Integer i,numMatches,numRules,pos,pos2,posBegin,posReturn,posKeepBuffer,posBreak,valBegin;
algorithm
lexCode := System.readFile("LexerCode.tmo");
stTime := copyright;
result := System.stringReplace(lexCode, "%LexerCode%", "Lexer"+outFileName);
result := System.stringReplace(result, "%time%", stTime);
result := System.stringReplace(result, "%Tokens%", tokens);
result := System.stringReplace(result, "%LexTable%", lexTable);
result := System.stringReplace(result, "%nameSpan%", "255");
result := readPrologEpilog(result,grammarFile);
caseAction := "";
resTable := {};
numRules := findValue(flexCode,"YY_NUM_RULES");
// print("\n new area");
re := "/* beginning of action switch */";
rest := System.stringFindString(flexCode,re);
if (debug==true) then
print("beginning of action switch\n");
end if;
for i in 1:numRules loop
cp := " case (";
resTable := cp::resTable;
cp := intString(i);
resTable := cp::resTable;
cp := ") // ";
resTable := cp::resTable;
re := "case " + intString(i) + ":";
(numMatches,_::rest::_) := System.regex(flexCode,"case " + intString(i) + ":[^#]*(#[^#]*)YY_BREAK[^#]*",2,extended=true);
if (numMatches < 2) then
print("Failed to find lexer case " + intString(i) + "\n");
fail();
end if;
(numMatches,_::tokenName::_) := System.regex(rest,"return *([^;]*);",2,extended=true);
print(intString(i) + " is token " + tokenName + "\n");
re := "#line";
pos := System.stringFind(rest,re);
pos2 := System.stringFind(rest,".l");
cp := substring2(rest,pos+1,pos2+3);
resTable := cp::resTable;
//posReturn,posKeepBuffer,posBreak
posReturn := System.stringFind(rest,"Return ");
posBegin := System.stringFind(rest,"BEGIN");
posKeepBuffer := System.stringFind(rest,"keepBuffer");
//print("\n pos:" + intString(pos) + ":" + "pos2:" + intString(pos2) + ":" + "posB:" + intString(posBegin) );
resTable := "\n equation" :: resTable;
if (posBegin>=0) then // starts BEGIN switch start state
// find token
pos := System.stringFind(rest,"(");
pos2 := System.stringFind(rest,")");
cp := substring2(rest,pos+2,pos2);
valBegin := findValue(flexCode,cp);
valBegin := 1+2*valBegin;
if (debug==true) then
print("\n BEGIN at" + intString(valBegin));
end if;
cp := "\n mm_startSt = " + intString(valBegin) +";";
resTable := cp::resTable;
end if;
if (posKeepBuffer>=0) then // starts keepbuffer switch start state
// print keep buffer
if (debug==true) then
print("\n keepbuffer");
end if;
cp := "\n bufferRet = buffer;";
resTable := cp::resTable;
end if;
if tokenName <> "" then
cp := "\n tok = TOKEN(\"";
resTable := tokenName::cp::resTable;
cp := ",fileContents,mm_pos-buffer,buffer,lineNrStart,mm_ePos+1,mm_linenr,mm_sPos+1);\n then tok;\n";
resTable := cp::tokenName::"\",TokenId."::resTable;
else
//print("NONE");
cp := "\n then noToken;\n";
resTable := cp::resTable;
end if;
end for;
resTable := listReverse(resTable);
caseAction := stringCharListString(resTable);
result := System.stringReplace(result,"%caseAction%",caseAction);
System.writeFile("Lexer" + outFileName + ".mo",result);
end buildLexerCode;
function findValue
input String flexCode;
input String variable;
output Integer value;
protected
Integer pos;
String rest,val,re;
algorithm
(_,_::val::{}) := System.regex(flexCode,"define " + variable + " ([0-9]*)",2,true,false);
print("\n val is:" +val);
value := stringInt(val);
print("\n value found");
end findValue;
function buildLexTable
input String flexCode;
output String buildResult;
protected
String cp,re,re1,ar1,rest,result,stTime;
Integer numMatches,pos1,pos2,len;
list<String> resultRegex,resTable,chars;
constant String prefixScalar = " constant Integer ";
constant String prefixList = " constant Integer ";
constant String prefixList2 = "[:] = array(";
constant String suffixScalar = ";\n";
constant String suffixList = ");\n";
algorithm
stTime := copyright;
cp := prefixScalar + "yy_limit = ";
resTable := cp::{};
// Insert yy_limit
re := "if ( yy_current_state >= ";
re1 := "if ( yy_current_state >=[^)]*)";
(numMatches,resultRegex) := System.regex(flexCode,re1,1,false,false);
ar1::_ := resultRegex;
if (debug==true) then
print("\nFound regex:" + ar1);
end if;
numMatches:=0;
(numMatches,resultRegex) := System.regex("if ( yy_current_state >= 65 )","[0-9]*",2,false,false);
if (debug==true) then
print("\nNumMatches:" + intString(numMatches));
end if;
cp::_ := resultRegex;
if (debug==true) then
print("\nFound regex2:" + cp);
end if;
rest := System.stringFindString(flexCode,re);
pos2 := System.stringFind(rest,")");
ar1 := substring2(rest,stringLength(re)+1,pos2-1);
resTable := ar1::resTable;
cp := suffixScalar+prefixScalar+"yy_finish = ";
resTable := cp::resTable;
re := "while ( yy_base[yy_current_state] != ";
rest := System.stringFindString(flexCode,re);
pos2 := System.stringFind(rest,")");
ar1 := substring2(rest,stringLength(re)+1,pos2-1);
resTable := ar1::resTable;
cp := suffixScalar+prefixList+"yy_acclist"+prefixList2;
resTable := cp::resTable;
// match acclist
re := "yy_acclist\\[[0-9]*\\] =[^}]*}";
(numMatches,resultRegex) := System.regex(flexCode,re,1,false,false);
ar1::_ := resultRegex;
if (numMatches > 0) then
pos1 := System.stringFind(ar1,",");
pos2 := System.stringFind(ar1,"}");
ar1 := substring2(ar1,pos1+2,pos2-1);
else
ar1 := "";
end if;
resTable := ar1::resTable;
cp := suffixList+prefixList+"yy_accept"+prefixList2;
resTable := cp::resTable;
re := "yy_accept\\[[0-9]*\\] =[^}]*}";
(numMatches,resultRegex) := System.regex(flexCode,re,1,false,false);
rest::_ := resultRegex;
if (numMatches > 0) then
//rest := System.stringFindString(flexCode,re);
pos1 := System.stringFind(rest,",");
pos2 := System.stringFind(rest,"}");
ar1 := substring2(rest,pos1+2,pos2-1);
resTable := ar1::resTable;
end if;
cp := suffixList+prefixList+"yy_ec"+prefixList2;
resTable := cp::resTable;
//re := "static yyconst int yy_ec";
re := "yy_ec\\[[0-9]*\\] =[^}]*}";
(numMatches,resultRegex) := System.regex(flexCode,re,1,false,false);
rest::_ := resultRegex;
if (numMatches > 0) then
//rest := System.stringFindString(flexCode,re);
pos1 := System.stringFind(rest,",");
pos2 := System.stringFind(rest,"}");
ar1 := substring2(rest,pos1+2,pos2-1);
resTable := ar1::resTable;
end if;
cp := suffixList+prefixList+"yy_meta"+prefixList2;
resTable := cp::resTable;
//re := "static yyconst int yy_meta";
re := "yy_meta\\[[0-9]*\\] =[^}]*}";
(numMatches,resultRegex) := System.regex(flexCode,re,1,false,false);
rest::_ := resultRegex;
if (numMatches > 0) then
//rest := System.stringFindString(flexCode,re);
pos1 := System.stringFind(rest,",");
pos2 := System.stringFind(rest,"}");
ar1 := substring2(rest,pos1+2,pos2-1);
resTable := ar1::resTable;
end if;
cp := suffixList+prefixList+"yy_base"+prefixList2;
resTable := cp::resTable;
//re := "static yyconst short int yy_base";
re := "yy_base\\[[0-9]*\\] =[^}]*}";
(numMatches,resultRegex) := System.regex(flexCode,re,1,false,false);
rest::_ := resultRegex;
if (numMatches > 0) then
//rest := System.stringFindString(flexCode,re);
pos1 := System.stringFind(rest,",");
pos2 := System.stringFind(rest,"}");
ar1 := substring2(rest,pos1+2,pos2-1);
resTable := ar1::resTable;
end if;
cp := suffixList+prefixList+"yy_def"+prefixList2;
resTable := cp::resTable;
//re := "static yyconst short int yy_def";
re := "yy_def\\[[0-9]*\\] =[^}]*}";
(numMatches,resultRegex) := System.regex(flexCode,re,1,false,false);
rest::_ := resultRegex;
if (numMatches > 0) then
//rest := System.stringFindString(flexCode,re);
pos1 := System.stringFind(rest,",");
pos2 := System.stringFind(rest,"}");
ar1 := substring2(rest,pos1+2,pos2-1);
resTable := ar1::resTable;
end if;
cp := suffixList+prefixList+"yy_nxt"+prefixList2;
resTable := cp::resTable;
//re := "static yyconst short int yy_nxt";
re := "yy_nxt\\[[0-9]*\\] =[^}]*}";
(numMatches,resultRegex) := System.regex(flexCode,re,1,false,false);
rest::_ := resultRegex;
if (debug==true) then
print("\nREST next" + rest);
end if;
if (numMatches > 0) then
//rest := System.stringFindString(flexCode,re);
pos1 := System.stringFind(rest,",");
pos2 := System.stringFind(rest,"}");
ar1 := substring2(rest,pos1+2,pos2-1);
resTable := ar1::resTable;
end if;
cp := suffixList+prefixList+"yy_chk"+prefixList2;
resTable := cp::resTable;
re := "static yyconst short int yy_chk";
re := "yy_chk\\[[0-9]*\\] =[^}]*}";
(numMatches,resultRegex) := System.regex(flexCode,re,1,false,false);
rest::_ := resultRegex;
if (numMatches > 0) then
//rest := System.stringFindString(flexCode,re);
pos1 := System.stringFind(rest,",");
pos2 := System.stringFind(rest,"}");
ar1 := substring2(rest,pos1+2,pos2-1);
resTable := ar1::resTable;
end if;
cp := suffixList;
resTable := cp::resTable;
resTable := listReverse(resTable);
result := stringCharListString(resTable);
buildResult := result;
end buildLexTable;
public function getCurrentTimeStr "
returns current time in format Www Mmm dd hh:mm:ss yyyy
using the asctime() function in time.h (libc)
"
output String timeStr;
protected
Integer sec, min, hour, mday, mon, year;
algorithm
timeStr := System.getCurrentTimeStr();
/*(sec, min, hour, mday, mon, year) := System.getCurrentDateTime();
timeStr := intString(year) + "/" + intString(mon)+ "/" + intString(mday)+
" " + intString(hour)+ ":" + intString(min) + ":" + intString(sec); */
end getCurrentTimeStr;
public function substring2
input String inString;
input Integer start;
input Integer stop;
output String outString;
protected
list<String> chars, result;
String c;
Integer i;
algorithm
outString := System.substring(inString,start,stop);
/* result :={};
chars := stringListStringChar(inString);
for i in 1:stop loop
c::chars := chars;
if (i>=start) then
result := c::result;
end if;
end for;
result := listReverse(result);
outString := stringCharListString(result); */
end substring2;
function buildTokens
input String flexFile;
output String result;
protected
String cp,re,flexCode,s;
// Integer numMatches=0;
list<String> resTable,tokens,tokens2,tmp;
algorithm
cp := "type TokenId = enumeration(\n ";
resTable := cp::{};
tokens := {};
flexCode := System.readFile(flexFile);
for str in System.strtok(flexCode, "return ") loop
tmp := System.strtok(str,";");
s := if listEmpty(tmp) then "" else System.trim(listGet(tmp,1));
if 1==System.regex(s, "^[A-Z_]{2,}$",0,extended=true) then
tokens := s :: tokens;
end if;
end for;
tokens2 := "_NO_TOKEN"::List.sortedUnique(List.sort(tokens, Util.strcmpBool), stringEqual);
// numMatches := listLength(tokens2);
cp := stringDelimitList(tokens2, ",\n ");
resTable := cp::resTable;
cp := "\n);";
resTable := cp::resTable;
resTable := listReverse(resTable);
result := stringAppendList(resTable);
end buildTokens;
end LexerGenerator;