Skip to content

Commit d8c0e20

Browse files
authored
Updated Cobol85 abstractor
1 parent 5de8e0e commit d8c0e20

16 files changed

+594
-226
lines changed

ASTBasicTerm.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ public ASTTerm removeWhitespaceTerms()
4242
}
4343
return this;
4444
}
45+
46+
public ASTTerm replaceCobolIdentifiers()
47+
{ if (tag.equals("cobolWord"))
48+
{ String vtrim = value.trim();
49+
String vsub = vtrim.replace("-", "_");
50+
return new ASTBasicTerm(tag,vsub);
51+
}
52+
return this;
53+
}
4554

4655
public boolean hasTag(String tagx)
4756
{ return tagx.equals(tag); }
@@ -2654,6 +2663,10 @@ public Vector normaliseAntlr()
26542663
public static void main(String[] args)
26552664
{ ASTBasicTerm tt = new ASTBasicTerm("primaryExpression", "'a'");
26562665
System.out.println(tt.isCharacter());
2666+
2667+
ASTBasicTerm ttrr = new ASTBasicTerm("cobolWord", "PAY-CHECKS-MAY");
2668+
ASTTerm ttx = ttrr.replaceCobolIdentifiers();
2669+
System.out.println(ttx);
26572670
}
26582671

26592672

ASTCompositeTerm.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,19 @@ else if (newterms.size() == 1)
212212
return res;
213213
}
214214

215+
public ASTTerm replaceCobolIdentifiers()
216+
{ // Each (cobolName sss) updated to be valid sss
217+
Vector newterms = new Vector();
218+
for (int i = 0; i < terms.size(); i++)
219+
{ ASTTerm trm = (ASTTerm) terms.get(i);
220+
ASTTerm ntrm = trm.replaceCobolIdentifiers();
221+
if (ntrm != null)
222+
{ newterms.add(ntrm); }
223+
}
224+
return new ASTCompositeTerm(tag,newterms);
225+
}
226+
227+
215228
public ASTTerm getTerm(int i)
216229
{ if (terms.size() > i)
217230
{ return (ASTTerm) terms.get(i); }
@@ -382,7 +395,7 @@ else if (tokens.size() == terms.size())
382395
{ String tok = (String) tokens.get(j);
383396
ASTTerm tm = (ASTTerm) terms.get(k);
384397

385-
System.out.println("$$$ matching token " + tok + " and term " + tm);
398+
// System.out.println("$$$ matching token " + tok + " and term " + tm);
386399

387400
if ("_*".equals(tok) && vars.contains(tok))
388401
{ // remainder of terms is processed as a list
@@ -393,7 +406,7 @@ else if (tokens.size() == terms.size())
393406
if (tokens.size() > j+1)
394407
{ nextTok = (String) tokens.get(j+1); }
395408

396-
System.out.println(">> Terminator token for _* is: " + nextTok);
409+
// System.out.println(">> Terminator token for _* is: " + nextTok);
397410
int remainingTokens = tokens.size() - (j+1);
398411

399412
boolean finished = false;
@@ -405,9 +418,9 @@ else if (tokens.size() == terms.size())
405418

406419
if (nextTok != null &&
407420
pterm.literalForm().equals(nextTok))
408-
{ System.out.println("$$$ Matched terminator token " +
409-
nextTok +
410-
" for _* and term " + pterm);
421+
{ // System.out.println("$$$ Matched terminator token " +
422+
// nextTok +
423+
// " for _* and term " + pterm);
411424
finished = true;
412425
matchedTokens.add("_*");
413426
// k++; // next term after terminator
@@ -436,7 +449,7 @@ else if ("_+".equals(tok) && vars.contains(tok))
436449
if (tokens.size() > j+1)
437450
{ nextTok = (String) tokens.get(j+1); }
438451

439-
System.out.println(">> Terminator token for _+ is: " + nextTok);
452+
// System.out.println(">> Terminator token for _+ is: " + nextTok);
440453
int remainingTokens = tokens.size() - (j+1);
441454

442455
boolean finished = false;

ASTSymbolTerm.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public ASTTerm removeWhitespaceTerms()
3636
return this;
3737
}
3838

39+
public ASTTerm replaceCobolIdentifiers()
40+
{ return this; }
41+
3942
public boolean hasTag(String tagx)
4043
{ return false; }
4144

ASTTerm.java

Lines changed: 172 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ public static Vector getLiteralForms(Vector trms)
133133

134134
public abstract ASTTerm removeWhitespaceTerms();
135135

136+
public abstract ASTTerm replaceCobolIdentifiers();
137+
136138
public abstract String tagFunction();
137139

138140
public static Vector allNestedTagsArities(Vector sasts)
@@ -207,67 +209,196 @@ public static Vector rulesFromTagsArities(Vector tagsarities)
207209

208210
public void addStereotype(String str)
209211
{ String lit = literalForm();
210-
Vector stereotypes =
211-
(Vector) ASTTerm.metafeatures.get(lit);
212-
if (stereotypes == null)
213-
{ stereotypes = new Vector();
214-
ASTTerm.metafeatures.put(lit,stereotypes);
215-
}
212+
if (ASTTerm.metafeatures.get(lit) instanceof Vector)
213+
{
214+
Vector stereotypes =
215+
(Vector) ASTTerm.metafeatures.get(lit);
216+
if (stereotypes == null)
217+
{ stereotypes = new Vector();
218+
ASTTerm.metafeatures.put(lit,stereotypes);
219+
}
216220

217-
if (stereotypes.contains(str)) {}
218-
else
219-
{ stereotypes.add(str); }
221+
System.out.println(">> Adding stereotype " + str + " to " + lit);
222+
223+
if (stereotypes.contains(str)) {}
224+
else
225+
{ stereotypes.add(str); }
226+
}
227+
}
228+
229+
public static void addStereo(ASTTerm ast, String str)
230+
{ if (ast == null) { return; }
231+
232+
String lit = ast.literalForm();
233+
addStereo(lit,str);
220234
}
221235

222236
public static void addStereo(String lit, String str)
223-
{ Vector stereotypes =
224-
(Vector) ASTTerm.metafeatures.get(lit);
225-
if (stereotypes == null)
226-
{ stereotypes = new Vector();
227-
ASTTerm.metafeatures.put(lit,stereotypes);
237+
{ Object mfs = ASTTerm.metafeatures.get(lit);
238+
if (mfs == null)
239+
{ mfs = new Vector();
240+
ASTTerm.metafeatures.put(lit, mfs);
228241
}
229242

230-
if (stereotypes.contains(str)) {}
231-
else
232-
{ stereotypes.add(str); }
243+
if (mfs instanceof Vector)
244+
{
245+
Vector stereotypes = (Vector) mfs;
246+
247+
if (stereotypes.contains(str)) {}
248+
else
249+
{ stereotypes.add(str); }
250+
}
251+
252+
System.out.println("*** Metafeatures of " + lit + " are " + mfs);
233253
}
234254

255+
public static void setTaggedValue(ASTTerm ast, String mf, String val)
256+
{ // updates stereotypes of ast so that mf=val
257+
258+
String lit = ast.literalForm();
259+
260+
setTaggedValue(lit, mf, val);
261+
}
262+
263+
264+
public static void setTaggedValue(String lit, String mf, String val)
265+
{ Object mfs = ASTTerm.metafeatures.get(lit);
266+
System.out.println("*** " + lit + " has tagged values: " +
267+
mfs);
268+
269+
if (mfs == null)
270+
{ mfs = new Vector();
271+
ASTTerm.metafeatures.put(lit,mfs);
272+
}
273+
274+
if (mfs instanceof Vector)
275+
{ Vector stereos = (Vector) mfs;
276+
Vector newstereos = new Vector();
277+
for (int x = 0; x < stereos.size(); x++)
278+
{ String stereo = (String) stereos.get(x);
279+
if (stereo.startsWith(mf + "="))
280+
{ }
281+
else
282+
{ newstereos.add(stereo); }
283+
}
284+
newstereos.add(mf + "=" + val);
285+
ASTTerm.metafeatures.put(lit,newstereos);
286+
System.out.println("*** Set " + lit + " tagged values: " +
287+
newstereos);
288+
}
289+
}
290+
235291

236292
public void removeStereotype(String str)
237293
{ String lit = literalForm();
238-
Vector stereotypes =
239-
(Vector) ASTTerm.metafeatures.get(lit);
240-
if (stereotypes == null)
241-
{ stereotypes = new Vector();
242-
ASTTerm.metafeatures.put(lit,stereotypes);
294+
if (ASTTerm.metafeatures.get(lit) instanceof Vector)
295+
{
296+
Vector stereotypes =
297+
(Vector) ASTTerm.metafeatures.get(lit);
298+
if (stereotypes == null)
299+
{ stereotypes = new Vector();
300+
ASTTerm.metafeatures.put(lit,stereotypes);
301+
}
302+
Vector removed = new Vector();
303+
removed.add(str);
304+
stereotypes.removeAll(removed);
243305
}
244-
Vector removed = new Vector();
245-
removed.add(str);
246-
stereotypes.removeAll(removed);
247306
}
248307

249308
public static void removeStereo(String lit, String str)
250-
{
251-
Vector stereotypes =
252-
(Vector) ASTTerm.metafeatures.get(lit);
253-
if (stereotypes == null)
254-
{ stereotypes = new Vector();
255-
ASTTerm.metafeatures.put(lit,stereotypes);
256-
}
257-
Vector removed = new Vector();
258-
removed.add(str);
259-
stereotypes.removeAll(removed);
309+
{ if (ASTTerm.metafeatures.get(lit) instanceof Vector)
310+
{
311+
Vector stereotypes =
312+
(Vector) ASTTerm.metafeatures.get(lit);
313+
if (stereotypes == null)
314+
{ stereotypes = new Vector();
315+
ASTTerm.metafeatures.put(lit,stereotypes);
316+
}
317+
Vector removed = new Vector();
318+
removed.add(str);
319+
stereotypes.removeAll(removed);
320+
}
260321
}
261322

262323
public boolean hasStereotype(String str)
263324
{ String lit = literalForm();
264-
Vector stereotypes =
265-
(Vector) ASTTerm.metafeatures.get(lit);
266-
if (stereotypes == null)
267-
{ stereotypes = new Vector();
268-
ASTTerm.metafeatures.put(lit,stereotypes);
325+
if (ASTTerm.metafeatures.get(lit) instanceof Vector)
326+
{ Vector stereotypes =
327+
(Vector) ASTTerm.metafeatures.get(lit);
328+
if (stereotypes == null)
329+
{ stereotypes = new Vector();
330+
ASTTerm.metafeatures.put(lit,stereotypes);
331+
}
332+
return stereotypes.contains(str);
269333
}
270-
return stereotypes.contains(str);
334+
return false;
335+
}
336+
337+
public static String getStereotypeValue(String lit)
338+
{ Object stereo =
339+
ASTTerm.metafeatures.get(lit);
340+
System.out.println(">>>--- Global variable " + lit +
341+
" has value " + stereo);
342+
System.out.println();
343+
if (stereo == null)
344+
{ return ""; }
345+
else
346+
{ return "" + stereo; }
347+
}
348+
349+
public static void setStereotypeValue(String lit, String val)
350+
{ System.out.println(">>>--- Global variable " + lit +
351+
" set to " + val);
352+
System.out.println();
353+
ASTTerm.metafeatures.put(lit,val);
354+
}
355+
356+
public static boolean hasTaggedValue(ASTTerm trm, String str)
357+
{ String lit = trm.literalForm();
358+
System.out.println("*** " + lit + " has tagged values: " +
359+
ASTTerm.metafeatures.get(lit));
360+
361+
if (ASTTerm.metafeatures.get(lit) instanceof Vector)
362+
{
363+
Vector stereotypes =
364+
(Vector) ASTTerm.metafeatures.get(lit);
365+
if (stereotypes == null)
366+
{ stereotypes = new Vector();
367+
ASTTerm.metafeatures.put(lit,stereotypes);
368+
}
369+
370+
for (int x = 0; x < stereotypes.size(); x++)
371+
{ String stereo = (String) stereotypes.get(x);
372+
if (stereo.startsWith(str + "="))
373+
{ return true; }
374+
}
375+
return false;
376+
}
377+
return false;
378+
}
379+
380+
public static String getTaggedValue(ASTTerm trm, String str)
381+
{ String lit = trm.literalForm();
382+
Object mfs = ASTTerm.metafeatures.get(lit);
383+
if (mfs == null)
384+
{ mfs = new Vector();
385+
ASTTerm.metafeatures.put(lit,mfs);
386+
}
387+
388+
System.out.println("*** " + lit + " gets tagged values: " +
389+
mfs);
390+
391+
if (mfs instanceof Vector)
392+
{ Vector stereotypes = (Vector) mfs;
393+
for (int x = 0; x < stereotypes.size(); x++)
394+
{ String stereo = (String) stereotypes.get(x);
395+
if (stereo.startsWith(str + "="))
396+
{ int indx = stereo.indexOf("=");
397+
return stereo.substring(indx + 1);
398+
}
399+
}
400+
}
401+
return null;
271402
}
272403

273404
public static void addRequiredLibrary(String lib)

0 commit comments

Comments
 (0)