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;
00330 static public Exp2ParserTokenManager token_source;
00331 static JavaCharStream jj_input_stream;
00333 static public Token token;
00335 static public Token jj_nt;
00336 static private int jj_ntk;
00337 static private int jj_gen;
00338 static final private int[] jj_la1 = new int[7];
00339 static private int[] jj_la1_0;
00340 static private int[] jj_la1_1;
00341 static {
00342 jj_la1_init_0();
00343 jj_la1_init_1();
00344 }
00345 private static void jj_la1_init_0() {
00346 jj_la1_0 = new int[] {0x6000,0x446000,0x4c46000,0x0,0x4c4f800,0x600,0x600,};
00347 }
00348 private static void jj_la1_init_1() {
00349 jj_la1_1 = new int[] {0x0,0x0,0x0,0x2,0x40000,0x70400,0x70400,};
00350 }
00351
00353 public Exp2Parser(java.io.InputStream stream) {
00354 this(stream, null);
00355 }
00357 public Exp2Parser(java.io.InputStream stream, String encoding) {
00358 if (jj_initialized_once) {
00359 System.out.println("ERROR: Second call to constructor of static parser. ");
00360 System.out.println(" You must either use ReInit() or set the JavaCC option STATIC to false");
00361 System.out.println(" during parser generation.");
00362 throw new Error();
00363 }
00364 jj_initialized_once = true;
00365 try { jj_input_stream = new JavaCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
00366 token_source = new Exp2ParserTokenManager(jj_input_stream);
00367 token = new Token();
00368 jj_ntk = -1;
00369 jj_gen = 0;
00370 for (int i = 0; i < 7; i++) jj_la1[i] = -1;
00371 }
00372
00374 static public void ReInit(java.io.InputStream stream) {
00375 ReInit(stream, null);
00376 }
00378 static public void ReInit(java.io.InputStream stream, String encoding) {
00379 try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
00380 token_source.ReInit(jj_input_stream);
00381 token = new Token();
00382 jj_ntk = -1;
00383 jj_gen = 0;
00384 for (int i = 0; i < 7; i++) jj_la1[i] = -1;
00385 }
00386
00388 public Exp2Parser(java.io.Reader stream) {
00389 if (jj_initialized_once) {
00390 System.out.println("ERROR: Second call to constructor of static parser. ");
00391 System.out.println(" You must either use ReInit() or set the JavaCC option STATIC to false");
00392 System.out.println(" during parser generation.");
00393 throw new Error();
00394 }
00395 jj_initialized_once = true;
00396 jj_input_stream = new JavaCharStream(stream, 1, 1);
00397 token_source = new Exp2ParserTokenManager(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
00405 static public void ReInit(java.io.Reader stream) {
00406 jj_input_stream.ReInit(stream, 1, 1);
00407 token_source.ReInit(jj_input_stream);
00408 token = new Token();
00409 jj_ntk = -1;
00410 jj_gen = 0;
00411 for (int i = 0; i < 7; i++) jj_la1[i] = -1;
00412 }
00413
00415 public Exp2Parser(Exp2ParserTokenManager tm) {
00416 if (jj_initialized_once) {
00417 System.out.println("ERROR: Second call to constructor of static parser. ");
00418 System.out.println(" You must either use ReInit() or set the JavaCC option STATIC to false");
00419 System.out.println(" during parser generation.");
00420 throw new Error();
00421 }
00422 jj_initialized_once = true;
00423 token_source = tm;
00424 token = new Token();
00425 jj_ntk = -1;
00426 jj_gen = 0;
00427 for (int i = 0; i < 7; i++) jj_la1[i] = -1;
00428 }
00429
00431 public void ReInit(Exp2ParserTokenManager tm) {
00432 token_source = tm;
00433 token = new Token();
00434 jj_ntk = -1;
00435 jj_gen = 0;
00436 for (int i = 0; i < 7; i++) jj_la1[i] = -1;
00437 }
00438
00439 static private Token jj_consume_token(int kind) throws ParseException {
00440 Token oldToken;
00441 if ((oldToken = token).next != null) token = token.next;
00442 else token = token.next = token_source.getNextToken();
00443 jj_ntk = -1;
00444 if (token.kind == kind) {
00445 jj_gen++;
00446 return token;
00447 }
00448 token = oldToken;
00449 jj_kind = kind;
00450 throw generateParseException();
00451 }
00452
00453
00455 static final public Token getNextToken() {
00456 if (token.next != null) token = token.next;
00457 else token = token.next = token_source.getNextToken();
00458 jj_ntk = -1;
00459 jj_gen++;
00460 return token;
00461 }
00462
00464 static final public Token getToken(int index) {
00465 Token t = token;
00466 for (int i = 0; i < index; i++) {
00467 if (t.next != null) t = t.next;
00468 else t = t.next = token_source.getNextToken();
00469 }
00470 return t;
00471 }
00472
00473 static private int jj_ntk() {
00474 if ((jj_nt=token.next) == null)
00475 return (jj_ntk = (token.next=token_source.getNextToken()).kind);
00476 else
00477 return (jj_ntk = jj_nt.kind);
00478 }
00479
00480 static private java.util.List jj_expentries = new java.util.ArrayList();
00481 static private int[] jj_expentry;
00482 static private int jj_kind = -1;
00483
00485 static public ParseException generateParseException() {
00486 jj_expentries.clear();
00487 boolean[] la1tokens = new boolean[57];
00488 if (jj_kind >= 0) {
00489 la1tokens[jj_kind] = true;
00490 jj_kind = -1;
00491 }
00492 for (int i = 0; i < 7; i++) {
00493 if (jj_la1[i] == jj_gen) {
00494 for (int j = 0; j < 32; j++) {
00495 if ((jj_la1_0[i] & (1<<j)) != 0) {
00496 la1tokens[j] = true;
00497 }
00498 if ((jj_la1_1[i] & (1<<j)) != 0) {
00499 la1tokens[32+j] = true;
00500 }
00501 }
00502 }
00503 }
00504 for (int i = 0; i < 57; i++) {
00505 if (la1tokens[i]) {
00506 jj_expentry = new int[1];
00507 jj_expentry[0] = i;
00508 jj_expentries.add(jj_expentry);
00509 }
00510 }
00511 int[][] exptokseq = new int[jj_expentries.size()][];
00512 for (int i = 0; i < jj_expentries.size(); i++) {
00513 exptokseq[i] = (int[])jj_expentries.get(i);
00514 }
00515 return new ParseException(token, exptokseq, tokenImage);
00516 }
00517
00519 static final public void enable_tracing() {
00520 }
00521
00523 static final public void disable_tracing() {
00524 }
00525
00526 }