Math2mat
|
00001 // $ANTLR 2.7.7 (20060906): "octaveParser.g" -> "OctaveParser.java"$ 00002 00003 package m2m.backend.octaveparser; 00004 00005 //import org.eclipse.ui.IWorkbenchPage; 00006 //import org.eclipse.ui.PlatformUI; 00007 00008 //import m2mgui.view.Editor; 00009 00010 import m2m.backend.structure.*; 00011 import java.util.Vector; 00012 import java.util.HashMap; 00013 import java.util.Iterator; 00014 00015 00016 import antlr.TokenBuffer; 00017 import antlr.TokenStreamException; 00018 import antlr.TokenStreamIOException; 00019 import antlr.ANTLRException; 00020 import antlr.LLkParser; 00021 import antlr.Token; 00022 import antlr.TokenStream; 00023 import antlr.RecognitionException; 00024 import antlr.NoViableAltException; 00025 import antlr.MismatchedTokenException; 00026 import antlr.SemanticException; 00027 import antlr.ParserSharedInputState; 00028 import antlr.collections.impl.BitSet; 00029 00030 public class OctaveParser extends antlr.LLkParser implements OctaveParserTokenTypes 00031 { 00032 00036 StructTreatment treat; 00040 Function top; 00044 boolean successful; 00045 00046 public boolean isSuccessful() { 00047 return successful; 00048 } 00049 00053 public void setTreat(StructTreatment treat) { 00054 this.treat = treat; 00055 } 00059 public void setTop(Function top) { 00060 this.top = top; 00061 } 00062 00063 public void reportError(RecognitionException e) //throws ParsingException 00064 { 00065 System.out.println("Parsing error at line " + e.line); 00066 successful=false; 00067 // IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); 00068 // Editor editor = ((Editor)page.getActiveEditor()); 00069 // editor.setParseDone(false); 00070 } 00071 00072 private HashMap<String, ReadWriteIndex> varCpt = new HashMap<String, ReadWriteIndex>(); 00073 00074 protected OctaveParser(TokenBuffer tokenBuf, int k) { 00075 super(tokenBuf,k); 00076 tokenNames = _tokenNames; 00077 } 00078 00079 public OctaveParser(TokenBuffer tokenBuf) { 00080 this(tokenBuf,1); 00081 } 00082 00083 protected OctaveParser(TokenStream lexer, int k) { 00084 super(lexer,k); 00085 tokenNames = _tokenNames; 00086 } 00087 00088 public OctaveParser(TokenStream lexer) { 00089 this(lexer,1); 00090 } 00091 00092 public OctaveParser(ParserSharedInputState state) { 00093 super(state,1); 00094 tokenNames = _tokenNames; 00095 } 00096 00102 public final void entry() throws RecognitionException, TokenStreamException, ParsingException { 00103 00104 00105 Function func = null; 00106 successful=true; 00107 00108 00109 try { // for error handling 00110 { 00111 _loop3: 00112 do { 00113 if ((LA(1)==M2M)) { 00114 pragma(); 00115 } 00116 else { 00117 break _loop3; 00118 } 00119 00120 } while (true); 00121 } 00122 func=function(); 00123 00124 this.treat.setTop(func); 00125 this.treat.clean(func.getBody(), new Vector<Element>()); 00126 00127 } 00128 catch (RecognitionException ex) { 00129 reportError(ex); 00130 recover(ex,_tokenSet_0); 00131 } 00132 } 00133 00140 public final void pragma() throws RecognitionException, TokenStreamException { 00141 00142 00143 String i1,i2; 00144 00145 00146 try { // for error handling 00147 match(M2M); 00148 i1=ident(); 00149 match(COLON); 00150 i2=ident(); 00151 00152 System.out.println("#Pramga détécté!"+i1+" "+i2); 00153 treat.addPragma(i1,i2); 00154 00155 } 00156 catch (RecognitionException ex) { 00157 reportError(ex); 00158 recover(ex,_tokenSet_1); 00159 } 00160 } 00161 00167 public final Function function() throws RecognitionException, TokenStreamException, ParsingException { 00168 Function func = null; 00169 00170 00171 Vector<Element> b = null; 00172 Function tmpTop = this.top; 00173 00174 00175 try { // for error handling 00176 func=function_begin(); 00177 b=body(); 00178 function_end(); 00179 00180 if(func == null) 00181 throw new ParsingException("Invalid function"); 00182 00183 func.addBody(b); 00184 // Create all affectations for the input and output variables 00185 this.treat.createInOutAff(func, this.varCpt); 00186 this.top = tmpTop; 00187 this.treat.setTop(this.top); 00188 00189 } 00190 catch (RecognitionException ex) { 00191 reportError(ex); 00192 recover(ex,_tokenSet_2); 00193 } 00194 return func; 00195 } 00196 00203 public final Vector<Element> affect() throws RecognitionException, TokenStreamException, ParsingException { 00204 Vector<Element> vec = null; 00205 00206 00207 String name = null; 00208 Vector<Element> exp = null; 00209 Element var = null; 00210 boolean vector = false; 00211 Integer ind = null; 00212 00213 00214 try { // for error handling 00215 name=ident(); 00216 { 00217 switch ( LA(1)) { 00218 case LPAR: 00219 { 00220 ind=tab_ind(); 00221 vector=true; 00222 break; 00223 } 00224 case EQUAL: 00225 { 00226 break; 00227 } 00228 default: 00229 { 00230 throw new NoViableAltException(LT(1), getFilename()); 00231 } 00232 } 00233 } 00234 match(EQUAL); 00235 exp=expr_lvl1(); 00236 { 00237 switch ( LA(1)) { 00238 case SEMI: 00239 { 00240 match(SEMI); 00241 break; 00242 } 00243 case FUNCTION: 00244 case END: 00245 case ENDFUNCTION: 00246 case IF: 00247 case ENDIF: 00248 case ELSEIF: 00249 case ELSE: 00250 case STRING: 00251 case ENDFOR: 00252 case FOR: 00253 case ENDWHILE: 00254 case WHILE: 00255 case PRINTF: 00256 case ERROR: 00257 case RETURN: 00258 case SWITCH: 00259 case CASE: 00260 case OTHERW: 00261 { 00262 break; 00263 } 00264 default: 00265 { 00266 throw new NoViableAltException(LT(1), getFilename()); 00267 } 00268 } 00269 } 00270 00271 if(exp == null) 00272 throw new ParsingException("Invalid assignment expression"); 00273 00274 if (!this.varCpt.containsKey(name)) { 00275 this.varCpt.put(name, new ReadWriteIndex()); 00276 } else { 00277 this.varCpt.put(name, ((ReadWriteIndex)this.varCpt.get(name)).getNextVariable()); 00278 } 00279 var = treat.createVar(name+"_m2m_"+((ReadWriteIndex)this.varCpt.get(name)).getReadIndex(), 00280 0.0, "", false).lastElement(); 00281 // If variable contains an indice, we need to treat it as a 00282 // VectorVariable and set the pragma for this variable 00283 if (vector) { 00284 if(ind == null) 00285 throw new ParsingException("Invalid vector index"); 00286 var = new VectorVariable((Variable)var, ind); 00287 treat.addPragma(name, "vector"); 00288 } 00289 00290 vec = treat.createAff((Variable)var, exp); 00291 00292 } 00293 catch (RecognitionException ex) { 00294 reportError(ex); 00295 recover(ex,_tokenSet_3); 00296 } 00297 return vec; 00298 } 00299 00303 public final String ident() throws RecognitionException, TokenStreamException { 00304 String i=new String(); 00305 00306 Token s = null; 00307 00308 try { // for error handling 00309 s = LT(1); 00310 match(STRING); 00311 i=s.getText(); 00312 } 00313 catch (RecognitionException ex) { 00314 reportError(ex); 00315 recover(ex,_tokenSet_4); 00316 } 00317 return i; 00318 } 00319 00324 public final Integer tab_ind() throws RecognitionException, TokenStreamException { 00325 Integer ind = null; 00326 00327 Token n = null; 00328 00329 try { // for error handling 00330 match(LPAR); 00331 n = LT(1); 00332 match(NUMBER); 00333 match(RPAR); 00334 ind = Integer.parseInt(n.getText()); 00335 } 00336 catch (RecognitionException ex) { 00337 reportError(ex); 00338 recover(ex,_tokenSet_5); 00339 } 00340 return ind; 00341 } 00342 00347 public final Vector<Element> expr_lvl1() throws RecognitionException, TokenStreamException, ParsingException { 00348 Vector<Element> vec = null; 00349 00350 00351 Vector<Element> tmp = null; 00352 Operation op = null; 00353 00354 00355 try { // for error handling 00356 vec=expr_lvl2(); 00357 { 00358 _loop17: 00359 do { 00360 if ((LA(1)==PLUS||LA(1)==DPLUS)) { 00361 { 00362 switch ( LA(1)) { 00363 case PLUS: 00364 { 00365 match(PLUS); 00366 break; 00367 } 00368 case DPLUS: 00369 { 00370 match(DPLUS); 00371 break; 00372 } 00373 default: 00374 { 00375 throw new NoViableAltException(LT(1), getFilename()); 00376 } 00377 } 00378 } 00379 tmp=expr_lvl2(); 00380 00381 try { 00382 op = new Addition(); 00383 vec = treat.createOp(vec,tmp,op); 00384 } 00385 catch (Exception e){ 00386 throw new ParsingException("Incomplete operation \"" + op.getOpSymbol() + "\""); 00387 } 00388 00389 } 00390 else { 00391 break _loop17; 00392 } 00393 00394 } while (true); 00395 } 00396 } 00397 catch (RecognitionException ex) { 00398 reportError(ex); 00399 recover(ex,_tokenSet_6); 00400 } 00401 return vec; 00402 } 00403 00408 public final Vector<Element> body() throws RecognitionException, TokenStreamException, ParsingException { 00409 Vector<Element> vec = null; 00410 00411 00412 Vector<Element> tmp = null; 00413 vec = new Vector<Element>(); 00414 00415 00416 try { // for error handling 00417 00418 for (String s : varCpt.keySet()) { 00419 ((ReadWriteIndex)this.varCpt.get(s)).addReadIndex(); 00420 } 00421 00422 { 00423 _loop9: 00424 do { 00425 if ((_tokenSet_7.member(LA(1)))) { 00426 tmp=instruction(); 00427 00428 if (tmp != null) { 00429 vec.addAll(tmp); 00430 } 00431 00432 } 00433 else { 00434 break _loop9; 00435 } 00436 00437 } while (true); 00438 } 00439 00440 for (String s : varCpt.keySet()) { 00441 ((ReadWriteIndex)this.varCpt.get(s)).removeReadIndex(); 00442 } 00443 00444 } 00445 catch (RecognitionException ex) { 00446 reportError(ex); 00447 recover(ex,_tokenSet_8); 00448 } 00449 return vec; 00450 } 00451 00457 public final Vector<Element> instruction() throws RecognitionException, TokenStreamException, ParsingException { 00458 Vector<Element> vec = null; 00459 00460 00461 Element el = null; 00462 00463 00464 try { // for error handling 00465 switch ( LA(1)) { 00466 case STRING: 00467 { 00468 vec=affect(); 00469 break; 00470 } 00471 case FUNCTION: 00472 case FOR: 00473 case WHILE: 00474 case SWITCH: 00475 { 00476 { 00477 switch ( LA(1)) { 00478 case FUNCTION: 00479 { 00480 el=function(); 00481 break; 00482 } 00483 case FOR: 00484 { 00485 el=loop_for(); 00486 break; 00487 } 00488 case WHILE: 00489 { 00490 el=loop_while(); 00491 break; 00492 } 00493 case SWITCH: 00494 { 00495 el=op_switch(); 00496 break; 00497 } 00498 default: 00499 { 00500 throw new NoViableAltException(LT(1), getFilename()); 00501 } 00502 } 00503 } 00504 00505 vec = new Vector<Element>(); 00506 vec.add(el); 00507 00508 break; 00509 } 00510 case IF: 00511 { 00512 vec=ifthenelse(); 00513 break; 00514 } 00515 case PRINTF: 00516 case ERROR: 00517 case RETURN: 00518 { 00519 not_instruction_cmd(); 00520 break; 00521 } 00522 default: 00523 { 00524 throw new NoViableAltException(LT(1), getFilename()); 00525 } 00526 } 00527 } 00528 catch (RecognitionException ex) { 00529 reportError(ex); 00530 recover(ex,_tokenSet_3); 00531 } 00532 return vec; 00533 } 00534 00535 public final Vector<Element> body_function() throws RecognitionException, TokenStreamException, ParsingException { 00536 Vector<Element> vec = null; 00537 00538 00539 Vector<Element> tmp = null; 00540 vec = new Vector<Element>(); 00541 00542 00543 try { // for error handling 00544 { 00545 _loop12: 00546 do { 00547 if ((_tokenSet_7.member(LA(1)))) { 00548 tmp=instruction(); 00549 00550 if (tmp != null) { 00551 vec.addAll(tmp); 00552 } 00553 00554 } 00555 else { 00556 break _loop12; 00557 } 00558 00559 } while (true); 00560 } 00561 } 00562 catch (RecognitionException ex) { 00563 reportError(ex); 00564 recover(ex,_tokenSet_9); 00565 } 00566 return vec; 00567 } 00568 00573 public final Operation cmp_op() throws RecognitionException, TokenStreamException { 00574 Operation op = null; 00575 00576 00577 try { // for error handling 00578 switch ( LA(1)) { 00579 case CEQUAL: 00580 { 00581 match(CEQUAL); 00582 op = new Equal(); 00583 break; 00584 } 00585 case CNOTEQ: 00586 { 00587 match(CNOTEQ); 00588 op = new NotEqual(); 00589 break; 00590 } 00591 case CLESS: 00592 { 00593 match(CLESS); 00594 op = new Less(); 00595 break; 00596 } 00597 case CGREAT: 00598 { 00599 match(CGREAT); 00600 op = new Greater(); 00601 break; 00602 } 00603 case CLESEQ: 00604 { 00605 match(CLESEQ); 00606 op = new LessEqual(); 00607 break; 00608 } 00609 case CGREEQ: 00610 { 00611 match(CGREEQ); 00612 op = new GreaterEqual(); 00613 break; 00614 } 00615 default: 00616 { 00617 throw new NoViableAltException(LT(1), getFilename()); 00618 } 00619 } 00620 } 00621 catch (RecognitionException ex) { 00622 reportError(ex); 00623 recover(ex,_tokenSet_10); 00624 } 00625 return op; 00626 } 00627 00632 public final Vector<Element> expr_lvl2() throws RecognitionException, TokenStreamException, ParsingException { 00633 Vector<Element> vec = null; 00634 00635 00636 Vector<Element> tmp = null; 00637 Operation op = null; 00638 00639 00640 try { // for error handling 00641 vec=expr_lvl3(); 00642 { 00643 _loop21: 00644 do { 00645 if ((LA(1)==MINUS||LA(1)==DMINUS)) { 00646 { 00647 switch ( LA(1)) { 00648 case MINUS: 00649 { 00650 match(MINUS); 00651 break; 00652 } 00653 case DMINUS: 00654 { 00655 match(DMINUS); 00656 break; 00657 } 00658 default: 00659 { 00660 throw new NoViableAltException(LT(1), getFilename()); 00661 } 00662 } 00663 } 00664 tmp=expr_lvl3(); 00665 00666 try{ 00667 op = new Subtraction(); 00668 vec = treat.createOp(vec,tmp,op); 00669 } 00670 catch (Exception e){ 00671 throw new ParsingException("Incomplete operation \"" + op.getOpSymbol() + "\""); 00672 } 00673 00674 } 00675 else { 00676 break _loop21; 00677 } 00678 00679 } while (true); 00680 } 00681 } 00682 catch (RecognitionException ex) { 00683 reportError(ex); 00684 recover(ex,_tokenSet_11); 00685 } 00686 return vec; 00687 } 00688 00693 public final Vector<Element> expr_lvl3() throws RecognitionException, TokenStreamException, ParsingException { 00694 Vector<Element> vec = null; 00695 00696 00697 Vector<Element> tmp = null; 00698 Operation op = null; 00699 00700 00701 try { // for error handling 00702 vec=expr_lvl4(); 00703 { 00704 _loop25: 00705 do { 00706 if ((LA(1)==MUL||LA(1)==DMUL)) { 00707 { 00708 switch ( LA(1)) { 00709 case MUL: 00710 { 00711 match(MUL); 00712 op = new Multiplication(); 00713 break; 00714 } 00715 case DMUL: 00716 { 00717 match(DMUL); 00718 op = new DotMultiplication(); 00719 break; 00720 } 00721 default: 00722 { 00723 throw new NoViableAltException(LT(1), getFilename()); 00724 } 00725 } 00726 } 00727 tmp=expr_lvl4(); 00728 00729 00730 try { 00731 vec = treat.createOp(vec,tmp,op); 00732 } 00733 catch (Exception e){ 00734 throw new ParsingException("Incomplete operation \"" + op.getOpSymbol() + "\""); 00735 } 00736 00737 } 00738 else { 00739 break _loop25; 00740 } 00741 00742 } while (true); 00743 } 00744 } 00745 catch (RecognitionException ex) { 00746 reportError(ex); 00747 recover(ex,_tokenSet_12); 00748 } 00749 return vec; 00750 } 00751 00756 public final Vector<Element> expr_lvl4() throws RecognitionException, TokenStreamException, ParsingException { 00757 Vector<Element> vec = null; 00758 00759 00760 Vector<Element> tmp = null; 00761 Operation op = null; 00762 00763 00764 try { // for error handling 00765 vec=expr_lvl5(); 00766 { 00767 _loop29: 00768 do { 00769 if ((LA(1)==DIV||LA(1)==DDIV)) { 00770 { 00771 switch ( LA(1)) { 00772 case DIV: 00773 { 00774 match(DIV); 00775 op = new Division(); 00776 break; 00777 } 00778 case DDIV: 00779 { 00780 match(DDIV); 00781 op = new DotDivision(); 00782 break; 00783 } 00784 default: 00785 { 00786 throw new NoViableAltException(LT(1), getFilename()); 00787 } 00788 } 00789 } 00790 tmp=expr_lvl5(); 00791 00792 try{ 00793 vec = treat.createOp(vec,tmp,op); 00794 } 00795 catch (Exception e){ 00796 throw new ParsingException("Incomplete operation \"" + op.getOpSymbol() + "\""); 00797 } 00798 00799 } 00800 else { 00801 break _loop29; 00802 } 00803 00804 } while (true); 00805 } 00806 } 00807 catch (RecognitionException ex) { 00808 reportError(ex); 00809 recover(ex,_tokenSet_13); 00810 } 00811 return vec; 00812 } 00813 00818 public final Vector<Element> expr_lvl5() throws RecognitionException, TokenStreamException, ParsingException { 00819 Vector<Element> vec = null; 00820 00821 00822 Operation op = null; 00823 Vector<Element> tmp; 00824 00825 00826 try { // for error handling 00827 switch ( LA(1)) { 00828 case MINUS: 00829 case LBRAK: 00830 case STRING: 00831 case NUMBER: 00832 { 00833 vec=func_var(); 00834 { 00835 _loop33: 00836 do { 00837 if ((LA(1)==PWR1||LA(1)==PWR2)) { 00838 { 00839 switch ( LA(1)) { 00840 case PWR1: 00841 { 00842 match(PWR1); 00843 break; 00844 } 00845 case PWR2: 00846 { 00847 match(PWR2); 00848 break; 00849 } 00850 default: 00851 { 00852 throw new NoViableAltException(LT(1), getFilename()); 00853 } 00854 } 00855 } 00856 tmp=func_var(); 00857 00858 try { 00859 op = new Power(); 00860 vec = treat.createOp(vec, tmp, op); 00861 } 00862 catch (Exception e){ 00863 throw new ParsingException("Incomplete operation \"" + op.getOpSymbol() + "\""); 00864 } 00865 00866 } 00867 else { 00868 break _loop33; 00869 } 00870 00871 } while (true); 00872 } 00873 break; 00874 } 00875 case LPAR: 00876 { 00877 match(LPAR); 00878 vec=expr_lvl1(); 00879 match(RPAR); 00880 break; 00881 } 00882 default: 00883 { 00884 throw new NoViableAltException(LT(1), getFilename()); 00885 } 00886 } 00887 } 00888 catch (RecognitionException ex) { 00889 reportError(ex); 00890 recover(ex,_tokenSet_14); 00891 } 00892 return vec; 00893 } 00894 00905 public final Vector<Element> func_var() throws RecognitionException, TokenStreamException, ParsingException { 00906 Vector<Element> vec = null; 00907 00908 00909 boolean minus = false; 00910 boolean param = false; 00911 String s = null; 00912 Vector<Element> par = null; 00913 Element el = null; 00914 Double val = null; 00915 SimpleVariable var = null; 00916 00917 00918 try { // for error handling 00919 switch ( LA(1)) { 00920 case MINUS: 00921 case STRING: 00922 case NUMBER: 00923 { 00924 { 00925 switch ( LA(1)) { 00926 case MINUS: 00927 { 00928 match(MINUS); 00929 minus = true; 00930 break; 00931 } 00932 case STRING: 00933 case NUMBER: 00934 { 00935 break; 00936 } 00937 default: 00938 { 00939 throw new NoViableAltException(LT(1), getFilename()); 00940 } 00941 } 00942 } 00943 { 00944 switch ( LA(1)) { 00945 case STRING: 00946 { 00947 s=ident(); 00948 { 00949 switch ( LA(1)) { 00950 case LPAR: 00951 { 00952 match(LPAR); 00953 par=func_param(); 00954 match(RPAR); 00955 param = true; 00956 break; 00957 } 00958 case SEMI: 00959 case CEQUAL: 00960 case CNOTEQ: 00961 case CLESS: 00962 case CGREAT: 00963 case CLESEQ: 00964 case CGREEQ: 00965 case PLUS: 00966 case DPLUS: 00967 case MINUS: 00968 case DMINUS: 00969 case MUL: 00970 case DMUL: 00971 case DIV: 00972 case DDIV: 00973 case PWR1: 00974 case PWR2: 00975 case RPAR: 00976 case COMMA: 00977 case FUNCTION: 00978 case END: 00979 case ENDFUNCTION: 00980 case IF: 00981 case ENDIF: 00982 case ELSEIF: 00983 case ELSE: 00984 case STRING: 00985 case COR: 00986 case CAND: 00987 case ENDFOR: 00988 case FOR: 00989 case COLON: 00990 case ENDWHILE: 00991 case WHILE: 00992 case PRINTF: 00993 case ERROR: 00994 case RETURN: 00995 case SWITCH: 00996 case CASE: 00997 case RBRAC: 00998 case OTHERW: 00999 { 01000 break; 01001 } 01002 default: 01003 { 01004 throw new NoViableAltException(LT(1), getFilename()); 01005 } 01006 } 01007 } 01008 01009 // Unable to call a function wich have the same name that a variable 01010 if (treat.isVariable(s) && param && par.size() > 1) { 01011 throw new RecognitionException("error"); 01012 // If the name is a variable 01013 } else if (treat.isVariable(s)) { 01014 if (!this.varCpt.containsKey(s)) { 01015 this.varCpt.put(s, new ReadWriteIndex()); 01016 } 01017 vec = new Vector<Element>(); 01018 vec = treat.createVar(s+"_m2m_"+((ReadWriteIndex)this.varCpt.get(s)).getReadIndex(), 0.0, "", minus); 01019 if (param && par.size() == 1) { 01020 el = new VectorVariable((Variable)vec.lastElement(), (int)((Variable)par.lastElement()).getVal()); 01021 ((Variable)vec.lastElement()).setType("vector"); 01022 vec.remove(vec.lastElement()); 01023 vec.add(el); 01024 } 01025 // If there is a function with no minus in front of 01026 } else if (param && !minus) { 01027 el = new GenericOperation(s); 01028 el.setName(s+treat.getNextOpCpt()); 01029 ((Operation)el).setInput(par); 01030 vec = new Vector<Element>(); 01031 vec.add(el); 01032 } else { 01033 throw new ParsingException("Variable "+s+" non déclarée au préalable."); 01034 } 01035 01036 break; 01037 } 01038 case NUMBER: 01039 { 01040 val=number(); 01041 01042 // If this is a number, create a normal variable 01043 if (minus) { 01044 val = -val; 01045 } 01046 if (!this.varCpt.containsKey("const")) { 01047 this.varCpt.put("const", new ReadWriteIndex()); 01048 } else { 01049 this.varCpt.put("const", ((ReadWriteIndex)this.varCpt.get("const").getNextVariable())); 01050 } 01051 try { 01052 vec = treat.createVar("m2m_const_"+((ReadWriteIndex)this.varCpt.get("const")).getReadIndex(), val, "const", false); 01053 } 01054 catch (Exception e){ 01055 throw new ParsingException("Invalid character for a constant variable"); 01056 } 01057 01058 break; 01059 } 01060 default: 01061 { 01062 throw new NoViableAltException(LT(1), getFilename()); 01063 } 01064 } 01065 } 01066 break; 01067 } 01068 case LBRAK: 01069 { 01070 match(LBRAK); 01071 01072 // If there is values inside brackets, we need to create a vector 01073 vec = new Vector<Element>(); 01074 var = new SimpleVariable(); 01075 var.setType("vector"); 01076 vec.add(var); 01077 01078 { 01079 switch ( LA(1)) { 01080 case MINUS: 01081 case NUMBER: 01082 { 01083 { 01084 switch ( LA(1)) { 01085 case MINUS: 01086 { 01087 match(MINUS); 01088 minus = true; 01089 break; 01090 } 01091 case NUMBER: 01092 { 01093 break; 01094 } 01095 default: 01096 { 01097 throw new NoViableAltException(LT(1), getFilename()); 01098 } 01099 } 01100 } 01101 val=number(); 01102 01103 if (minus) 01104 val = -val; 01105 var.addVal(val); 01106 minus = false; 01107 01108 { 01109 _loop46: 01110 do { 01111 if ((LA(1)==MINUS||LA(1)==COMMA||LA(1)==NUMBER)) { 01112 { 01113 switch ( LA(1)) { 01114 case COMMA: 01115 { 01116 match(COMMA); 01117 break; 01118 } 01119 case MINUS: 01120 case NUMBER: 01121 { 01122 break; 01123 } 01124 default: 01125 { 01126 throw new NoViableAltException(LT(1), getFilename()); 01127 } 01128 } 01129 } 01130 { 01131 switch ( LA(1)) { 01132 case MINUS: 01133 { 01134 match(MINUS); 01135 minus = true; 01136 break; 01137 } 01138 case NUMBER: 01139 { 01140 break; 01141 } 01142 default: 01143 { 01144 throw new NoViableAltException(LT(1), getFilename()); 01145 } 01146 } 01147 } 01148 val=number(); 01149 01150 if (minus) 01151 val = -val; 01152 var.addVal(val); 01153 minus = false; 01154 01155 } 01156 else { 01157 break _loop46; 01158 } 01159 01160 } while (true); 01161 } 01162 break; 01163 } 01164 case RBRAK: 01165 { 01166 break; 01167 } 01168 default: 01169 { 01170 throw new NoViableAltException(LT(1), getFilename()); 01171 } 01172 } 01173 } 01174 match(RBRAK); 01175 break; 01176 } 01177 default: 01178 { 01179 throw new NoViableAltException(LT(1), getFilename()); 01180 } 01181 } 01182 } 01183 catch (RecognitionException ex) { 01184 reportError(ex); 01185 recover(ex,_tokenSet_15); 01186 } 01187 return vec; 01188 } 01189 01195 public final Vector<Element> func_param() throws RecognitionException, TokenStreamException, ParsingException { 01196 Vector<Element> par = null; 01197 01198 01199 Vector<Element> vec = null; 01200 01201 01202 try { // for error handling 01203 vec=func_var(); 01204 01205 par = vec; 01206 01207 { 01208 _loop36: 01209 do { 01210 if ((LA(1)==COMMA)) { 01211 match(COMMA); 01212 vec=func_var(); 01213 01214 par.add(vec.lastElement()); 01215 01216 } 01217 else { 01218 break _loop36; 01219 } 01220 01221 } while (true); 01222 } 01223 } 01224 catch (RecognitionException ex) { 01225 reportError(ex); 01226 recover(ex,_tokenSet_16); 01227 } 01228 return par; 01229 } 01230 01235 public final Double number() throws RecognitionException, TokenStreamException { 01236 Double val = null; 01237 01238 Token n1 = null; 01239 Token n2 = null; 01240 01241 String s = null; 01242 Boolean minus = false; 01243 01244 01245 try { // for error handling 01246 n1 = LT(1); 01247 match(NUMBER); 01248 01249 s = new String(); 01250 if (minus) { 01251 s += "-"; 01252 } 01253 s += n1.getText(); 01254 01255 { 01256 switch ( LA(1)) { 01257 case DOT: 01258 { 01259 match(DOT); 01260 n2 = LT(1); 01261 match(NUMBER); 01262 s += "."+n2.getText(); 01263 break; 01264 } 01265 case SEMI: 01266 case CEQUAL: 01267 case CNOTEQ: 01268 case CLESS: 01269 case CGREAT: 01270 case CLESEQ: 01271 case CGREEQ: 01272 case PLUS: 01273 case DPLUS: 01274 case MINUS: 01275 case DMINUS: 01276 case MUL: 01277 case DMUL: 01278 case DIV: 01279 case DDIV: 01280 case PWR1: 01281 case PWR2: 01282 case RPAR: 01283 case COMMA: 01284 case RBRAK: 01285 case FUNCTION: 01286 case END: 01287 case ENDFUNCTION: 01288 case IF: 01289 case ENDIF: 01290 case ELSEIF: 01291 case ELSE: 01292 case STRING: 01293 case COR: 01294 case CAND: 01295 case ENDFOR: 01296 case FOR: 01297 case COLON: 01298 case ENDWHILE: 01299 case WHILE: 01300 case PRINTF: 01301 case ERROR: 01302 case RETURN: 01303 case NUMBER: 01304 case SWITCH: 01305 case CASE: 01306 case RBRAC: 01307 case OTHERW: 01308 { 01309 break; 01310 } 01311 default: 01312 { 01313 throw new NoViableAltException(LT(1), getFilename()); 01314 } 01315 } 01316 } 01317 01318 val = Double.parseDouble(s); 01319 01320 } 01321 catch (RecognitionException ex) { 01322 reportError(ex); 01323 recover(ex,_tokenSet_17); 01324 } 01325 return val; 01326 } 01327 01333 public final Function function_begin() throws RecognitionException, TokenStreamException, ParsingException { 01334 Function func = null; 01335 01336 01337 String name,out = null; 01338 Vector<Element> par = null; 01339 01340 01341 try { // for error handling 01342 match(FUNCTION); 01343 func = new Function(); this.top = func; this.treat.setTop(this.top); 01344 { 01345 switch ( LA(1)) { 01346 case LBRAK: 01347 { 01348 match(LBRAK); 01349 out=ident(); 01350 01351 Variable var = (Variable)treat.createInOut(out); 01352 //this.top.addOutput(var); 01353 func.addOutput(var); 01354 01355 { 01356 _loop51: 01357 do { 01358 if ((LA(1)==COMMA)) { 01359 match(COMMA); 01360 out=ident(); 01361 01362 var = (Variable)treat.createInOut(out); 01363 func.addOutput(var); 01364 01365 } 01366 else { 01367 break _loop51; 01368 } 01369 01370 } while (true); 01371 } 01372 match(RBRAK); 01373 break; 01374 } 01375 case STRING: 01376 { 01377 out=ident(); 01378 01379 Variable var = (Variable)treat.createInOut(out); 01380 func.addOutput(var); 01381 01382 break; 01383 } 01384 default: 01385 { 01386 throw new NoViableAltException(LT(1), getFilename()); 01387 } 01388 } 01389 } 01390 match(EQUAL); 01391 name=ident(); 01392 match(LPAR); 01393 par=param(); 01394 match(RPAR); 01395 01396 func.setName(name); 01397 func.setInput(par); 01398 for (Element var : par) { 01399 if (var instanceof Variable) { 01400 this.varCpt.put(treat.getTrueName(var.getName()), new ReadWriteIndex()); 01401 } 01402 } 01403 01404 } 01405 catch (RecognitionException ex) { 01406 reportError(ex); 01407 recover(ex,_tokenSet_18); 01408 } 01409 return func; 01410 } 01411 01416 public final void function_end() throws RecognitionException, TokenStreamException { 01417 01418 01419 try { // for error handling 01420 switch ( LA(1)) { 01421 case END: 01422 { 01423 match(END); 01424 match(FUNCTION); 01425 match(SEMI); 01426 break; 01427 } 01428 case ENDFUNCTION: 01429 { 01430 match(ENDFUNCTION); 01431 { 01432 switch ( LA(1)) { 01433 case SEMI: 01434 { 01435 match(SEMI); 01436 break; 01437 } 01438 case EOF: 01439 case FUNCTION: 01440 case END: 01441 case ENDFUNCTION: 01442 case IF: 01443 case ENDIF: 01444 case ELSEIF: 01445 case ELSE: 01446 case STRING: 01447 case ENDFOR: 01448 case FOR: 01449 case ENDWHILE: 01450 case WHILE: 01451 case PRINTF: 01452 case ERROR: 01453 case RETURN: 01454 case SWITCH: 01455 case CASE: 01456 case OTHERW: 01457 { 01458 break; 01459 } 01460 default: 01461 { 01462 throw new NoViableAltException(LT(1), getFilename()); 01463 } 01464 } 01465 } 01466 break; 01467 } 01468 default: 01469 { 01470 throw new NoViableAltException(LT(1), getFilename()); 01471 } 01472 } 01473 } 01474 catch (RecognitionException ex) { 01475 reportError(ex); 01476 recover(ex,_tokenSet_2); 01477 } 01478 } 01479 01484 public final Vector<Element> param() throws RecognitionException, TokenStreamException, ParsingException { 01485 Vector<Element> par = null; 01486 01487 01488 String s; 01489 Variable var; 01490 01491 01492 try { // for error handling 01493 s=ident(); 01494 01495 par = new Vector<Element>(); 01496 var = treat.createInOut(s); 01497 par.add(var); 01498 01499 { 01500 _loop96: 01501 do { 01502 if ((LA(1)==COMMA)) { 01503 match(COMMA); 01504 s=ident(); 01505 01506 var = treat.createInOut(s); 01507 par.add(var); 01508 01509 } 01510 else { 01511 break _loop96; 01512 } 01513 01514 } while (true); 01515 } 01516 } 01517 catch (RecognitionException ex) { 01518 reportError(ex); 01519 recover(ex,_tokenSet_16); 01520 } 01521 return par; 01522 } 01523 01528 public final Vector<Element> ifthenelse() throws RecognitionException, TokenStreamException, ParsingException { 01529 Vector<Element> vec = null; 01530 01531 01532 try { // for error handling 01533 match(IF); 01534 vec=ifthenelsegen(); 01535 { 01536 switch ( LA(1)) { 01537 case END: 01538 { 01539 match(END); 01540 break; 01541 } 01542 case ENDIF: 01543 { 01544 match(ENDIF); 01545 break; 01546 } 01547 default: 01548 { 01549 throw new NoViableAltException(LT(1), getFilename()); 01550 } 01551 } 01552 } 01553 } 01554 catch (RecognitionException ex) { 01555 reportError(ex); 01556 recover(ex,_tokenSet_3); 01557 } 01558 return vec; 01559 } 01560 01565 public final Vector<Element> ifthenelsegen() throws RecognitionException, TokenStreamException, ParsingException { 01566 Vector<Element> vec = null; 01567 01568 01569 IfThenElse ite = null; 01570 Vector<Element> cond = null; 01571 Vector<Element> tmp = null; 01572 IfThenElse next = null; 01573 Element var; 01574 Vector<Element> b = null; 01575 HashMap<String, Element> list1 = new HashMap<String, Element>(); 01576 HashMap<String, Element> list2 = new HashMap<String, Element>(); 01577 01578 /* Use to go through the body */ 01579 Element element; 01580 Vector<Element> elementList = new Vector<Element>(); 01581 Iterator<Element> iterElement; 01582 01583 01584 try { // for error handling 01585 match(LPAR); 01586 cond=logexpr(); 01587 match(RPAR); 01588 { 01589 switch ( LA(1)) { 01590 case THEN: 01591 { 01592 match(THEN); 01593 break; 01594 } 01595 case FUNCTION: 01596 case END: 01597 case IF: 01598 case ENDIF: 01599 case ELSEIF: 01600 case ELSE: 01601 case STRING: 01602 case FOR: 01603 case WHILE: 01604 case PRINTF: 01605 case ERROR: 01606 case RETURN: 01607 case SWITCH: 01608 { 01609 break; 01610 } 01611 default: 01612 { 01613 throw new NoViableAltException(LT(1), getFilename()); 01614 } 01615 } 01616 } 01617 b=body(); 01618 01619 // Simple check to avoid errors on condition 01620 if(cond == null) 01621 throw new ParsingException("Invalid \"if\" condition"); 01622 01623 vec = new Vector<Element>(); 01624 ite = new IfThenElse(); 01625 vec.add(ite); 01626 treat.count(ite); 01627 // Create the condition output variable 01628 Variable sv = (Variable) treat.createVar(ite.getName()+"_cond").lastElement(); 01629 sv.setType("cond"); 01630 // Assign the right element to the condition variable 01631 if (cond.lastElement() instanceof Variable) { 01632 Assignment aff = new Assignment(); 01633 aff.addInput(cond.lastElement()); 01634 aff.addOutput(sv); 01635 cond = new Vector<Element>(); 01636 cond.add(aff); 01637 } else { 01638 ((Operation)cond.lastElement()).addOutput(sv); 01639 } 01640 // Add the condition variable if there isn't on temp or output 01641 if (!ite.getInternalVars().contains(sv)) { 01642 ite.addInternalVar(sv); 01643 } 01644 if (!ite.getOutput().contains(sv)) { 01645 ite.addOutput(sv); 01646 } 01647 01648 ite.setCond(cond); 01649 ite.setBodyTrue(b); 01650 01651 elementList.addAll(b); 01652 iterElement = elementList.iterator(); 01653 while (iterElement.hasNext()) 01654 { 01655 element = iterElement.next(); 01656 // Add all the input/output/internal variables of elements inside the body 01657 // to the if/then/else element 01658 if (element instanceof Operation) 01659 { 01660 /* Add internal and output variables to the "if" */ 01661 if(!((Operation)element).getOutput().isEmpty()) 01662 { 01663 var = ((Operation)element).getOutputAt(0); 01664 if(!ite.getInternalVars().contains(var)) 01665 ite.addInternalVar(var); 01666 if(!ite.getOutput().contains(var)) 01667 ite.addOutput(var); 01668 list1.put(treat.getTrueName(var.getName()), var); 01669 } 01670 01671 /* Add input variables to the "if" */ 01672 var = ((Operation)element).getInputAt(0); 01673 if(var instanceof Operation) 01674 elementList.add(var); 01675 else 01676 if(!ite.getInput().contains(var) && !((Variable)var).getType().equals("const")) 01677 ite.addInput(var); 01678 01679 if(((Operation)element).getInput().size() > 1) 01680 { 01681 var = ((Operation)element).getInputAt(1); 01682 if(var instanceof Operation) 01683 elementList.add(var); 01684 else 01685 if(!ite.getInput().contains(var) && !((Variable)var).getType().equals("const")) 01686 ite.addInput(var); 01687 } 01688 } 01689 else if (element instanceof GenericOperation) 01690 { 01691 var = ((GenericOperation)element).getOutputAt(0); 01692 ite.addInternalVar(var); 01693 list1.put(treat.getTrueName(var.getName()), var); 01694 } 01695 else if (element instanceof Function) 01696 { 01697 Element e1; 01698 for(int i = ((Function)element).getInput().size()-1; i >= 0; i --) 01699 { 01700 e1 = ((Function)element).getInput().elementAt(i); 01701 if(!ite.getInput().contains(e1) && !((Variable)e1).getType().equals("const")) 01702 ite.addInput(e1); 01703 } 01704 01705 for(int i = ((Function)element).getOutput().size()-1; i >= 0; i --) 01706 { 01707 e1 = ((Function)element).getOutput().elementAt(i); 01708 if(!ite.getInternalVars().contains(e1)) 01709 ite.addInternalVar(e1); 01710 if(!ite.getOutput().contains(e1)) 01711 ite.addOutput(e1); 01712 list1.put(treat.getTrueName(e1.getName()), e1); 01713 } 01714 } 01715 01716 elementList.remove(elementList.indexOf(element)); 01717 iterElement = elementList.iterator(); 01718 } 01719 01720 for(Element e: ite.getOutput()) 01721 ite.getInput().remove(e); 01722 01723 01724 { 01725 switch ( LA(1)) { 01726 case ELSEIF: 01727 { 01728 { 01729 match(ELSEIF); 01730 01731 for (String s : varCpt.keySet()) { 01732 ((ReadWriteIndex)this.varCpt.get(s)).addReadIndex(); 01733 } 01734 01735 { 01736 tmp=ifthenelsegen(); 01737 } 01738 01739 for (String s : varCpt.keySet()) { 01740 ((ReadWriteIndex)this.varCpt.get(s)).removeReadIndex(); 01741 } 01742 ite.setBodyFalse(tmp); 01743 01744 elementList.addAll(tmp); 01745 iterElement = elementList.iterator(); 01746 // Add input, output and internal variables to the structure 01747 while (iterElement.hasNext()) 01748 { 01749 element = iterElement.next(); 01750 01751 // Add all the input/output/internal variables of elements inside the body 01752 // to the if/then/else element 01753 if (element instanceof Operation) 01754 { 01755 /* Add internal and output variables to the "if" */ 01756 if(!((Operation)element).getOutput().isEmpty()) 01757 { 01758 var = ((Operation)element).getOutputAt(0); 01759 if(!ite.getInternalVars().contains(var)) 01760 ite.addInternalVar(var); 01761 if(!ite.getOutput().contains(var)) 01762 ite.addOutput(var); 01763 } 01764 01765 /* Add input variables to the "if" */ 01766 var = ((Operation)element).getInputAt(0); 01767 if(var instanceof Operation) 01768 elementList.add(var); 01769 else 01770 if(!ite.getInput().contains(var) && !((Variable)var).getType().equals("const")) 01771 ite.addInput(var); 01772 01773 if(((Operation)element).getInput().size() > 1) 01774 { 01775 var = ((Operation)element).getInputAt(1); 01776 if(var instanceof Operation) 01777 elementList.add(var); 01778 else 01779 if(!ite.getInput().contains(var) && !((Variable)var).getType().equals("const")) 01780 ite.addInput(var); 01781 } 01782 } 01783 else if (element instanceof GenericOperation) 01784 { 01785 var = ((GenericOperation)element).getOutputAt(0); 01786 ite.addInternalVar(var); 01787 } 01788 else if (element instanceof Function) 01789 { 01790 Element e1; 01791 for(int i = ((Function)element).getInput().size()-1; i >= 0; i --) 01792 { 01793 e1 = ((Function)element).getInput().elementAt(i); 01794 if(!ite.getInput().contains(e1) && !((Variable)e1).getType().equals("const")) 01795 ite.addInput(e1); 01796 } 01797 01798 for(int i = ((Function)element).getOutput().size()-1; i >= 0; i --) 01799 { 01800 e1 = ((Function)element).getOutput().elementAt(i); 01801 if(!ite.getInternalVars().contains(e1)) 01802 ite.addInternalVar(e1); 01803 if(!ite.getOutput().contains(e1)) 01804 ite.addOutput(e1); 01805 } 01806 } 01807 01808 elementList.remove(elementList.indexOf(element)); 01809 iterElement = elementList.iterator(); 01810 } 01811 01812 for(Element e: ite.getOutput()) 01813 ite.getInput().remove(e); 01814 01815 next = (IfThenElse)tmp.firstElement(); 01816 for (Element e : next.getInternalVars()) { 01817 list2.put(treat.getTrueName(e.getName()), e); 01818 } 01819 for (Element e : tmp) { 01820 if (e instanceof Operation) { 01821 list2.put(treat.getTrueName(((Operation)e).getOutputAt(0).getName()), ((Operation)e).getOutputAt(0)); 01822 } 01823 } 01824 // Create multiplexers for all the variables used into the if 01825 for (String s : list1.keySet()) { 01826 Multiplexer mux = new Multiplexer(); 01827 mux.setName("mux_"+ite.getName()); 01828 treat.count(mux); 01829 mux.setSize(2); 01830 mux.setSel((Variable)((Operation)ite.getCond().lastElement()).getOutputAt(0)); 01831 // If the variable is used in both branches of the if, we have 01832 // to create a multiplexer with the two input 01833 if (list2.containsKey(s)) { 01834 mux.addInput(list2.get(s)); 01835 mux.addInput(list1.get(s)); 01836 this.varCpt.put(s, ((ReadWriteIndex)this.varCpt.get(s)).getNextVariable()); 01837 var = treat.createVar(s+"_m2m_"+((ReadWriteIndex)this.varCpt.get(s)).getReadIndex(), 01838 0.0, "", false).lastElement(); 01839 mux.addOutput(var); 01840 vec.add(mux); 01841 // If the variable is used only in the first branche of the if, 01842 // we need to multiplex it with the variable used before the if 01843 } else if (varCpt.containsKey(s)) { 01844 var = treat.createVar(s+"_m2m_"+((ReadWriteIndex)this.varCpt.get(s)).getReadIndex(), 0.0, "", false).lastElement(); 01845 mux.addInput(var); 01846 mux.addInput(list1.get(s)); 01847 this.varCpt.put(s, ((ReadWriteIndex)this.varCpt.get(s)).getNextVariable()); 01848 var = treat.createVar(s+"_m2m_"+((ReadWriteIndex)this.varCpt.get(s)).getReadIndex(), 01849 0.0, "", false).lastElement(); 01850 mux.addOutput(var); 01851 vec.add(mux); 01852 } 01853 } 01854 // If the variable is used only in the second branche of the if, 01855 // we need to multiplex it with the variable used before the if 01856 for (String s : list2.keySet()) { 01857 if (!list1.containsKey(s) & varCpt.containsKey(s)) { 01858 Multiplexer mux = new Multiplexer(); 01859 mux.setName("mux_"+ite.getName()); 01860 treat.count(mux); 01861 mux.setSize(2); 01862 mux.setSel((Variable)((Operation)ite.getCond().lastElement()).getOutputAt(0)); 01863 var = treat.createVar(s+"_m2m_"+((ReadWriteIndex)this.varCpt.get(s)).getReadIndex(), 0.0, "", false).lastElement(); 01864 mux.addInput(list2.get(s)); 01865 mux.addInput(var); 01866 this.varCpt.put(s, ((ReadWriteIndex)this.varCpt.get(s)).getNextVariable()); 01867 var = treat.createVar(s+"_m2m_"+((ReadWriteIndex)this.varCpt.get(s)).getReadIndex(), 0.0, "", false).lastElement(); 01868 mux.addOutput(var); 01869 vec.add(mux); 01870 } 01871 } 01872 01873 } 01874 break; 01875 } 01876 case ELSE: 01877 { 01878 { 01879 match(ELSE); 01880 b=body(); 01881 01882 ite.setBodyFalse(b); 01883 01884 elementList.addAll(b); 01885 iterElement = elementList.iterator(); 01886 while (iterElement.hasNext()) 01887 { 01888 element = iterElement.next(); 01889 01890 // Add all the input/output/internal variables of elements inside the body 01891 // to the if/then/else element 01892 if (element instanceof Operation) 01893 { 01894 /* Add internal and output variables to the "if" */ 01895 if(!((Operation)element).getOutput().isEmpty()) 01896 { 01897 var = ((Operation)element).getOutputAt(0); 01898 if(!ite.getInternalVars().contains(var)) 01899 ite.addInternalVar(var); 01900 if(!ite.getOutput().contains(var)) 01901 ite.addOutput(var); 01902 list2.put(treat.getTrueName(var.getName()), var); 01903 } 01904 01905 /* Add input variables to the "if" */ 01906 var = ((Operation)element).getInputAt(0); 01907 if(var instanceof Operation) 01908 elementList.add(var); 01909 else 01910 if(!ite.getInput().contains(var) && !((Variable)var).getType().equals("const")) 01911 ite.addInput(var); 01912 01913 01914 if(((Operation)element).getInput().size() > 1) 01915 { 01916 var = ((Operation)element).getInputAt(1); 01917 if(var instanceof Operation) 01918 elementList.add(var); 01919 else 01920 if(!ite.getInput().contains(var) && !((Variable)var).getType().equals("const")) 01921 ite.addInput(var); 01922 } 01923 } 01924 else if (element instanceof GenericOperation) 01925 { 01926 var = ((GenericOperation)element).getOutputAt(0); 01927 ite.addInternalVar(var); 01928 list2.put(treat.getTrueName(var.getName()), var); 01929 } 01930 else if (element instanceof Function) 01931 { 01932 Element e1; 01933 for(int i = ((Function)element).getInput().size()-1; i >= 0; i --) 01934 { 01935 e1 = ((Function)element).getInput().elementAt(i); 01936 if(!ite.getInput().contains(e1) && !((Variable)e1).getType().equals("const")) 01937 ite.addInput(e1); 01938 } 01939 01940 for(int i = ((Function)element).getOutput().size()-1; i >= 0; i --) 01941 { 01942 e1 = ((Function)element).getOutput().elementAt(i); 01943 if(!ite.getInternalVars().contains(e1)) 01944 ite.addInternalVar(e1); 01945 if(!ite.getOutput().contains(e1)) 01946 ite.addOutput(e1); 01947 list2.put(treat.getTrueName(e1.getName()), e1); 01948 } 01949 } 01950 01951 elementList.remove(elementList.indexOf(element)); 01952 iterElement = elementList.iterator(); 01953 } 01954 01955 for(Element e: ite.getOutput()) 01956 ite.getInput().remove(e); 01957 01958 01959 // Create multiplexers for all the variables used into the if 01960 for (String s : list1.keySet()) { 01961 Multiplexer mux = new Multiplexer(); 01962 mux.setName("mux_"+ite.getName()); 01963 treat.count(mux); 01964 mux.setSize(2); 01965 mux.setSel((Variable)((Operation)ite.getCond().lastElement()).getOutputAt(0)); 01966 // If the variable is used in both branches of the if, we have 01967 // to create a multiplexer with the two input 01968 if (list2.containsKey(s)) { 01969 mux.addInput(list2.get(s)); 01970 mux.addInput(list1.get(s)); 01971 this.varCpt.put(s, ((ReadWriteIndex)this.varCpt.get(s)).getNextVariable()); 01972 var = treat.createVar(s+"_m2m_"+((ReadWriteIndex)this.varCpt.get(s)).getReadIndex(), 01973 0.0, "", false).lastElement(); 01974 mux.addOutput(var); 01975 vec.add(mux); 01976 // If the variable is used only in the first branche of the if, 01977 // we need to multiplex it with the variable used before the if 01978 } else if (varCpt.containsKey(s)) { 01979 var = treat.createVar(s+"_m2m_"+((ReadWriteIndex)this.varCpt.get(s)).getReadIndex(), 0.0, "", false).lastElement(); 01980 mux.addInput(var); 01981 mux.addInput(list1.get(s)); 01982 this.varCpt.put(s, ((ReadWriteIndex)this.varCpt.get(s)).getNextVariable()); 01983 var = treat.createVar(s+"_m2m_"+((ReadWriteIndex)this.varCpt.get(s)).getReadIndex(), 01984 0.0, "", false).lastElement(); 01985 mux.addOutput(var); 01986 vec.add(mux); 01987 } 01988 } 01989 // If the variable is used only in the second branche of the if, 01990 // we need to multiplex it with the variable used before the if 01991 for (String s : list2.keySet()) { 01992 if (!list1.containsKey(s) & varCpt.containsKey(s)) { 01993 Multiplexer mux = new Multiplexer(); 01994 mux.setName("mux_"+ite.getName()); 01995 treat.count(mux); 01996 mux.setSize(2); 01997 mux.setSel((Variable)((Operation)ite.getCond().lastElement()).getOutputAt(0)); 01998 var = treat.createVar(s+"_m2m_"+((ReadWriteIndex)this.varCpt.get(s)).getReadIndex(), 0.0, "", false).lastElement(); 01999 mux.addInput(list2.get(s)); 02000 mux.addInput(var); 02001 this.varCpt.put(s, ((ReadWriteIndex)this.varCpt.get(s)).getNextVariable()); 02002 var = treat.createVar(s+"_m2m_"+((ReadWriteIndex)this.varCpt.get(s)).getReadIndex(), 02003 0.0, "", false).lastElement(); 02004 mux.addOutput(var); 02005 vec.add(mux); 02006 } 02007 } 02008 02009 } 02010 break; 02011 } 02012 case END: 02013 case ENDIF: 02014 { 02015 02016 for (String s : list1.keySet()) { 02017 Multiplexer mux = new Multiplexer(); 02018 mux.setName("mux_"+ite.getName()); 02019 treat.count(mux); 02020 mux.setSize(2); 02021 mux.setSel((Variable)((Operation)ite.getCond().lastElement()).getOutputAt(0)); 02022 02023 // If the variable is used before the if, we have to multiplex it with the new value 02024 if (varCpt.containsKey(s)) { 02025 var = treat.createVar(s+"_m2m_"+((ReadWriteIndex)this.varCpt.get(s)).getReadIndex(), 0.0, "", false).lastElement(); 02026 mux.addInput(var); 02027 mux.addInput(list1.get(s)); 02028 this.varCpt.put(s, ((ReadWriteIndex)this.varCpt.get(s)).getNextVariable()); 02029 var = treat.createVar(s+"_m2m_"+((ReadWriteIndex)this.varCpt.get(s)).getReadIndex(), 02030 0.0, "", false).lastElement(); 02031 mux.addOutput(var); 02032 vec.add(mux); 02033 } 02034 } 02035 02036 break; 02037 } 02038 default: 02039 { 02040 throw new NoViableAltException(LT(1), getFilename()); 02041 } 02042 } 02043 } 02044 } 02045 catch (RecognitionException ex) { 02046 reportError(ex); 02047 recover(ex,_tokenSet_19); 02048 } 02049 return vec; 02050 } 02051 02057 public final Vector<Element> logexpr() throws RecognitionException, TokenStreamException, ParsingException { 02058 Vector<Element> vec = null; 02059 02060 02061 Vector<Element> tmp = null; 02062 Operation op = null; 02063 02064 02065 try { // for error handling 02066 vec=logterm(); 02067 { 02068 switch ( LA(1)) { 02069 case COR: 02070 case CAND: 02071 { 02072 op=log_op(); 02073 tmp=logexpr(); 02074 02075 try { 02076 vec = treat.createOp(vec,tmp,op); 02077 } 02078 catch (Exception e){ 02079 throw new ParsingException("Incomplete operation \"" + op.getOpSymbol() + "\""); 02080 } 02081 02082 break; 02083 } 02084 case RPAR: 02085 case FUNCTION: 02086 case END: 02087 case IF: 02088 case STRING: 02089 case FOR: 02090 case ENDWHILE: 02091 case WHILE: 02092 case PRINTF: 02093 case ERROR: 02094 case RETURN: 02095 case SWITCH: 02096 { 02097 break; 02098 } 02099 default: 02100 { 02101 throw new NoViableAltException(LT(1), getFilename()); 02102 } 02103 } 02104 } 02105 } 02106 catch (RecognitionException ex) { 02107 reportError(ex); 02108 recover(ex,_tokenSet_20); 02109 } 02110 return vec; 02111 } 02112 02117 public final LoopFor loop_for() throws RecognitionException, TokenStreamException, ParsingException { 02118 LoopFor loop = null; 02119 02120 02121 HashMap<String, ReadWriteIndex> tmpCpt = new HashMap<String, ReadWriteIndex>(); 02122 Vector<Element> vec = null; 02123 02124 /* Use to go through the body */ 02125 Element element; 02126 Vector<Element> elementList = new Vector<Element>(); 02127 Iterator<Element> iterElement; 02128 Element tempElement; 02129 02130 02131 try { // for error handling 02132 loop=loop_for_begin(); 02133 02134 if(loop == null) 02135 throw new ParsingException("Invalid \"loop\" condition"); 02136 02137 // Save the values of all index 02138 for (String var : varCpt.keySet()) 02139 tmpCpt.put(var, new ReadWriteIndex(varCpt.get(var))); 02140 // Increment all index 02141 for (String var : varCpt.keySet()) { 02142 if (!var.equals("const")) { 02143 varCpt.put(var, ((ReadWriteIndex)this.varCpt.get(var)).getNextVariable()); 02144 treat.createVar(var+"_m2m_"+((ReadWriteIndex)this.varCpt.get(var)).getReadIndex(), 0.0, "", false); 02145 } 02146 } 02147 02148 vec=body_function(); 02149 { 02150 switch ( LA(1)) { 02151 case END: 02152 { 02153 match(END); 02154 break; 02155 } 02156 case ENDFOR: 02157 { 02158 match(ENDFOR); 02159 break; 02160 } 02161 default: 02162 { 02163 throw new NoViableAltException(LT(1), getFilename()); 02164 } 02165 } 02166 } 02167 02168 loop.setBody(vec); 02169 this.treat.treeToVector(loop); 02170 this.treat.updateConditionsFor(tmpCpt, varCpt, loop); 02171 02172 // Check if the variables as been modified during the loop. If not, replace the old variables. 02173 for (String var : tmpCpt.keySet()) { 02174 if (var.equals("const")) { 02175 // If there is no change on the variable 02176 } else if (((ReadWriteIndex)tmpCpt.get(var)).getReadIndex() == ((ReadWriteIndex)this.varCpt.get(var)).getReadIndex()-1) { 02177 this.treat.changeVar(loop, (Variable)treat.createVar(var+"_m2m_"+((ReadWriteIndex)this.varCpt.get(var)).getReadIndex()).lastElement(), (Variable)treat.createVar(var+"_m2m_"+(((ReadWriteIndex)this.varCpt.get(var)).getReadIndex()-1)).lastElement()); 02178 varCpt.put(var, ((ReadWriteIndex)this.varCpt.get(var)).getPreviousVariable()); 02179 02180 // If there is change on the variable, we have to create an "if" 02181 // instruction for choose which one we want to 02182 // use between the old one and the new recalculated inside 02183 // the loop. For example, in the case of a = a + 1, 02184 // in first iteration we have to use the old "a" and after 02185 // we have to use the new value. 02186 02187 } else if (((ReadWriteIndex)tmpCpt.get(var)).getReadIndex()+1 < ((ReadWriteIndex)this.varCpt.get(var)).getReadIndex()) { 02188 IfThenElse ite = new IfThenElse(); 02189 ite.setName("ifThenElse_"+loop.getName()+"_aff"); 02190 treat.count(ite); 02191 Operation cond = new LessEqual(); 02192 cond.addOutput(treat.createVar(ite.getName()+"_cond").lastElement()); 02193 cond.addInput(loop.getInternalVars().firstElement()); 02194 cond.addInput(loop.getEnd()); 02195 Vector<Element> tmp = new Vector<Element>(); 02196 tmp.add(cond); 02197 ite.setCond(tmp); 02198 for(Element e : cond.getInput()) { 02199 if(!((SimpleVariable)e).getType().equalsIgnoreCase("const")) { 02200 ite.addInput(e); 02201 } 02202 } 02203 if(!((SimpleVariable)cond.getOutputAt(0)).getType().equalsIgnoreCase("const")) { 02204 ite.addInternalVar(cond.getOutputAt(0)); 02205 ite.addOutput(cond.getOutputAt(0)); 02206 } 02207 Assignment aff = new Assignment(); 02208 treat.count(aff); 02209 aff.addOutput(treat.createVar(var+"_m2m_"+(((ReadWriteIndex)tmpCpt.get(var)).getWriteIndex())).lastElement()); 02210 aff.addInput(treat.createVar(var+"_m2m_"+((ReadWriteIndex)tmpCpt.get(var)).getReadIndex()).lastElement()); 02211 if(!((Variable) aff.getInput().elementAt(0)).getType().equals("iter")) 02212 loop.addInput(aff.getInput().elementAt(0)); 02213 tmp = new Vector<Element>(); 02214 tmp.add(aff); 02215 ite.setBodyTrue(tmp); 02216 if(!ite.getInternalVars().contains(aff.getOutputAt(0))) 02217 ite.addInternalVar(aff.getOutputAt(0)); 02218 if(!ite.getOutput().contains(aff.getOutputAt(0))) 02219 ite.addOutput(aff.getOutputAt(0)); 02220 aff = new Assignment(); 02221 treat.count(aff); 02222 aff.addOutput(treat.createVar(var+"_m2m_"+(((ReadWriteIndex)tmpCpt.get(var)).getWriteIndex())).lastElement()); 02223 aff.addInput(treat.createVar(var+"_m2m_"+((ReadWriteIndex)varCpt.get(var)).getReadIndex()).lastElement()); 02224 tmp = new Vector<Element>(); 02225 tmp.add(aff); 02226 ite.setBodyFalse(tmp); 02227 if(!ite.getInternalVars().contains(aff.getOutputAt(0))) 02228 ite.addInternalVar(aff.getOutputAt(0)); 02229 if(!ite.getOutput().contains(aff.getOutputAt(0))) 02230 ite.addOutput(aff.getOutputAt(0)); 02231 loop.getBody().add(0,ite); 02232 loop.addOutput(treat.createVar(var+"_m2m_"+((ReadWriteIndex)this.varCpt.get(var)).getReadIndex()).lastElement()); 02233 } 02234 } 02235 this.treat.findInBody(loop, tmpCpt); 02236 02237 elementList.addAll(loop.getBody()); 02238 iterElement = elementList.iterator(); 02239 // Add all the input, output and internal variables to the loop 02240 while (iterElement.hasNext()) 02241 { 02242 element = iterElement.next(); 02243 02244 if (element instanceof Operation) 02245 { 02246 tempElement = ((Operation)element).getInputAt(0); 02247 if(tempElement instanceof Operation) 02248 elementList.add(tempElement); 02249 else 02250 if(!loop.getInput().contains(tempElement) && !((Variable)tempElement).getType().equals("const") && !((Variable)tempElement).getType().equals("iter")) 02251 loop.addInput(tempElement); 02252 02253 if(((Operation)element).getInput().size() > 1) 02254 { 02255 tempElement = ((Operation)element).getInputAt(1); 02256 if(tempElement instanceof Operation) 02257 elementList.add(tempElement); 02258 else 02259 if(!loop.getInput().contains(tempElement) && !((Variable)tempElement).getType().equals("const") && !((Variable)tempElement).getType().equals("iter")) 02260 loop.addInput(tempElement); 02261 } 02262 02263 if(!((Operation)element).getOutput().isEmpty()) 02264 { 02265 tempElement = ((Operation)element).getOutputAt(0); 02266 if(!loop.getInternalVars().contains(tempElement)) 02267 loop.addInternalVar(tempElement); 02268 if(!loop.getOutput().contains(tempElement)) 02269 loop.addOutput(tempElement); 02270 } 02271 } 02272 else if (element instanceof GenericOperation) 02273 { 02274 tempElement = ((GenericOperation)element).getOutputAt(0); 02275 loop.addInternalVar(tempElement); 02276 } 02277 else if (element instanceof Function) 02278 { 02279 Element e1; 02280 for(int i = ((Function)element).getInput().size()-1; i >= 0; i --) 02281 { 02282 e1 = ((Function)element).getInput().elementAt(i); 02283 if(!loop.getInput().contains(e1) && !((Variable)e1).getType().equals("const") && !((Variable)e1).getType().equals("iter")) 02284 loop.addInput(e1); 02285 } 02286 02287 for(int i = ((Function)element).getOutput().size()-1; i >= 0; i --) 02288 { 02289 e1 = ((Function)element).getOutput().elementAt(i); 02290 if(!loop.getInternalVars().contains(e1)) 02291 loop.addInternalVar(e1); 02292 if(!loop.getOutput().contains(e1)) 02293 loop.addOutput(e1); 02294 } 02295 } 02296 02297 elementList.remove(elementList.indexOf(element)); 02298 iterElement = elementList.iterator(); 02299 } 02300 02301 elementList.addAll(loop.getOutput()); 02302 for(Element e: elementList) 02303 { 02304 if(loop.getInput().contains(e)) 02305 loop.getInput().remove(e); 02306 if(((Variable)e).getType().equals("cond") || ((Variable)e).getType().equals("const") ) 02307 loop.getOutput().remove(e); 02308 } 02309 02310 } 02311 catch (RecognitionException ex) { 02312 reportError(ex); 02313 recover(ex,_tokenSet_3); 02314 } 02315 return loop; 02316 } 02317 02322 public final LoopWhile loop_while() throws RecognitionException, TokenStreamException, ParsingException { 02323 LoopWhile loop = null; 02324 02325 02326 HashMap<String, ReadWriteIndex> tmpCpt = new HashMap<String, ReadWriteIndex>(); 02327 Vector<Element> vec = null; 02328 02329 /* Use to go through the body */ 02330 Element element; 02331 Vector<Element> elementList = new Vector<Element>(); 02332 Iterator<Element> iterElement; 02333 Element tempElement; 02334 02335 02336 try { // for error handling 02337 loop=loop_while_begin(); 02338 02339 for (String var : varCpt.keySet()) 02340 tmpCpt.put(var, new ReadWriteIndex(varCpt.get(var))); 02341 02342 // Increment all index 02343 for (String var : varCpt.keySet()) { 02344 if (!var.equals("const")) { 02345 varCpt.put(var, ((ReadWriteIndex)this.varCpt.get(var)).getNextVariable()); 02346 treat.createVar(var+"_m2m_"+((ReadWriteIndex)this.varCpt.get(var)).getReadIndex(), 0.0, "", false); 02347 } 02348 } 02349 02350 vec=body_function(); 02351 { 02352 switch ( LA(1)) { 02353 case END: 02354 { 02355 match(END); 02356 break; 02357 } 02358 case ENDWHILE: 02359 { 02360 match(ENDWHILE); 02361 break; 02362 } 02363 default: 02364 { 02365 throw new NoViableAltException(LT(1), getFilename()); 02366 } 02367 } 02368 } 02369 02370 loop.setBody(vec); 02371 treat.count(loop); 02372 this.treat.treeToVector(loop); 02373 this.treat.updateConditionsWhile(tmpCpt, varCpt, loop); 02374 02375 // Check if the variables as been modified during the loop. 02376 // If not, replace the old variables. 02377 02378 for (String var : tmpCpt.keySet()) { 02379 if (var.equals("const")) { 02380 // If there is no change on the variable 02381 } else if (((ReadWriteIndex)tmpCpt.get(var)).getReadIndex() == ((ReadWriteIndex)this.varCpt.get(var)).getReadIndex()-1) { 02382 this.treat.changeVar(loop, (Variable)treat.createVar(var+"_m2m_"+((ReadWriteIndex)this.varCpt.get(var)).getReadIndex()).lastElement(), (Variable)treat.createVar(var+"_m2m_"+(((ReadWriteIndex)this.varCpt.get(var)).getReadIndex()-1)).lastElement()); 02383 varCpt.put(var, ((ReadWriteIndex)this.varCpt.get(var)).getPreviousVariable()); 02384 02385 // If there is change on the variable, we have to create an "if" 02386 // instruction for choose which one we want to 02387 // use between the old one and the new recalculated inside the 02388 // loop. For example, in the case of a = a + 1, 02389 // in first iteration we have to use the old "a" and after 02390 // we have to use the new value. 02391 02392 } else if (((ReadWriteIndex)tmpCpt.get(var)).getReadIndex()+1 < ((ReadWriteIndex)this.varCpt.get(var)).getReadIndex()) { 02393 IfThenElse ite = new IfThenElse(); 02394 ite.setName("ifThenElse_"+loop.getName()+"_aff"); 02395 treat.count(ite); 02396 Vector<Element> tmp = new Vector<Element>(); 02397 Variable sv = (Variable) treat.createVar(ite.getName()+"_cond").lastElement(); 02398 sv.setType("cond"); 02399 if (loop.getCond().lastElement() instanceof Variable) { 02400 Assignment aff = new Assignment(); 02401 aff.addInput(loop.getCond().lastElement()); 02402 aff.addOutput(sv); 02403 loop.setCond(new Vector<Element>()); 02404 loop.getCond().add(aff); 02405 } else { 02406 ((Operation)loop.getCond().lastElement()).addOutput(sv); 02407 } 02408 ite.setCond(loop.getCond()); 02409 Assignment aff = new Assignment(); 02410 treat.count(aff); 02411 aff.addOutput(treat.createVar(var+"_m2m_"+(((ReadWriteIndex)tmpCpt.get(var)).getWriteIndex())).lastElement()); 02412 aff.addInput(treat.createVar(var+"_m2m_"+((ReadWriteIndex)tmpCpt.get(var)).getReadIndex()).lastElement()); 02413 if(!((Variable) aff.getInput().elementAt(0)).getType().equals("iter")) 02414 loop.addInput(aff.getInput().elementAt(0)); 02415 tmp = new Vector<Element>(); 02416 tmp.add(aff); 02417 ite.setBodyTrue(tmp); 02418 if(!ite.getInternalVars().contains(aff.getOutputAt(0))) 02419 ite.addInternalVar(aff.getOutputAt(0)); 02420 if(!ite.getOutput().contains(aff.getOutputAt(0))) 02421 ite.addOutput(aff.getOutputAt(0)); 02422 aff = new Assignment(); 02423 treat.count(aff); 02424 aff.addOutput(treat.createVar(var+"_m2m_"+(((ReadWriteIndex)tmpCpt.get(var)).getWriteIndex())).lastElement()); 02425 aff.addInput(treat.createVar(var+"_m2m_"+((ReadWriteIndex)varCpt.get(var)).getReadIndex()).lastElement()); 02426 tmp = new Vector<Element>(); 02427 tmp.add(aff); 02428 ite.setBodyFalse(tmp); 02429 if(!ite.getInternalVars().contains(aff.getOutputAt(0))) 02430 ite.addInternalVar(aff.getOutputAt(0)); 02431 if(!ite.getOutput().contains(aff.getOutputAt(0))) 02432 ite.addOutput(aff.getOutputAt(0)); 02433 loop.getBody().add(0,ite); 02434 loop.addOutput(treat.createVar(var+"_m2m_"+((ReadWriteIndex)this.varCpt.get(var)).getReadIndex()).lastElement()); 02435 } 02436 } 02437 this.treat.findInBody(loop, tmpCpt); 02438 02439 elementList.addAll(loop.getBody()); 02440 iterElement = elementList.iterator(); 02441 // Add all the input, output and internal variables to the loop 02442 while (iterElement.hasNext()) 02443 { 02444 element = iterElement.next(); 02445 02446 if (element instanceof Operation) 02447 { 02448 tempElement = ((Operation)element).getInputAt(0); 02449 if(tempElement instanceof Operation) 02450 elementList.add(tempElement); 02451 else 02452 if(!loop.getInput().contains(tempElement) && !((Variable)tempElement).getType().equals("const") && !((Variable)tempElement).getType().equals("iter")) 02453 loop.addInput(tempElement); 02454 02455 if(((Operation)element).getInput().size() > 1) 02456 { 02457 tempElement = ((Operation)element).getInputAt(1); 02458 if(tempElement instanceof Operation) 02459 elementList.add(tempElement); 02460 else 02461 if(!loop.getInput().contains(tempElement) && !((Variable)tempElement).getType().equals("const") && !((Variable)tempElement).getType().equals("iter")) 02462 loop.addInput(tempElement); 02463 } 02464 02465 if(!((Operation)element).getOutput().isEmpty()) 02466 { 02467 tempElement = ((Operation)element).getOutputAt(0); 02468 if(!loop.getInternalVars().contains(tempElement)) 02469 loop.addInternalVar(tempElement); 02470 if(!loop.getOutput().contains(tempElement)) 02471 loop.addOutput(tempElement); 02472 } 02473 } 02474 else if (element instanceof GenericOperation) 02475 { 02476 tempElement = ((GenericOperation)element).getOutputAt(0); 02477 loop.addInternalVar(tempElement); 02478 } 02479 else if (element instanceof Function) 02480 { 02481 Element e1; 02482 for(int i = ((Function)element).getInput().size()-1; i >= 0; i --) 02483 { 02484 e1 = ((Function)element).getInput().elementAt(i); 02485 if(!loop.getInput().contains(e1) && !((Variable)e1).getType().equals("const") && !((Variable)e1).getType().equals("iter")) 02486 loop.addInput(e1); 02487 } 02488 02489 for(int i = ((Function)element).getOutput().size()-1; i >= 0; i --) 02490 { 02491 e1 = ((Function)element).getOutput().elementAt(i); 02492 if(!loop.getInternalVars().contains(e1)) 02493 loop.addInternalVar(e1); 02494 if(!loop.getOutput().contains(e1)) 02495 loop.addOutput(e1); 02496 } 02497 } 02498 02499 elementList.remove(elementList.indexOf(element)); 02500 iterElement = elementList.iterator(); 02501 } 02502 02503 elementList.addAll(loop.getOutput()); 02504 for(Element e: elementList) 02505 { 02506 if(loop.getInput().contains(e)) 02507 loop.getInput().remove(e); 02508 if(((Variable)e).getType().equals("cond") || ((Variable)e).getType().equals("const") ) 02509 loop.getOutput().remove(e); 02510 } 02511 02512 } 02513 catch (RecognitionException ex) { 02514 reportError(ex); 02515 recover(ex,_tokenSet_3); 02516 } 02517 return loop; 02518 } 02519 02527 public final Switch op_switch() throws RecognitionException, TokenStreamException, ParsingException { 02528 Switch sw = null; 02529 02530 02531 Vector<Element> vec = null; 02532 Vector<Element> var = null; 02533 Vector<Variable> cond = null; 02534 Vector<Element> b = null; 02535 02536 02537 try { // for error handling 02538 match(SWITCH); 02539 sw = new Switch(); treat.count(sw); 02540 vec=func_var(); 02541 sw.setSwitchVar((Variable)vec.lastElement()); 02542 { 02543 _loop91: 02544 do { 02545 if ((LA(1)==CASE)) { 02546 match(CASE); 02547 { 02548 switch ( LA(1)) { 02549 case MINUS: 02550 case LBRAK: 02551 case STRING: 02552 case NUMBER: 02553 { 02554 var=func_var(); 02555 02556 cond = new Vector<Variable>(); 02557 cond.add((Variable)var.lastElement()); 02558 02559 break; 02560 } 02561 case LBRAC: 02562 { 02563 match(LBRAC); 02564 var=func_var(); 02565 02566 cond = new Vector<Variable>(); 02567 cond.add((Variable)var.lastElement()); 02568 02569 { 02570 _loop89: 02571 do { 02572 if ((LA(1)==COMMA)) { 02573 match(COMMA); 02574 var=func_var(); 02575 02576 cond.add((Variable)var.lastElement()); 02577 02578 } 02579 else { 02580 break _loop89; 02581 } 02582 02583 } while (true); 02584 } 02585 match(RBRAC); 02586 break; 02587 } 02588 default: 02589 { 02590 throw new NoViableAltException(LT(1), getFilename()); 02591 } 02592 } 02593 } 02594 { 02595 switch ( LA(1)) { 02596 case COMMA: 02597 { 02598 match(COMMA); 02599 break; 02600 } 02601 case FUNCTION: 02602 case END: 02603 case IF: 02604 case STRING: 02605 case FOR: 02606 case WHILE: 02607 case PRINTF: 02608 case ERROR: 02609 case RETURN: 02610 case SWITCH: 02611 case CASE: 02612 case OTHERW: 02613 { 02614 break; 02615 } 02616 default: 02617 { 02618 throw new NoViableAltException(LT(1), getFilename()); 02619 } 02620 } 02621 } 02622 b=body(); 02623 sw.addCase(cond,b); 02624 } 02625 else { 02626 break _loop91; 02627 } 02628 02629 } while (true); 02630 } 02631 { 02632 switch ( LA(1)) { 02633 case OTHERW: 02634 { 02635 match(OTHERW); 02636 { 02637 switch ( LA(1)) { 02638 case COMMA: 02639 { 02640 match(COMMA); 02641 break; 02642 } 02643 case FUNCTION: 02644 case END: 02645 case IF: 02646 case STRING: 02647 case FOR: 02648 case WHILE: 02649 case PRINTF: 02650 case ERROR: 02651 case RETURN: 02652 case SWITCH: 02653 { 02654 break; 02655 } 02656 default: 02657 { 02658 throw new NoViableAltException(LT(1), getFilename()); 02659 } 02660 } 02661 } 02662 b=body(); 02663 sw.addCase(null,b); 02664 break; 02665 } 02666 case END: 02667 { 02668 break; 02669 } 02670 default: 02671 { 02672 throw new NoViableAltException(LT(1), getFilename()); 02673 } 02674 } 02675 } 02676 match(END); 02677 } 02678 catch (RecognitionException ex) { 02679 reportError(ex); 02680 recover(ex,_tokenSet_3); 02681 } 02682 return sw; 02683 } 02684 02692 public final void not_instruction_cmd() throws RecognitionException, TokenStreamException { 02693 02694 02695 try { // for error handling 02696 switch ( LA(1)) { 02697 case PRINTF: 02698 case ERROR: 02699 { 02700 { 02701 switch ( LA(1)) { 02702 case PRINTF: 02703 { 02704 match(PRINTF); 02705 break; 02706 } 02707 case ERROR: 02708 { 02709 match(ERROR); 02710 break; 02711 } 02712 default: 02713 { 02714 throw new NoViableAltException(LT(1), getFilename()); 02715 } 02716 } 02717 } 02718 match(LPAR); 02719 match(ANY_STRING); 02720 match(RPAR); 02721 { 02722 switch ( LA(1)) { 02723 case SEMI: 02724 { 02725 match(SEMI); 02726 break; 02727 } 02728 case FUNCTION: 02729 case END: 02730 case ENDFUNCTION: 02731 case IF: 02732 case ENDIF: 02733 case ELSEIF: 02734 case ELSE: 02735 case STRING: 02736 case ENDFOR: 02737 case FOR: 02738 case ENDWHILE: 02739 case WHILE: 02740 case PRINTF: 02741 case ERROR: 02742 case RETURN: 02743 case SWITCH: 02744 case CASE: 02745 case OTHERW: 02746 { 02747 break; 02748 } 02749 default: 02750 { 02751 throw new NoViableAltException(LT(1), getFilename()); 02752 } 02753 } 02754 } 02755 break; 02756 } 02757 case RETURN: 02758 { 02759 match(RETURN); 02760 { 02761 switch ( LA(1)) { 02762 case SEMI: 02763 { 02764 match(SEMI); 02765 break; 02766 } 02767 case FUNCTION: 02768 case END: 02769 case ENDFUNCTION: 02770 case IF: 02771 case ENDIF: 02772 case ELSEIF: 02773 case ELSE: 02774 case STRING: 02775 case ENDFOR: 02776 case FOR: 02777 case ENDWHILE: 02778 case WHILE: 02779 case PRINTF: 02780 case ERROR: 02781 case RETURN: 02782 case SWITCH: 02783 case CASE: 02784 case OTHERW: 02785 { 02786 break; 02787 } 02788 default: 02789 { 02790 throw new NoViableAltException(LT(1), getFilename()); 02791 } 02792 } 02793 } 02794 break; 02795 } 02796 default: 02797 { 02798 throw new NoViableAltException(LT(1), getFilename()); 02799 } 02800 } 02801 } 02802 catch (RecognitionException ex) { 02803 reportError(ex); 02804 recover(ex,_tokenSet_3); 02805 } 02806 } 02807 02813 public final Vector<Element> logterm() throws RecognitionException, TokenStreamException, ParsingException { 02814 Vector<Element> vec = null; 02815 02816 02817 Vector<Element> tmp = null; 02818 Operation op = null; 02819 02820 02821 try { // for error handling 02822 vec=logfactor(); 02823 { 02824 switch ( LA(1)) { 02825 case CEQUAL: 02826 case CNOTEQ: 02827 case CLESS: 02828 case CGREAT: 02829 case CLESEQ: 02830 case CGREEQ: 02831 { 02832 op=cmp_op(); 02833 tmp=logterm(); 02834 02835 try { 02836 vec = treat.createOp(vec,tmp,op); 02837 } 02838 catch (Exception e){ 02839 throw new ParsingException("Incomplete operation \"" + op.getOpSymbol() + "\""); 02840 } 02841 02842 break; 02843 } 02844 case RPAR: 02845 case FUNCTION: 02846 case END: 02847 case IF: 02848 case STRING: 02849 case COR: 02850 case CAND: 02851 case FOR: 02852 case ENDWHILE: 02853 case WHILE: 02854 case PRINTF: 02855 case ERROR: 02856 case RETURN: 02857 case SWITCH: 02858 { 02859 break; 02860 } 02861 default: 02862 { 02863 throw new NoViableAltException(LT(1), getFilename()); 02864 } 02865 } 02866 } 02867 } 02868 catch (RecognitionException ex) { 02869 reportError(ex); 02870 recover(ex,_tokenSet_21); 02871 } 02872 return vec; 02873 } 02874 02879 public final Operation log_op() throws RecognitionException, TokenStreamException { 02880 Operation op = null; 02881 02882 02883 try { // for error handling 02884 switch ( LA(1)) { 02885 case COR: 02886 { 02887 match(COR); 02888 op = new Or(); 02889 break; 02890 } 02891 case CAND: 02892 { 02893 match(CAND); 02894 op = new And(); 02895 break; 02896 } 02897 default: 02898 { 02899 throw new NoViableAltException(LT(1), getFilename()); 02900 } 02901 } 02902 } 02903 catch (RecognitionException ex) { 02904 reportError(ex); 02905 recover(ex,_tokenSet_10); 02906 } 02907 return op; 02908 } 02909 02915 public final Vector<Element> logfactor() throws RecognitionException, TokenStreamException, ParsingException { 02916 Vector<Element> vec = null; 02917 02918 02919 Not not = null; 02920 02921 02922 try { // for error handling 02923 { 02924 switch ( LA(1)) { 02925 case NOT: 02926 { 02927 match(NOT); 02928 not = new Not(); 02929 break; 02930 } 02931 case MINUS: 02932 case LPAR: 02933 case LBRAK: 02934 case STRING: 02935 case NUMBER: 02936 { 02937 break; 02938 } 02939 default: 02940 { 02941 throw new NoViableAltException(LT(1), getFilename()); 02942 } 02943 } 02944 } 02945 { 02946 switch ( LA(1)) { 02947 case MINUS: 02948 case LBRAK: 02949 case STRING: 02950 case NUMBER: 02951 { 02952 vec=func_var(); 02953 break; 02954 } 02955 case LPAR: 02956 { 02957 match(LPAR); 02958 vec=logexpr(); 02959 match(RPAR); 02960 break; 02961 } 02962 default: 02963 { 02964 throw new NoViableAltException(LT(1), getFilename()); 02965 } 02966 } 02967 } 02968 02969 if (not != null) { 02970 vec = treat.createNot(vec, not); 02971 } 02972 02973 } 02974 catch (RecognitionException ex) { 02975 reportError(ex); 02976 recover(ex,_tokenSet_22); 02977 } 02978 return vec; 02979 } 02980 02986 public final LoopFor loop_for_begin() throws RecognitionException, TokenStreamException, ParsingException { 02987 LoopFor loop = null; 02988 02989 02990 String name = null; 02991 SimpleVariable iterOut = null; 02992 Vector<Element> vec = null; 02993 Vector<Element> tmp = null; 02994 Operation op = new Addition(); 02995 op.setName("AddIter"); 02996 String var; 02997 02998 02999 try { // for error handling 03000 match(FOR); 03001 name=ident(); 03002 match(EQUAL); 03003 vec=func_var(); 03004 match(COLON); 03005 03006 if(vec == null) 03007 throw new ParsingException("Invalid \"loop\" condition"); 03008 03009 loop = new LoopFor(); 03010 treat.count(loop); 03011 var = treat.getTrueName(vec.lastElement().getName()); 03012 if(!((SimpleVariable)vec.lastElement()).getType().equals("const") && ((ReadWriteIndex)varCpt.get(var)).getReadIndex()-1 >= 0) 03013 loop.setStart((SimpleVariable)treat.createVar(var +"_m2m_"+(((ReadWriteIndex)varCpt.get(var)).getReadIndex()-1)).lastElement()); 03014 else 03015 loop.setStart((SimpleVariable)this.treat.createVar((Variable)vec.lastElement()).lastElement()); 03016 // If the loop indice isn't already in the table 03017 if (varCpt.get(name) == null) { 03018 varCpt.put(name, new ReadWriteIndex()); 03019 } 03020 // Create the loop indice variable 03021 tmp = treat.createVar(name+"_m2m_"+((ReadWriteIndex)this.varCpt.get(name)).getReadIndex(), 0.0, "", false); 03022 loop.addInternalVar((Variable)tmp.lastElement()); 03023 ((Variable)tmp.lastElement()).setType("iter"); 03024 03025 vec=func_var(); 03026 03027 if(vec == null) 03028 throw new ParsingException("Invalid \"loop\" condition"); 03029 03030 // Set the variable to the end position for the case where 03031 // there isn't another loop indice next. 03032 // The increment value is automaticly set to 1 03033 if (vec.size() == 1 && vec.lastElement() instanceof Variable) 03034 { 03035 var = treat.getTrueName(vec.lastElement().getName()); 03036 if(!((SimpleVariable)vec.lastElement()).getType().equals("const") && ((ReadWriteIndex)varCpt.get(var)).getReadIndex()-1 >= 0) 03037 loop.setEnd((SimpleVariable)treat.createVar(var +"_m2m_"+(((ReadWriteIndex)varCpt.get(var)).getReadIndex()-1)).lastElement()); 03038 else 03039 loop.setEnd((SimpleVariable)this.treat.createVar((Variable)vec.lastElement()).lastElement()); 03040 } 03041 else 03042 { 03043 this.top.addBody(vec); 03044 var = treat.getTrueName(((Operation)vec.lastElement()).getOutput().lastElement().getName()); 03045 if(!((SimpleVariable)vec.lastElement()).getType().equals("const") && ((ReadWriteIndex)varCpt.get(var)).getReadIndex()-1 >= 0) 03046 loop.setEnd((SimpleVariable)treat.createVar(var +"_m2m_"+(((ReadWriteIndex)varCpt.get(var)).getReadIndex()-1)).lastElement()); 03047 else 03048 loop.setEnd((SimpleVariable)this.treat.createVar((Variable)vec.lastElement()).lastElement()); 03049 } 03050 03051 match(COLON); 03052 vec=func_var(); 03053 03054 if(vec == null) 03055 throw new ParsingException("Invalid \"loop\" condition"); 03056 03057 // If there is a third variable, move the end variable to the 03058 // increment variable and set the new end variable 03059 loop.setIncr(loop.getEnd()); 03060 loop.setEnd((SimpleVariable)vec.lastElement()); 03061 this.top.addBody(vec); 03062 var = treat.getTrueName(vec.lastElement().getName()); 03063 if(!((SimpleVariable)vec.lastElement()).getType().equals("const") && ((ReadWriteIndex)varCpt.get(var)).getReadIndex()-1 >= 0) 03064 loop.setEnd((SimpleVariable)treat.createVar(var +"_m2m_"+(((ReadWriteIndex)varCpt.get(var)).getReadIndex()-1)).lastElement()); 03065 else 03066 loop.setEnd((SimpleVariable)this.treat.createVar((Variable)vec.lastElement()).lastElement()); 03067 03068 /* Create the iterOperation */ 03069 iterOut = new SimpleVariable(loop.getInternalVars().firstElement().getName()+"_Add", 0.0, ""); 03070 loop.addInternalVar((Variable)iterOut); 03071 op.getInput().add(loop.getInternalVars().firstElement()); 03072 op.getInput().add(loop.getIncr()); 03073 op.getOutput().add(iterOut); 03074 loop.setIterOperation(op); 03075 03076 } 03077 catch (RecognitionException ex) { 03078 reportError(ex); 03079 recover(ex,_tokenSet_23); 03080 } 03081 return loop; 03082 } 03083 03088 public final LoopWhile loop_while_begin() throws RecognitionException, TokenStreamException, ParsingException { 03089 LoopWhile loop = null; 03090 03091 03092 Vector<Element> cond = null; 03093 03094 03095 try { // for error handling 03096 match(WHILE); 03097 cond=logexpr(); 03098 03099 if(cond == null) 03100 throw new ParsingException("Invalid \"while\" condition"); 03101 loop = new LoopWhile(); 03102 treat.count(loop); 03103 loop.setCond(cond); 03104 03105 } 03106 catch (RecognitionException ex) { 03107 reportError(ex); 03108 recover(ex,_tokenSet_24); 03109 } 03110 return loop; 03111 } 03112 03113 03114 public static final String[] _tokenNames = { 03115 "<0>", 03116 "EOF", 03117 "<2>", 03118 "NULL_TREE_LOOKAHEAD", 03119 "EQUAL", 03120 "SEMI", 03121 "CEQUAL", 03122 "CNOTEQ", 03123 "CLESS", 03124 "CGREAT", 03125 "CLESEQ", 03126 "CGREEQ", 03127 "PLUS", 03128 "DPLUS", 03129 "MINUS", 03130 "DMINUS", 03131 "MUL", 03132 "DMUL", 03133 "DIV", 03134 "DDIV", 03135 "PWR1", 03136 "PWR2", 03137 "LPAR", 03138 "RPAR", 03139 "COMMA", 03140 "LBRAK", 03141 "RBRAK", 03142 "\"function\"", 03143 "\"end\"", 03144 "\"endfunction\"", 03145 "\"if\"", 03146 "\"endif\"", 03147 "\"then\"", 03148 "\"elseif\"", 03149 "\"else\"", 03150 "STRING", 03151 "NOT", 03152 "COR", 03153 "CAND", 03154 "\"endfor\"", 03155 "\"for\"", 03156 "COLON", 03157 "\"endwhile\"", 03158 "\"while\"", 03159 "\"printf\"", 03160 "\"error\"", 03161 "ANY_STRING", 03162 "\"return\"", 03163 "NUMBER", 03164 "DOT", 03165 "\"switch\"", 03166 "\"case\"", 03167 "LBRAC", 03168 "RBRAC", 03169 "\"otherwise\"", 03170 "M2M", 03171 "\"break\"", 03172 "DIGIT", 03173 "CHAR", 03174 "WS", 03175 "COMMENT", 03176 "ML_COMMENT" 03177 }; 03178 03179 private static final long[] mk_tokenSet_0() { 03180 long[] data = { 2L, 0L}; 03181 return data; 03182 } 03183 public static final BitSet _tokenSet_0 = new BitSet(mk_tokenSet_0()); 03184 private static final long[] mk_tokenSet_1() { 03185 long[] data = { 36028797153181696L, 0L}; 03186 return data; 03187 } 03188 public static final BitSet _tokenSet_1 = new BitSet(mk_tokenSet_1()); 03189 private static final long[] mk_tokenSet_2() { 03190 long[] data = { 21600519973765122L, 0L}; 03191 return data; 03192 } 03193 public static final BitSet _tokenSet_2 = new BitSet(mk_tokenSet_2()); 03194 private static final long[] mk_tokenSet_3() { 03195 long[] data = { 21600519973765120L, 0L}; 03196 return data; 03197 } 03198 public static final BitSet _tokenSet_3 = new BitSet(mk_tokenSet_3()); 03199 private static final long[] mk_tokenSet_4() { 03200 long[] data = { 66639127688249328L, 0L}; 03201 return data; 03202 } 03203 public static final BitSet _tokenSet_4 = new BitSet(mk_tokenSet_4()); 03204 private static final long[] mk_tokenSet_5() { 03205 long[] data = { 16L, 0L}; 03206 return data; 03207 } 03208 public static final BitSet _tokenSet_5 = new BitSet(mk_tokenSet_5()); 03209 private static final long[] mk_tokenSet_6() { 03210 long[] data = { 21600519982153760L, 0L}; 03211 return data; 03212 } 03213 public static final BitSet _tokenSet_6 = new BitSet(mk_tokenSet_6()); 03214 private static final long[] mk_tokenSet_7() { 03215 long[] data = { 1329345125679104L, 0L}; 03216 return data; 03217 } 03218 public static final BitSet _tokenSet_7 = new BitSet(mk_tokenSet_7()); 03219 private static final long[] mk_tokenSet_8() { 03220 long[] data = { 20266227045761024L, 0L}; 03221 return data; 03222 } 03223 public static final BitSet _tokenSet_8 = new BitSet(mk_tokenSet_8()); 03224 private static final long[] mk_tokenSet_9() { 03225 long[] data = { 4948070760448L, 0L}; 03226 return data; 03227 } 03228 public static final BitSet _tokenSet_9 = new BitSet(mk_tokenSet_9()); 03229 private static final long[] mk_tokenSet_10() { 03230 long[] data = { 281578093690880L, 0L}; 03231 return data; 03232 } 03233 public static final BitSet _tokenSet_10 = new BitSet(mk_tokenSet_10()); 03234 private static final long[] mk_tokenSet_11() { 03235 long[] data = { 21600519982166048L, 0L}; 03236 return data; 03237 } 03238 public static final BitSet _tokenSet_11 = new BitSet(mk_tokenSet_11()); 03239 private static final long[] mk_tokenSet_12() { 03240 long[] data = { 21600519982215200L, 0L}; 03241 return data; 03242 } 03243 public static final BitSet _tokenSet_12 = new BitSet(mk_tokenSet_12()); 03244 private static final long[] mk_tokenSet_13() { 03245 long[] data = { 21600519982411808L, 0L}; 03246 return data; 03247 } 03248 public static final BitSet _tokenSet_13 = new BitSet(mk_tokenSet_13()); 03249 private static final long[] mk_tokenSet_14() { 03250 long[] data = { 21600519983198240L, 0L}; 03251 return data; 03252 } 03253 public static final BitSet _tokenSet_14 = new BitSet(mk_tokenSet_14()); 03254 private static final long[] mk_tokenSet_15() { 03255 long[] data = { 30610330597982176L, 0L}; 03256 return data; 03257 } 03258 public static final BitSet _tokenSet_15 = new BitSet(mk_tokenSet_15()); 03259 private static final long[] mk_tokenSet_16() { 03260 long[] data = { 8388608L, 0L}; 03261 return data; 03262 } 03263 public static final BitSet _tokenSet_16 = new BitSet(mk_tokenSet_16()); 03264 private static final long[] mk_tokenSet_17() { 03265 long[] data = { 30891805641801696L, 0L}; 03266 return data; 03267 } 03268 public static final BitSet _tokenSet_17 = new BitSet(mk_tokenSet_17()); 03269 private static final long[] mk_tokenSet_18() { 03270 long[] data = { 1329345930985472L, 0L}; 03271 return data; 03272 } 03273 public static final BitSet _tokenSet_18 = new BitSet(mk_tokenSet_18()); 03274 private static final long[] mk_tokenSet_19() { 03275 long[] data = { 2415919104L, 0L}; 03276 return data; 03277 } 03278 public static final BitSet _tokenSet_19 = new BitSet(mk_tokenSet_19()); 03279 private static final long[] mk_tokenSet_20() { 03280 long[] data = { 1333743449014272L, 0L}; 03281 return data; 03282 } 03283 public static final BitSet _tokenSet_20 = new BitSet(mk_tokenSet_20()); 03284 private static final long[] mk_tokenSet_21() { 03285 long[] data = { 1334155765874688L, 0L}; 03286 return data; 03287 } 03288 public static final BitSet _tokenSet_21 = new BitSet(mk_tokenSet_21()); 03289 private static final long[] mk_tokenSet_22() { 03290 long[] data = { 1334155765878720L, 0L}; 03291 return data; 03292 } 03293 public static final BitSet _tokenSet_22 = new BitSet(mk_tokenSet_22()); 03294 private static final long[] mk_tokenSet_23() { 03295 long[] data = { 1329895149928448L, 0L}; 03296 return data; 03297 } 03298 public static final BitSet _tokenSet_23 = new BitSet(mk_tokenSet_23()); 03299 private static final long[] mk_tokenSet_24() { 03300 long[] data = { 1333743440625664L, 0L}; 03301 return data; 03302 } 03303 public static final BitSet _tokenSet_24 = new BitSet(mk_tokenSet_24()); 03304 03305 }