@@ -713,20 +713,20 @@ public void nospacelexicalanalysis(String str)
713713 { char c = str .charAt (i );
714714 if (in == INUNKNOWN )
715715 { if (isSymbolCharacter (c ))
716- { sb = new StringBuffer (); // start new buffer for the symbol
716+ { sb = new StringBuffer (); // new buffer for symbol
717717 lexicals .addElement (sb );
718718 in = INSYMBOL ;
719719 sb .append (c );
720720 previous = c ;
721721 }
722722 else if (isBasicExpCharacter (c ))
723- { sb = new StringBuffer (); // start new buffer for the expression
723+ { sb = new StringBuffer (); // new buffer for expression
724724 lexicals .addElement (sb );
725725 in = INBASICEXP ;
726726 sb .append (c );
727727 }
728728 else if (isStringDelimiter (c ))
729- { sb = new StringBuffer (); // start new buffer for the string
729+ { sb = new StringBuffer (); // new buffer for the string
730730 lexicals .addElement (sb );
731731 in = INSTRING ;
732732 sb .append ('"' );
@@ -742,18 +742,19 @@ else if (c == ' ' || c == '\n' || c == '\t' || c == '\r')
742742 in = INBASICEXP ;
743743 }
744744 else
745- { System .err .println ("Unrecognised literal : " + c ); }
745+ { System .err .println ("!! Unrecognised UML/OCL token : " + c ); }
746746 }
747747 }
748748 else if (in == INBASICEXP )
749- { if (isBasicExpCharacter (c ) || c == '"' ) // Why allow " in a basic exp???
750- { sb .append (c ); } // carry on adding to current basic exp
749+ { if (isBasicExpCharacter (c ) ||
750+ c == '"' ) // Why allow " in a basic exp???
751+ { sb .append (c ); } // carry on adding to current basic exp
751752 else if (c == '_' )
752753 { // System.out.println("Metavariable symbol: " + c);
753754 sb .append (c );
754755 }
755756 else if (isSymbolCharacter (c ))
756- { sb = new StringBuffer (); // start new buffer for the symbol
757+ { sb = new StringBuffer (); // new buffer for symbol
757758 lexicals .addElement (sb );
758759 in = INSYMBOL ;
759760 sb .append (c );
@@ -765,27 +766,27 @@ else if (c == ' ' || c == '\n' || c == '\t' || c == '\r')
765766 { sb = new StringBuffer (); // unrecognised lexical
766767 lexicals .addElement (sb );
767768 in = INUNKNOWN ;
768- System .err .println ("Unrecognised literal in expression: " + c );
769+ System .err .println ("!! Unrecognised token in expression: " + c );
769770 sb .append (c );
770771 }
771772 }
772773 else if (in == INSYMBOL )
773774 { if (isStringDelimiter (c ))
774- { sb = new StringBuffer (); // start new buffer for the string
775+ { sb = new StringBuffer (); // new buffer for the string
775776 lexicals .addElement (sb );
776777 in = INSTRING ;
777778 sb .append ('"' );
778779 }
779780 else if (c == '(' || c == ')' )
780- { sb = new StringBuffer (); // start new buffer for the new symbol
781+ { sb = new StringBuffer (); // new buffer for new symbol
781782 lexicals .addElement (sb );
782783 in = INSYMBOL ;
783784 previous = c ;
784785 sb .append (c );
785786 }
786787 else if (c == '_' )
787788 { // System.out.println("Metavariable symbol: " + c);
788- sb = new StringBuffer (); // start new buffer for the string
789+ sb = new StringBuffer (); // new buffer for the string
789790 lexicals .addElement (sb );
790791 in = INBASICEXP ;
791792 sb .append (c );
@@ -1091,6 +1092,50 @@ else if (c == ' ' || c == '\n' ||
10911092 }
10921093 }
10931094
1095+ public void filterLexicals ()
1096+ { // removes invalid sequences such as ; ; and ; )
1097+
1098+ Vector newlexicals = new Vector ();
1099+ boolean previousSemi = false ; // last token was ';'
1100+ StringBuffer semi = new StringBuffer ();
1101+ semi .append (";" );
1102+
1103+ for (int i = 0 ; i < lexicals .size (); i ++)
1104+ { StringBuffer sb = (StringBuffer ) lexicals .get (i );
1105+ String ss = sb + "" ;
1106+ if (";" .equals (ss ))
1107+ { if (previousSemi ) { } // skip the token
1108+ else
1109+ { previousSemi = true ; }
1110+ }
1111+ else if (")" .equals (ss ))
1112+ { if (previousSemi )
1113+ { previousSemi = false ;
1114+ newlexicals .add (sb );
1115+ }
1116+ else
1117+ { newlexicals .add (sb ); }
1118+ }
1119+ else if ("else" .equals (ss ))
1120+ { if (previousSemi )
1121+ { previousSemi = false ;
1122+ newlexicals .add (sb );
1123+ }
1124+ else
1125+ { newlexicals .add (sb ); }
1126+ }
1127+ else if (previousSemi )
1128+ { newlexicals .add (semi );
1129+ previousSemi = false ;
1130+ newlexicals .add (sb );
1131+ }
1132+ else
1133+ { newlexicals .add (sb ); }
1134+ }
1135+
1136+ lexicals = newlexicals ;
1137+ }
1138+
10941139 public void nospacelexicalanalysisAST (String str )
10951140 { int in = INUNKNOWN ;
10961141 char previous = ' ' ;
@@ -2771,6 +2816,13 @@ public BinaryExpression parse_implies_expression(int bcount, int pstart,
27712816 }
27722817 }
27732818
2819+
2820+ public void showLexicals ()
2821+ { int sze = lexicals .size ();
2822+ String lexs = showLexicals (0 ,sze -1 );
2823+ System .out .println (lexs );
2824+ }
2825+
27742826 public String showLexicals (int st , int en )
27752827 { String res = "" ;
27762828 for (int i = st ; i <= en ; i ++)
@@ -3408,10 +3460,19 @@ else if (i + 3 <= pend) // && "(".equals(lexicals.get(i+2) + "") &&
34083460 { // System.out.println(">> Trying to parse at " + ss2);
34093461 Expression ee1 = parse_expression (bc +1 ,i +3 ,pend -1 ,entities ,types );
34103462 if (ee1 == null ) { continue ; }
3411- Expression ee2 = parse_factor_expression (bc ,pstart ,i -1 ,entities ,types );
3463+
3464+ Expression ee2 =
3465+ parse_factor_expression (bc ,pstart ,i -1 ,
3466+ entities ,types );
3467+
34123468 if (ee2 == null ) { continue ; }
3469+
34133470 if ("selectRows" .equals (ss2 ))
3414- { // convert it
3471+ { // convert it to
3472+ // MathLib.dataTableFromRows(
3473+ // MathLib.rowsOfDataTable(ee2)->select(
3474+ // $row | ee1[$row/ee2] ) )
3475+
34153476 BasicExpression realarg =
34163477 BasicExpression .newStaticCallBasicExpression (
34173478 "rowsOfDataTable" , "MathLib" , ee2 );
@@ -3429,8 +3490,32 @@ else if (i + 3 <= pend) // && "(".equals(lexicals.get(i+2) + "") &&
34293490 "dataTableFromRows" , "MathLib" , be );
34303491 return res ;
34313492 }
3493+
3494+ if ("selectElements" .equals (ss2 ))
3495+ { // convert it to
3496+ // MatrixLib.selectElements(ee2,
3497+ // lambda $x : double in ee1[$x/ee2])
3498+
3499+ BasicExpression svar =
3500+ BasicExpression .newVariableBasicExpression ("$x" );
3501+ svar .isEvent = false ;
3502+ Type dtype = new Type ("double" , null );
3503+ Expression subee1 =
3504+ ee1 .substituteEq ("" + ee2 , svar );
3505+ Expression func =
3506+ UnaryExpression .newLambdaUnaryExpression (
3507+ "$x" , dtype , subee1 );
3508+ Vector pars = new Vector ();
3509+ pars .add (ee2 );
3510+ pars .add (func );
3511+ BasicExpression res =
3512+ BasicExpression .newStaticCallBasicExpression (
3513+ "selectElements" , "MatrixLib" , pars );
3514+ return res ;
3515+ }
34323516
3433- BinaryExpression be = new BinaryExpression (ss +ss2 ,ee2 ,ee1 );
3517+ BinaryExpression be =
3518+ new BinaryExpression (ss +ss2 ,ee2 ,ee1 );
34343519 // System.out.println(">>> Parsed binary -> expression: " + be);
34353520 return be ;
34363521 }
@@ -3648,7 +3733,8 @@ public Expression parse_basic_expression(int bc,
36483733 // return null;
36493734 }
36503735
3651- if (pstart < pend && "]" .equals (lexicals .get (pend ) + "" ) &&
3736+ if (pstart < pend &&
3737+ "]" .equals (lexicals .get (pend ) + "" ) &&
36523738 "[" .equals (lexicals .get (pstart +1 ) + "" ))
36533739 { // iden[indexes]
36543740
@@ -11257,7 +11343,7 @@ public NLPSentence parseRoot(int st, int en)
1125711343 { String tag = "" + lexicals .get (st +1 );
1125811344 NLPSentence sent = parseSentence (st +2 ,en -1 );
1125911345 if (sent == null )
11260- { System .err .println ("!! Not a sentence: " + showLexicals (st +2 ,en -1 ));
11346+ { System .err .println ("!! Error: Not a sentence: " + showLexicals (st +2 ,en -1 ));
1126111347 return null ;
1126211348 }
1126311349 return sent ;
@@ -11299,10 +11385,36 @@ public static void main(String[] args)
1129911385
1130011386 // c.nospacelexicalanalysis("Map{ \"Name\" |-> Sequence{\"Braund, Mr. Owen Harris\"}->union(Sequence{\"Allen, Mr. William Henry\"}->union(Sequence{ \"Bonnell, Miss. Elizabeth\" })) }->union(Map{ \"Age\" |-> Sequence{22}->union(Sequence{35}->union(Sequence{ 58 })) }->union(Map{ \"Sex\" |-> Sequence{\"male\"}->union(Sequence{\"male\"}->union(Sequence{ \"female\" })) }->union(Map{ \"Fare\" |-> Sequence{102.0}->union(Sequence{99.0}->union(Sequence{ 250.0 })) }) ) )");
1130111387
11302- c .nospacelexicalanalysis ("table->selectRows(table->at(p) > v)" );
11303- Expression zz = c .parseExpression ();
11388+
11389+
11390+ c .nospacelexicalanalysis ("table->restrict(table > v)" );
11391+ BinaryExpression zz = (BinaryExpression ) c .parseExpression ();
11392+
11393+ Expression zleft = zz .getLeft ();
11394+ zleft .setType (new Type ("Sequence" , null ));
11395+
11396+ System .out .println (zz );
11397+
11398+ Expression yy = zz .transformPythonSelectExpressions ();
11399+
11400+ System .out .println (yy );
11401+
11402+ /*
11403+ c.nospacelexicalanalysis("execute (OclFile[\"system.in\"]).println(x)");
1130411404
11305- System .out .println (zz );
11405+ Statement stat = c.parseStatement();
11406+
11407+ System.out.println(stat);
11408+
11409+ c.showLexicals(); */
11410+
11411+ // c.filterLexicals();
11412+
11413+ // c.showLexicals();
11414+
11415+ // stat = c.parseStatement();
11416+
11417+ // System.out.println(stat);
1130611418
1130711419 /* zz.typeCheck(new Vector(), new Vector(), new Vector(), new Vector());
1130811420
0 commit comments