forked from eclipse-agileuml/agileuml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler.java
More file actions
574 lines (509 loc) · 17.9 KB
/
Copy pathCompiler.java
File metadata and controls
574 lines (509 loc) · 17.9 KB
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
import java.io.*;
import java.util.Vector;
/******************************
* Copyright (c) 2003,2020 Kevin Lano
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
* *****************************/
/* This is a parser for simple expressions involving =, =>, <=>, &, or.
For example: var1 = 5 & var2 = 6 => var3 = tty
No brackets are needed: => groups expressions first, then or then &
so att = val & att2 = val2 or ggh = val4 => val5 = val6
is interpreted as
((att = val & att2 = val2) or (ggh = val4)) => (val5 = val6)
Modified to allow != (25.12.2000)
Modified to allow object references (7.4.2001)
Modified to allow <, >, etc (12.4.2002)
Modified to allow assignment := (21.4.2002)
Modified to allow +, - inside := and comparitors (4.5.2002)
Allow ":" (15.1.2003)
Allow "<:, intersect, union" (6.4.2003)
Added "*, /" and checkIfSetExpression (16.4.2003)
Introduced "|" and unspaced parsing (2.1.2006)
*/
public class Compiler
{ static final int INUNKNOWN = 0;
static final int INBASICEXP = 1;
static final int INSYMBOL = 2;
static final int INSTRING = 3;
Vector lexicals;
Expression finalexpression;
/* This method breaks up a string into a sequence of strings, using
spaces as the break points: */
public void lexicalanalysis(String str)
{
boolean intoken = false; /* True if a token has been recognised */
boolean instring = false; // true if within a string
int explen = str.length();
lexicals = new Vector(explen); /* Sequence of lexicals */
StringBuffer sb = null; /* Holds current lexical item */
for (int i = 0; i < explen; i++)
{ char c = str.charAt(i);
if (intoken)
{ if (instring)
{ if (c == '"')
{ sb.append(c); instring = false;
// intoken = false;
}
else
{ sb.append(c); }
}
else // intoken, not instring
{ if (c == '"')
{ instring = true;
// sb = new StringBuffer();
// lexicals.addElement(sb);
sb.append(c);
}
else if (c == ' ' || c == '\n')
{ intoken = false; }
else
{ sb.append(c); }
}
}
else // not intoken
{ if (c == '"')
{ intoken = true;
instring = true; sb = new StringBuffer();
lexicals.addElement(sb); sb.append(c);
}
else if (c == ' ' || c == '\n')
{ intoken = false; }
else
{ intoken = true; sb = new StringBuffer();
lexicals.addElement(sb); sb.append(c);
}
}
}
}
/* if (str.charAt(i) == ' ' || str.charAt(i) == '\n')
{
if (intoken) { intoken = false; }
}
else
{
if (intoken == false)
{
intoken = true;
sb = new StringBuffer();
lexicals.addElement(sb);
}
sb.append(str.charAt(i));
}
}
} */
private boolean isBasicExpCharacter(char c)
{ return (Character.isLetterOrDigit(c) || c == '.' || c == '(' || c == ')' ||
c == '{' || c == '}' || c == '[' || c == ']' || c == ',');
}
private boolean isSymbolCharacter(char c)
{ return (c == '<' || c == '>' || c == '=' || c == '*' || c == '/' || c == '-' ||
c == '\'' || c == '+' || c == '&' || c == '^' || c == ':' || c == '|' ||
c == '#');
}
public void nospacelexicalanalysis(String str)
{ int in = INUNKNOWN;
char previous;
int explen = str.length();
lexicals = new Vector(explen); /* Sequence of lexicals */
StringBuffer sb = null; /* Holds current lexical item */
for (int i = 0; i < explen; i++)
{ char c = str.charAt(i);
if (in == INBASICEXP)
{ if (isBasicExpCharacter(c) || c == '"')
{ sb.append(c); } // carry on adding to current basic exp
else if (isSymbolCharacter(c))
{ sb = new StringBuffer(); // start new buffer for the symbol
lexicals.addElement(sb);
in = INSYMBOL;
sb.append(c);
}
else
{ sb = new StringBuffer(); // unrecognised lexical
lexicals.addElement(sb);
in = INUNKNOWN;
sb.append(c);
}
}
else if (in == INSYMBOL)
{ if (c == '"')
{ sb = new StringBuffer(); // start new buffer for the string
lexicals.addElement(sb);
in = INSTRING;
sb.append(c);
}
else if (c == '(' || c == ')')
{ sb = new StringBuffer(); // start new buffer for the new symbol
lexicals.addElement(sb);
in = INSYMBOL;
sb.append(c);
}
else if (isBasicExpCharacter(c))
{ sb = new StringBuffer(); // start new buffer for basic exp
lexicals.addElement(sb);
in = INBASICEXP;
sb.append(c);
}
}
previous = c;
}
}
public void displaylexs()
{
for (int i = 0; i < lexicals.size(); i++)
{
System.out.println(((StringBuffer) lexicals.elementAt(i)).toString());
}
}
public Expression parse()
{
Expression ee = parse_expression(0,lexicals.size()-1);
finalexpression = ee;
return ee;
}
public Expression parse_expression(int pstart, int pend)
{
Expression ee = parse_bracketed_expression(pstart,pend);
if (ee == null)
{ ee = parse_binary_expression(pstart,pend);
if (ee == null)
{ System.out.println("Failed to parse binary expression " + pstart +
".." + pend + " -- trying basic");
ee = parse_basic_expression(pstart,pend);
return ee;
}
}
return ee;
}
public Expression parse_binary_expression(int pstart,
int pend)
{
Expression ee = parse_implies_expression(pstart,pend);
if (ee == null)
{ ee = parse_or_expression(pstart,pend);
if (ee == null)
{ ee = parse_and_expression(pstart,pend);
if (ee == null)
{ ee = parse_eq_expression(pstart,pend);
if (ee == null)
{ ee = parse_factor_expression(pstart,pend); }
}
}
}
return ee;
}
public BinaryExpression parse_implies_expression(int pstart,
int pend)
{
Expression e1 = null;
Expression e2 = null;
boolean found = false;
System.out.println("Trying to parse implies expression");
for (int i = pstart; i < pend; i++)
{
String ss = lexicals.elementAt(i).toString();
System.out.println(ss);
if (ss.equals("=>") || ss.equals("<=>") || ss.equals("#"))
{
e1 = parse_binary_expression(pstart,i-1);
e2 = parse_binary_expression(i+1,pend);
if (e1 == null || e2 == null)
{ return null; }
else
{ BinaryExpression ee = new BinaryExpression(ss,e1,e2);
System.out.println("Parsed implies expression: " + ee);
return ee; }
}
}
return null;
}
public BinaryExpression parse_or_expression(int pstart,
int pend)
{
Expression e1 = null;
Expression e2 = null;
System.out.println("Trying to parse or expression");
for (int i = pstart; i < pend; i++)
{
String ss = lexicals.elementAt(i).toString();
if (ss.equals("or"))
{
e1 = parse_binary_expression(pstart,i-1);
e2 = parse_binary_expression(i+1,pend);
if (e1 == null || e2 == null)
{ return null; }
else
{ BinaryExpression ee = new BinaryExpression(ss,e1,e2);
System.out.println("Parsed or expression: " + ee);
return ee; }
}
}
return null;
}
public BinaryExpression parse_and_expression(int pstart,
int pend)
{
Expression e1 = null;
Expression e2 = null;
System.out.println("Trying to parse and expression");
for (int i = pstart; i < pend; i++)
{
String ss = lexicals.elementAt(i).toString();
if (ss.equals("&"))
{
e1 = parse_binary_expression(pstart,i-1);
e2 = parse_binary_expression(i+1,pend);
if (e1 == null || e2 == null)
{ } // return null; }
else
{ BinaryExpression ee = new BinaryExpression(ss,e1,e2);
System.out.println("Parsed and expression: " + ee);
return ee;
}
}
}
return null;
}
public BinaryExpression parse_eq_expression(int pstart,
int pend)
{ Expression e1 = null;
Expression e2 = null;
System.out.println("Trying to parse eq. expression");
for (int i = pstart; i < pend; i++)
{
String ss = lexicals.elementAt(i).toString();
if (ss.equals(":=") || ss.equals(":") || ss.equals("<:") ||
ss.equals("/:") || ss.equals("/<:") ||
Expression.comparitors.contains(ss))
{ e1 = parse_factor_expression(pstart,i-1);
e2 = parse_factor_expression(i+1,pend);
if (e1 == null || e2 == null)
{ return null; }
else
{ BinaryExpression ee = new BinaryExpression(ss,e1,e2);
System.out.println("Parsed equality expression: " + ee);
return ee;
}
}
}
return null;
}
public Expression parse_factor_expression(int pstart, int pend)
{ System.out.println("Trying to parse factor expression");
for (int i = pstart; i < pend; i++)
{ String ss = lexicals.get(i).toString();
if (ss.equals("+") || ss.equals("-") || ss.equals("/\\") || ss.equals("^") ||
ss.equals("\\/") || ss.equals("*") || ss.equals("/") || ss.equals("|"))
{ Expression e1 = parse_basic_expression(pstart,i-1);
Expression e2 = parse_factor_expression(i+1,pend);
if (e1 == null || e2 == null)
{ } // return null; }
else
{ BinaryExpression ee = new BinaryExpression(ss,e1,e2);
System.out.println("Parsed factor expression: " + ee);
return ee;
}
}
}
return parse_basic_expression(pstart,pend);
} // reads left to right, not putting priorities on * above +
public Expression parse_basic_expression(int pstart, int pend)
{
if (pstart == pend)
{ String ss = lexicals.elementAt(pstart).toString();
BasicExpression ee = new BasicExpression(ss,0);
Expression ef = ee.checkIfSetExpression();
// if (ef instanceof BasicExpression)
// { ((BasicExpression) ef).setPrestate(ee.getPrestate()); }
System.out.println("Parsed basic expression: " + ss + " " + ee + " " + ef);
return ef;
}
// if (pstart < pend && ")".equals(lexicals.get(pend) + "") &&
// !("(".equals(lexicals.get(pstart) + "")))
// { return parse_call_expression(pstart,pend); }
return parse_bracketed_expression(pstart,pend);
}
public Expression parse_call_expression(int pstart, int pend)
{ for (int i = pstart; i < pend; i++)
{ String ss = lexicals.get(i) + "";
if ("(".equals(ss))
{ Expression opref = parse_basic_expression(pstart,i-1);
if (opref == null) { return null; }
Vector args = parse_call_arguments(i+1,pend-1);
if (opref instanceof BasicExpression)
{ ((BasicExpression) opref).setParameters(args); }
System.out.println("parsed call expression: " + opref);
return opref;
}
}
System.out.println("Failed to parse call expression");
return null;
}
public Vector parse_call_arguments(int st, int ed)
{ Vector res = new Vector();
for (int i = st; i < ed; i++)
{ String ss = lexicals.get(i) + "";
if (",".equals(ss))
{ Expression e1 = parse_basic_expression(st,i-1);
res = parse_call_arguments(i+1,ed);
res.add(0,e1);
return res;
}
}
return res;
}
public Expression parse_bracketed_expression(int pstart, int pend)
{ if (lexicals.size() > 2 &&
lexicals.get(pstart).toString().equals("(") &&
lexicals.get(pend).toString().equals(")"))
{ Expression eg = parse_expression(pstart+1,pend-1);
System.out.println("parsed bracketed expression: " + eg);
if (eg != null)
{ eg.setBrackets(true); }
return eg;
}
return null;
}
public Statement parseStatement(int s, int e)
{ Statement st = parseSequenceStatement(s,e);
if (st == null)
{ st = parseLoopStatement(s,e); }
if (st == null)
{ st = parseConditionalStatement(s,e); }
if (st == null)
{ st = parseBasicStatement(s,e); }
return st;
}
public Statement parseSequenceStatement(int s, int e)
{ Statement s1 = null;
Statement s2 = null;
for (int i = s; i < e; i++)
{ String ss = lexicals.get(i) + "";
if (ss.equals(";"))
{ s1 = parseStatement(s,i-1);
s2 = parseStatement(i+1,e);
if (s1 == null || s2 == null) { return null; }
else
{ SequenceStatement res = new SequenceStatement();
res.addStatement(s1);
res.addStatement(s2);
System.out.println("Parsed ; statement: " + res);
return res;
}
}
}
return null;
}
public Statement parseLoopStatement(int s, int e)
{ if ("for".equals(lexicals.get(s) + ""))
{ Statement s1 = null;
Expression test = null;
for (int i = s+1; i < e; i++)
{ String ss = lexicals.get(i) + "";
if (ss.equals("do"))
{ test = parse_expression(s+1,i-1);
s1 = parseStatement(i+1, e);
if (s1 == null || test == null)
{ return null; }
WhileStatement res = new WhileStatement(test,s1);
res.setLoopKind(Statement.FOR);
if (test instanceof BinaryExpression)
{ BinaryExpression betest = (BinaryExpression) test;
if (":".equals(betest.getOperator()))
{ res.setLoopRange(betest.getLeft(), betest.getRight()); }
}
System.out.println("Parsed for statement: " + res);
return res;
}
}
}
return null;
}
public Statement parseConditionalStatement(int s, int e)
{ if ("if".equals(lexicals.get(s) + ""))
{ Statement s1 = null;
Statement s2 = null;
Expression test = null;
for (int i = s+1; i < e; i++)
{ String ss = lexicals.get(i) + "";
if (ss.equals("then"))
{ test = parse_expression(s+1,i-1);
for (int j = i+1; j < e; j++)
{ String ss1 = lexicals.get(j) + "";
if (ss1.equals("else"))
{ s1 = parseStatement(i+1, j-1);
s2 = parseBasicStatement(j+1,e);
if (s1 == null || s2 == null || test == null)
{ return null; }
IfStatement res;
if (s2.isSkip())
{ res = new IfStatement(test,s1); }
else
{ res = new IfStatement(test,s1,s2); }
System.out.println("Parsed if statement: " + res);
return res;
}
}
}
}
}
return null;
}
public Statement parseBasicStatement(int s, int e)
{ if (e == s)
{ if ("skip".equals(lexicals.get(e) + ""))
{ return new InvocationStatement("skip",null,null); }
else // operation call
{ Expression ee = parse_basic_expression(s,e);
InvocationStatement istat = new InvocationStatement(ee + "",null,null);
istat.setCallExp(ee);
return istat;
}
}
if ("(".equals(lexicals.get(s) + "") &&
")".equals(lexicals.get(e) + ""))
{ return parseStatement(s+1,e-1); } // set brackets
else
{ for (int i = s; i < e; i++)
{ String ss = lexicals.get(i) + "";
if (":=".equals(ss))
{ Expression e1 = parse_expression(s,i-1);
Expression e2 = parse_expression(i+1,e);
if (e1 == null || e2 == null)
{ return null; }
System.out.println("Parsed assignment: " + e1 + " := " + e2);
return new AssignStatement(e1,e2);
}
}
}
return null;
}
public Statement parseStatement()
{ return parseStatement(0,lexicals.size()-1); }
public static void main(String[] args)
{ Compiler2 c = new Compiler2();
// c.lexicalanalysis("( kind = initial => n : InitialNode # n.name1 = name ) & ( kind = join => n : JoinNode # n.name1 = name ) & ( kind = fork => n : ForkNode # n.name1 = name ) & ( kind = junction => ( incoming.size = 1 => n : DecisionNode # n.name1 = name ) & ( incoming.size > 1 => n : MergeNode # n.name1 = name ) )");
// c.lexicalanalysis("AG ( alpha => EX beta )");
// Expression ss = c.parse();
Invariant ss = TemporalInvariant.parseTemporalInvariant("alpha", "x > 10", "AF AX",c);
if (ss != null)
{ ss.display(); }
}
/* public class ForStatement extends Statement
{ Expression test;
Statement body;
public ForStatement(Expression t, Statement b)
{ test = t; body = b; }
} */
/* public static void main(String[] args)
{ Compiler c = new Compiler();
// c.lexicalanalysis("( x * y ) + ( ( 3 / 2 ) + 4 )");
c.lexicalanalysis("( x : B ) # ( x.name = \"felix\" & x.y = 2 )");
System.out.println(c.parse());
c.lexicalanalysis("xx + \"ui \n oop\" + \" \" ");
System.out.println(c.parse());
} */
}