00001
00002 package plp.expressions2.parser;
00003
00004 import plp.expressions2.*;
00005 import plp.expressions2.memory.*;
00006 import plp.expressions2.declaration.*;
00007 import plp.expressions2.expression.*;
00008 import java.util.List;
00009 import java.util.LinkedList;
00010
00011 public class Exp2Parser implements Exp2ParserConstants {
00012
00013 public static void main(String args[]) {
00014 Exp2Parser parser;
00015 if (args.length == 0) {
00016 System.out.println("Expressoes 2 PLP Parser Version 0.0.1: Reading from standard input . . .");
00017 parser = new Exp2Parser(System.in);
00018 } else if (args.length == 1) {
00019 System.out.println("Expressoes 2 PLP Parser Version 0.0.1: Reading from file " + args[0] + " . . .");
00020 try {
00021 parser = new Exp2Parser(new java.io.FileInputStream(args[0]));
00022 } catch (java.io.FileNotFoundException e) {
00023 System.out.println("Expressoes 2 PLP Parser Version 0.0.1: File " + args[0] + " not found.");
00024 return;
00025 }
00026 } else {
00027 System.out.println("Expressoes 2 PLP Parser Version 0.0.1: Usage is one of:");
00028 System.out.println(" java Exp2Parser < inputfile");
00029 System.out.println("OR");
00030 System.out.println(" java Exp2Parser inputfile");
00031 return;
00032 }
00033 try {
00034 Programa programa = parser.Input();
00035 System.out.println("Expressoes 2 PLP Parser Version 0.0.1: Expressoes2 program parsed successfully.");
00036 try {
00037 if (!programa.checaTipo()) {
00038 System.out.println("Erro de tipo.");
00039 } else {
00040 System.out.println(programa.executar());
00041 }
00042 } catch (VariavelJaDeclaradaException ee) {
00043 System.out.println("Erro: " + ee);
00044 ee.printStackTrace();
00045 } catch (VariavelNaoDeclaradaException ee) {
00046 System.out.println("Erro: " + ee);
00047 ee.printStackTrace();
00048 }
00049 } catch (ParseException e) {
00050 e.printStackTrace();
00051 System.out.println("Expressoes 2 PLP Parser Version 0.0.1: Encountered errors during parse.");
00052 }
00053 }
00054
00055 static final public Programa Input() throws ParseException {
00056 Programa retorno;
00057 retorno = PPrograma();
00058 jj_consume_token(0);
00059 {if (true) return retorno;}
00060 throw new Error("Missing return statement in function");
00061 }
00062
00063 static final public Valor PValorInteiro() throws ParseException {
00064 Token token;
00065 token = jj_consume_token(INTEGER_LITERAL);
00066 {if (true) return new ValorInteiro(Integer.parseInt(token.toString()));}
00067 throw new Error("Missing return statement in function");
00068 }
00069
00070 static final public Valor PValorBooleano() throws ParseException {
00071 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
00072 case FALSE:
00073 jj_consume_token(FALSE);
00074 {if (true) return new ValorBooleano(false);}
00075 break;
00076 case TRUE:
00077 jj_consume_token(TRUE);
00078 {if (true) return new ValorBooleano(true);}
00079 break;
00080 default:
00081 jj_la1[0] = jj_gen;
00082 jj_consume_token(-1);
00083 throw new ParseException();
00084 }
00085 throw new Error("Missing return statement in function");
00086 }
00087
00088 static final public Valor PValorString() throws ParseException {
00089 Token token;
00090 token = jj_consume_token(STRING_LITERAL);
00091 String tokenStr = token.toString();
00092 tokenStr = tokenStr.substring(1,tokenStr.length()-1);
00093 {if (true) return new ValorString(tokenStr);}
00094 throw new Error("Missing return statement in function");
00095 }
00096
00097 static final public Valor PValor() throws ParseException {
00098 Valor retorno;
00099 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
00100 case INTEGER_LITERAL:
00101 retorno = PValorInteiro();
00102 break;
00103 case TRUE:
00104 case FALSE:
00105 retorno = PValorBooleano();
00106 break;
00107 case STRING_LITERAL:
00108 retorno = PValorString();
00109 break;
00110 default:
00111 jj_la1[1] = jj_gen;
00112 jj_consume_token(-1);
00113 throw new ParseException();
00114 }
00115 {if (true) return retorno;}
00116 throw new Error("Missing return statement in function");
00117 }
00118
00119 static final public Id PId() throws ParseException {
00120 Token token;
00121 token = jj_consume_token(IDENTIFIER);
00122 String tokenStr = token.toString();
00123
00124 {if (true) return new Id(tokenStr);}
00125 throw new Error("Missing return statement in function");
00126 }
00127
00128 static final public Expressao PExpMenos() throws ParseException {
00129 Expressao retorno;
00130 jj_consume_token(MINUS);
00131 retorno = PExpPrimaria();
00132 {if (true) return new ExpMenos(retorno);}
00133 throw new Error("Missing return statement in function");
00134 }
00135
00136 static final public Expressao PExpNot() throws ParseException {
00137 Expressao retorno;
00138 jj_consume_token(NOT);
00139 retorno = PExpPrimaria();
00140 {if (true) return new ExpNot(retorno);}
00141 throw new Error("Missing return statement in function");
00142 }
00143
00144 static final public Expressao PExpLength() throws ParseException {
00145 Expressao retorno;
00146 jj_consume_token(LENGTH);
00147 retorno = PExpPrimaria();
00148 if (retorno instanceof ValorString) {
00149 ValorString val = (ValorString) retorno;
00150 }
00151 {if (true) return new ExpLength(retorno);}
00152 throw new Error("Missing return statement in function");
00153 }
00154
00155 static final public Expressao PExpPrimaria() throws ParseException {
00156 Expressao retorno;
00157 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
00158 case TRUE:
00159 case FALSE:
00160 case INTEGER_LITERAL:
00161 case STRING_LITERAL:
00162 retorno = PValor();
00163 break;
00164 case IDENTIFIER:
00165 retorno = PId();
00166 break;
00167 case LPAREN:
00168 jj_consume_token(LPAREN);
00169 retorno = PExpressao();
00170 jj_consume_token(RPAREN);
00171 break;
00172 default:
00173 jj_la1[2] = jj_gen;
00174 jj_consume_token(-1);
00175 throw new ParseException();
00176 }
00177 {if (true) return retorno;}
00178 throw new Error("Missing return statement in function");
00179 }
00180
00181 static final public List PDeclVar() throws ParseException {
00182 Id id;
00183 Expressao expressao;
00184 List retorno;
00185 jj_consume_token(VAR);
00186 id = PId();
00187 jj_consume_token(ASSIGN);
00188 expressao = PExpressao();
00189 retorno = new LinkedList();
00190 retorno.add (new DecVariavel(id, expressao));
00191 label_1:
00192 while (true) {
00193 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
00194 case COMMA:
00195 ;
00196 break;
00197 default:
00198 jj_la1[3] = jj_gen;
00199 break label_1;
00200 }
00201 jj_consume_token(COMMA);
00202 jj_consume_token(VAR);
00203 id = PId();
00204 jj_consume_token(ASSIGN);
00205 expressao = PExpressao();
00206 retorno.add (0, new DecVariavel(id, expressao));
00207 }
00208 {if (true) return retorno;}
00209 throw new Error("Missing return statement in function");
00210 }
00211
00212 static final public Expressao PExpDeclaracao() throws ParseException {
00213 List declaracoes;
00214 Expressao expressao;
00215 jj_consume_token(LET);
00216 declaracoes = PDeclVar();
00217 jj_consume_token(IN);
00218 expressao = PExpressao();
00219 {if (true) return new ExpDeclaracao(declaracoes, expressao);}
00220 throw new Error("Missing return statement in function");
00221 }
00222
00223 static final public Expressao PExpUnaria() throws ParseException {
00224 Expressao retorno;
00225 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
00226 case MINUS:
00227 retorno = PExpMenos();
00228 break;
00229 case NOT:
00230 retorno = PExpNot();
00231 break;
00232 case LENGTH:
00233 retorno = PExpLength();
00234 break;
00235 case TRUE:
00236 case FALSE:
00237 case INTEGER_LITERAL:
00238 case STRING_LITERAL:
00239 case IDENTIFIER:
00240 case LPAREN:
00241 retorno = PExpPrimaria();
00242 break;
00243 case LET:
00244 retorno = PExpDeclaracao();
00245 break;
00246 default:
00247 jj_la1[4] = jj_gen;
00248 jj_consume_token(-1);
00249 throw new ParseException();
00250 }
00251 {if (true) return retorno;}
00252 throw new Error("Missing return statement in function");
00253 }
00254
00255 static final public Expressao PExpBinaria() throws ParseException {
00256 Expressao retorno, param2;
00257 retorno = PExpUnaria();
00258 label_2:
00259 while (true) {
00260 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
00261 case AND:
00262 case OR:
00263 case EQ:
00264 case CONCAT:
00265 case PLUS:
00266 case MINUS:
00267 ;
00268 break;
00269 default:
00270 jj_la1[5] = jj_gen;
00271 break label_2;
00272 }
00273 switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
00274 case PLUS:
00275 jj_consume_token(PLUS);
00276 param2 = PExpUnaria();
00277 retorno = new ExpSoma(retorno, param2);
00278 break;
00279 case MINUS:
00280 jj_consume_token(MINUS);
00281 param2 = PExpUnaria();
00282 retorno = new ExpSub(retorno, param2);
00283 break;
00284 case AND:
00285 jj_consume_token(AND);
00286 param2 = PExpUnaria();
00287 retorno = new ExpAnd(retorno, param2);
00288 break;
00289 case OR:
00290 jj_consume_token(OR);
00291 param2 = PExpUnaria();
00292 retorno = new ExpOr(retorno, param2);
00293 break;
00294 case EQ:
00295 jj_consume_token(EQ);
00296 param2 = PExpUnaria();
00297 retorno = new ExpEquals(retorno, param2);
00298 break;
00299 case CONCAT:
00300 jj_consume_token(CONCAT);
00301 param2 = PExpUnaria();
00302 retorno = new ExpConcat(retorno, param2);
00303 break;
00304 default:
00305 jj_la1[6] = jj_gen;
00306 jj_consume_token(-1);
00307 throw new ParseException();
00308 }
00309 }
00310 {if (true) return retorno;}
00311 throw new Error("Missing return statement in function");
00312 }
00313
00314 static final public Expressao PExpressao() throws ParseException {
00315 Expressao retorno;
00316 retorno = PExpBinaria();
00317 {if (true) return retorno;}
00318 throw new Error("Missing return statement in function");
00319 }
00320
00321 static final public Programa PPrograma() throws ParseException {
00322 Expressao retorno;
00323 retorno = PExpressao();
00324 {if (true) return new Programa(retorno);}
00325 throw new Error("Missing return statement in function");
00326 }
00327
00328 static private boolean jj_initialized_once = false;
00329 static public Exp2ParserTokenManager token_source;
00330 static JavaCharStream jj_input_stream;
00331 static public Token token, jj_nt;
00332 static private int jj_ntk;
00333 static private int jj_gen;
00334 static final private int[] jj_la1 = new int[7];
00335 static private int[] jj_la1_0;
00336 static private int[] jj_la1_1;
00337 static {
00338 jj_la1_0();
00339 jj_la1_1();
00340 }
00341 private static void jj_la1_0() {
00342 jj_la1_0 = new int[] {0x6000,0x446000,0x4c46000,0x0,0x4c4f800,0x600,0x600,};
00343 }
00344 private static void jj_la1_1() {
00345 jj_la1_1 = new int[] {0x0,0x0,0x0,0x2,0x40000,0x70400,0x70400,};
00346 }
00347
00348 public Exp2Parser(java.io.InputStream stream) {
00349 this(stream, null);
00350 }
00351 public Exp2Parser(java.io.InputStream stream, String encoding) {
00352 if (jj_initialized_once) {
00353 System.out.println("ERROR: Second call to constructor of static parser. You must");
00354 System.out.println(" either use ReInit() or set the JavaCC option STATIC to false");
00355 System.out.println(" during parser generation.");
00356 throw new Error();
00357 }
00358 jj_initialized_once = true;
00359 try { jj_input_stream = new JavaCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
00360 token_source = new Exp2ParserTokenManager(jj_input_stream);
00361 token = new Token();
00362 jj_ntk = -1;
00363 jj_gen = 0;
00364 for (int i = 0; i < 7; i++) jj_la1[i] = -1;
00365 }
00366
00367 static public void ReInit(java.io.InputStream stream) {
00368 ReInit(stream, null);
00369 }
00370 static public void ReInit(java.io.InputStream stream, String encoding) {
00371 try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
00372 token_source.ReInit(jj_input_stream);
00373 token = new Token();
00374 jj_ntk = -1;
00375 jj_gen = 0;
00376 for (int i = 0; i < 7; i++) jj_la1[i] = -1;
00377 }
00378
00379 public Exp2Parser(java.io.Reader stream) {
00380 if (jj_initialized_once) {
00381 System.out.println("ERROR: Second call to constructor of static parser. You must");
00382 System.out.println(" either use ReInit() or set the JavaCC option STATIC to false");
00383 System.out.println(" during parser generation.");
00384 throw new Error();
00385 }
00386 jj_initialized_once = true;
00387 jj_input_stream = new JavaCharStream(stream, 1, 1);
00388 token_source = new Exp2ParserTokenManager(jj_input_stream);
00389 token = new Token();
00390 jj_ntk = -1;
00391 jj_gen = 0;
00392 for (int i = 0; i < 7; i++) jj_la1[i] = -1;
00393 }
00394
00395 static public void ReInit(java.io.Reader stream) {
00396 jj_input_stream.ReInit(stream, 1, 1);
00397 token_source.ReInit(jj_input_stream);
00398 token = new Token();
00399 jj_ntk = -1;
00400 jj_gen = 0;
00401 for (int i = 0; i < 7; i++) jj_la1[i] = -1;
00402 }
00403
00404 public Exp2Parser(Exp2ParserTokenManager tm) {
00405 if (jj_initialized_once) {
00406 System.out.println("ERROR: Second call to constructor of static parser. You must");
00407 System.out.println(" either use ReInit() or set the JavaCC option STATIC to false");
00408 System.out.println(" during parser generation.");
00409 throw new Error();
00410 }
00411 jj_initialized_once = true;
00412 token_source = tm;
00413 token = new Token();
00414 jj_ntk = -1;
00415 jj_gen = 0;
00416 for (int i = 0; i < 7; i++) jj_la1[i] = -1;
00417 }
00418
00419 public void ReInit(Exp2ParserTokenManager tm) {
00420 token_source = tm;
00421 token = new Token();
00422 jj_ntk = -1;
00423 jj_gen = 0;
00424 for (int i = 0; i < 7; i++) jj_la1[i] = -1;
00425 }
00426
00427 static final private Token jj_consume_token(int kind) throws ParseException {
00428 Token oldToken;
00429 if ((oldToken = token).next != null) token = token.next;
00430 else token = token.next = token_source.getNextToken();
00431 jj_ntk = -1;
00432 if (token.kind == kind) {
00433 jj_gen++;
00434 return token;
00435 }
00436 token = oldToken;
00437 jj_kind = kind;
00438 throw generateParseException();
00439 }
00440
00441 static final public Token getNextToken() {
00442 if (token.next != null) token = token.next;
00443 else token = token.next = token_source.getNextToken();
00444 jj_ntk = -1;
00445 jj_gen++;
00446 return token;
00447 }
00448
00449 static final public Token getToken(int index) {
00450 Token t = token;
00451 for (int i = 0; i < index; i++) {
00452 if (t.next != null) t = t.next;
00453 else t = t.next = token_source.getNextToken();
00454 }
00455 return t;
00456 }
00457
00458 static final private int jj_ntk() {
00459 if ((jj_nt=token.next) == null)
00460 return (jj_ntk = (token.next=token_source.getNextToken()).kind);
00461 else
00462 return (jj_ntk = jj_nt.kind);
00463 }
00464
00465 static private java.util.Vector<int[]> jj_expentries = new java.util.Vector<int[]>();
00466 static private int[] jj_expentry;
00467 static private int jj_kind = -1;
00468
00469 static public ParseException generateParseException() {
00470 jj_expentries.removeAllElements();
00471 boolean[] la1tokens = new boolean[57];
00472 for (int i = 0; i < 57; i++) {
00473 la1tokens[i] = false;
00474 }
00475 if (jj_kind >= 0) {
00476 la1tokens[jj_kind] = true;
00477 jj_kind = -1;
00478 }
00479 for (int i = 0; i < 7; i++) {
00480 if (jj_la1[i] == jj_gen) {
00481 for (int j = 0; j < 32; j++) {
00482 if ((jj_la1_0[i] & (1<<j)) != 0) {
00483 la1tokens[j] = true;
00484 }
00485 if ((jj_la1_1[i] & (1<<j)) != 0) {
00486 la1tokens[32+j] = true;
00487 }
00488 }
00489 }
00490 }
00491 for (int i = 0; i < 57; i++) {
00492 if (la1tokens[i]) {
00493 jj_expentry = new int[1];
00494 jj_expentry[0] = i;
00495 jj_expentries.addElement(jj_expentry);
00496 }
00497 }
00498 int[][] exptokseq = new int[jj_expentries.size()][];
00499 for (int i = 0; i < jj_expentries.size(); i++) {
00500 exptokseq[i] = (int[])jj_expentries.elementAt(i);
00501 }
00502 return new ParseException(token, exptokseq, tokenImage);
00503 }
00504
00505 static final public void enable_tracing() {
00506 }
00507
00508 static final public void disable_tracing() {
00509 }
00510
00511 }