@@ -178,6 +178,30 @@ public ASTTerm getTerm(int i)
178178 return null;
179179 }
180180
181+ public Vector allNestedTagsArities()
182+ { Vector res = new Vector();
183+ Vector pair = new Vector();
184+ pair.add(tag); pair.add(terms.size());
185+ res.add(pair);
186+ for (int i = 0; i < terms.size(); i++)
187+ { ASTTerm tt = (ASTTerm) terms.get(i);
188+ res.addAll(tt.allNestedTagsArities());
189+ }
190+ return res;
191+ }
192+
193+ public Vector allTagsArities()
194+ { Vector res = new Vector();
195+ Vector pair = new Vector();
196+ pair.add(tag); pair.add(terms.size());
197+ res.add(pair);
198+ for (int i = 0; i < terms.size(); i++)
199+ { ASTTerm tt = (ASTTerm) terms.get(i);
200+ res.addAll(tt.allTagsArities());
201+ }
202+ return res;
203+ }
204+
181205 public Vector getTerms()
182206 { return terms; }
183207
@@ -39642,7 +39666,206 @@ public String getJSLabel()
3964239666 }
3964339667
3964439668
39669+ public String antlr2cstl()
39670+ { System.out.println(">>> Tag: " + tag + " terms: " +
39671+ terms.size());
39672+ if (tag.equals("rules"))
39673+ { String res = "";
39674+ for (int i = 0; i < terms.size(); i++)
39675+ { ASTCompositeTerm tt = (ASTCompositeTerm) terms.get(i);
39676+ res = res + tt.antlr2cstl() + "\n\n";
39677+ }
39678+ return res;
39679+ }
39680+
39681+ if (tag.equals("ruleSpec") ||
39682+ tag.equals("ruleBlock") ||
39683+ tag.equals("labeledAlt"))
39684+ { ASTCompositeTerm t1 = (ASTCompositeTerm) terms.get(0);
39685+ return t1.antlr2cstl();
39686+ }
39687+
39688+ if (tag.equals("parserRuleSpec"))
39689+ { String rname = "";
39690+ for (int i = 0; i < terms.size(); i++)
39691+ { ASTTerm tt = (ASTTerm) terms.get(i);
39692+ if (tt instanceof ASTSymbolTerm)
39693+ { String symb = tt + "";
39694+ if (":".equals(symb))
39695+ { if (i < terms.size() - 1)
39696+ { ASTCompositeTerm rblock =
39697+ (ASTCompositeTerm) terms.get(i+1);
39698+ String blockcode = rblock.antlr2cstl();
39699+ return rname + "::\n" + blockcode;
39700+ }
39701+ return rname + "::\n_* |-->";
39702+ }
39703+ else
39704+ { rname = symb; }
39705+ }
39706+ }
39707+ }
39708+
39709+ if ("ruleAltList".equals(tag) || "altList".equals(tag))
39710+ { String res = "";
39711+ for (int i = 0; i < terms.size(); i++)
39712+ { ASTTerm tt = (ASTTerm) terms.get(i);
39713+ String symb = tt + "";
39714+ if ("|".equals(symb))
39715+ { res = res + "\n"; }
39716+ if (tt instanceof ASTCompositeTerm)
39717+ { ASTCompositeTerm ct = (ASTCompositeTerm) tt;
39718+ Vector normalisedct =
39719+ ct.normaliseAntlr();
39720+ System.out.println(">>> " + this + " >> Normalised= " + normalisedct);
39721+ for (int j = 0; j < normalisedct.size(); j++)
39722+ { Vector pathj = (Vector) normalisedct.get(j);
39723+ ASTCompositeTerm newct = new ASTCompositeTerm("alternative", pathj);
39724+ res = res + newct.antlr2cstl() + "\n";
39725+ }
39726+ }
39727+ }
39728+ return res;
39729+ }
39730+
39731+ if ("alternative".equals(tag))
39732+ { String res = "";
39733+ for (int i = 0; i < terms.size(); i++)
39734+ { ASTTerm tt = (ASTTerm) terms.get(i);
39735+ String tg = tt.getTag();
39736+ if ("element".equals(tg) || "atom".equals(tg))
39737+ { String elem = tt.antlrElement2cstl(i);
39738+ res = res + elem;
39739+ }
39740+ }
39741+ return res + " |-->";
39742+ }
39743+
39744+ return "";
39745+ }
39746+
39747+ public String antlrElement2cstl(int i)
39748+ { if ("atom".equals(tag))
39749+ { ASTTerm t1 = (ASTTerm) terms.get(0);
39750+ return t1.antlrElement2cstl(i);
39751+ }
39752+
39753+ if ("terminal".equals(tag))
39754+ { ASTTerm t1 = (ASTTerm) terms.get(0);
39755+ return t1 + " ";
39756+ }
39757+
39758+ if ("ruleref".equals(tag))
39759+ { return "_" + (i+1) + " "; }
39760+
39761+ if ("element".equals(tag) && terms.size() == 2)
39762+ { ASTTerm trm = (ASTTerm) terms.get(0);
39763+ ASTTerm suffix = (ASTTerm) terms.get(1);
39764+ return "_" + (i+1) + suffix.antlr2cstl() + " ";
39765+ }
39766+
39767+ if ("element".equals(tag))
39768+ { ASTTerm t1 = (ASTTerm) terms.get(0);
39769+ return t1.antlrElement2cstl(i);
39770+ }
39771+
39772+ return "";
39773+ }
39774+
39775+ public Vector normaliseAntlr()
39776+ { Vector alternatives = new Vector();
39777+ Vector emptyAlt = new Vector();
39778+ alternatives.add(emptyAlt);
39779+
39780+ // result for an alternative is all combinations of
39781+ // possibilities for each of its elements
3964539782
39783+ if (tag.equals("labeledAlt"))
39784+ { ASTCompositeTerm t1 = (ASTCompositeTerm) terms.get(0);
39785+ return t1.normaliseAntlr();
39786+ }
39787+
39788+ if ("alternative".equals(tag))
39789+ { for (int i = 0; i < terms.size(); i++)
39790+ { ASTTerm tt = (ASTTerm) terms.get(i);
39791+ String tg = tt.getTag();
39792+ if ("element".equals(tg))
39793+ { Vector elemchoices = tt.normaliseAntlr();
39794+ Vector newalts = new Vector();
39795+ for (int j = 0; j < alternatives.size(); j++)
39796+ { Vector altern = (Vector) alternatives.get(j);
39797+ for (int k = 0; k < elemchoices.size(); k++)
39798+ { Vector choice = (Vector) elemchoices.get(k);
39799+ Vector newbranch = new Vector();
39800+ newbranch.addAll(altern);
39801+ newbranch.addAll(choice);
39802+ newalts.add(newbranch);
39803+ }
39804+ }
39805+ alternatives = newalts;
39806+ }
39807+ }
39808+ return alternatives;
39809+ }
39810+
39811+ if ("atom".equals(tag))
39812+ { ASTTerm t1 = (ASTTerm) terms.get(0);
39813+ emptyAlt.add(t1);
39814+ return alternatives;
39815+ } // Sequence{ Sequence{t1} }
39816+
39817+ if ("terminal".equals(tag))
39818+ { ASTTerm t1 = (ASTTerm) terms.get(0);
39819+ emptyAlt.add(t1);
39820+ return alternatives;
39821+ }
39822+
39823+ if ("ruleref".equals(tag))
39824+ { emptyAlt.add(this);
39825+ return alternatives;
39826+ }
39827+
39828+ if ("element".equals(tag) && terms.size() == 2)
39829+ { ASTTerm trm = (ASTTerm) terms.get(0);
39830+ ASTTerm suffix = (ASTTerm) terms.get(1);
39831+ String suf = suffix.literalForm();
39832+ if ("?".equals(suf))
39833+ { emptyAlt.add(trm);
39834+ Vector newEmpty = new Vector();
39835+ alternatives.add(newEmpty);
39836+ }
39837+ else if ("+".equals(suf))
39838+ { emptyAlt.add(trm);
39839+ ASTBasicTerm ts1 = new ASTBasicTerm("terminal", "'_*'");
39840+ ASTCompositeTerm terminalStar =
39841+ new ASTCompositeTerm("atom", ts1);
39842+ emptyAlt.add(terminalStar);
39843+ }
39844+ else if ("*".equals(suf))
39845+ { Vector newalts = new Vector();
39846+ Vector newEmpty = new Vector();
39847+ newalts.add(newEmpty);
39848+ newalts.addAll(alternatives);
39849+ // emptyAlt.add(trm);
39850+ ASTBasicTerm ts1 = new ASTBasicTerm("terminal", "'_*'");
39851+ ASTCompositeTerm terminalStar =
39852+ new ASTCompositeTerm("atom", ts1);
39853+ emptyAlt.add(terminalStar);
39854+ alternatives = newalts;
39855+ }
39856+
39857+ return alternatives;
39858+ }
39859+
39860+ if ("element".equals(tag))
39861+ { ASTTerm t1 = (ASTTerm) terms.get(0);
39862+ emptyAlt.add(t1);
39863+ return alternatives;
39864+ }
39865+
39866+ return alternatives;
39867+ }
39868+
3964639869
3964739870 public static void main(String[] args)
3964839871 { // Testing of JS to KM3
@@ -39697,8 +39920,11 @@ public static void main(String[] args)
3969739920 Vector v1 = new Vector();
3969839921 Vector v2 = new Vector();
3969939922
39923+ String rr = xx.antlr2cstl();
39924+ System.out.println(rr);
39925+
3970039926 // Statement expr = xx.jsstatementToKM3(m1,m2,v1,v2);
39701- Vector expr =
39927+ /* Vector expr =
3970239928 ((ASTCompositeTerm) xx).jsprogramToKM3(
3970339929 m1,m2,v1,v2);
3970439930
@@ -39739,7 +39965,7 @@ else if (elem instanceof Entity)
3973939965 }
3974039966 else
3974139967 { System.out.println(elem); }
39742- }
39968+ } */
3974339969
3974439970 // Object sobj = "string object";
3974539971 // Object robj = new Object();
0 commit comments