00001 package edu.ksu.cis.bandera.abstraction.options.parser;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 import edu.ksu.cis.bandera.abstraction.options.lexer.*;
00039 import edu.ksu.cis.bandera.abstraction.options.node.*;
00040 import edu.ksu.cis.bandera.abstraction.options.analysis.*;
00041 import java.util.*;
00042
00043 import java.io.DataInputStream;
00044 import java.io.BufferedInputStream;
00045 import java.io.IOException;
00046
00047 public class Parser
00048 {
00049 public final Analysis ignoredTokens = new AnalysisAdapter();
00050
00051 protected Node node;
00052
00053 private final Lexer lexer;
00054 private final ListIterator stack = new LinkedList().listIterator();
00055 private int last_shift;
00056 private int last_pos;
00057 private int last_line;
00058 private final TokenIndex converter = new TokenIndex();
00059 private final int[] action = new int[2];
00060
00061 private final static int SHIFT = 0;
00062 private final static int REDUCE = 1;
00063 private final static int ACCEPT = 2;
00064 private final static int ERROR = 3;
00065
00066 private static int[][][] actionTable;
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119 private static int[][][] gotoTable;
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 private static String[] errorMessages;
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 private static int[] errors;
00152
00153
00154
00155 public Parser(Lexer lexer)
00156 {
00157 this.lexer = lexer;
00158
00159 if(actionTable == null)
00160 {
00161 try
00162 {
00163 DataInputStream s = new DataInputStream(
00164 new BufferedInputStream(
00165 Parser.class.getResourceAsStream("parser.dat")));
00166
00167
00168 int length = s.readInt();
00169 actionTable = new int[length][][];
00170 for(int i = 0; i < actionTable.length; i++)
00171 {
00172 length = s.readInt();
00173 actionTable[i] = new int[length][3];
00174 for(int j = 0; j < actionTable[i].length; j++)
00175 {
00176 for(int k = 0; k < 3; k++)
00177 {
00178 actionTable[i][j][k] = s.readInt();
00179 }
00180 }
00181 }
00182
00183
00184 length = s.readInt();
00185 gotoTable = new int[length][][];
00186 for(int i = 0; i < gotoTable.length; i++)
00187 {
00188 length = s.readInt();
00189 gotoTable[i] = new int[length][2];
00190 for(int j = 0; j < gotoTable[i].length; j++)
00191 {
00192 for(int k = 0; k < 2; k++)
00193 {
00194 gotoTable[i][j][k] = s.readInt();
00195 }
00196 }
00197 }
00198
00199
00200 length = s.readInt();
00201 errorMessages = new String[length];
00202 for(int i = 0; i < errorMessages.length; i++)
00203 {
00204 length = s.readInt();
00205 StringBuffer buffer = new StringBuffer();
00206
00207 for(int j = 0; j < length; j++)
00208 {
00209 buffer.append(s.readChar());
00210 }
00211 errorMessages[i] = buffer.toString();
00212 }
00213
00214
00215 length = s.readInt();
00216 errors = new int[length];
00217 for(int i = 0; i < errors.length; i++)
00218 {
00219 errors[i] = s.readInt();
00220 }
00221
00222 s.close();
00223 }
00224 catch(Exception e)
00225 {
00226 throw new RuntimeException("Unable to read parser.dat.");
00227 }
00228 }
00229 }
00230 protected void filter() throws ParserException, LexerException, IOException
00231 {
00232 }
00233 private int goTo(int index)
00234 {
00235 int state = state();
00236 int low = 1;
00237 int high = gotoTable[index].length - 1;
00238 int value = gotoTable[index][0][1];
00239
00240 while(low <= high)
00241 {
00242 int middle = (low + high) / 2;
00243
00244 if(state < gotoTable[index][middle][0])
00245 {
00246 high = middle - 1;
00247 }
00248 else if(state > gotoTable[index][middle][0])
00249 {
00250 low = middle + 1;
00251 }
00252 else
00253 {
00254 value = gotoTable[index][middle][1];
00255 break;
00256 }
00257 }
00258
00259 return value;
00260 }
00261 private int index(Switchable token)
00262 {
00263 converter.index = -1;
00264 token.apply(converter);
00265 return converter.index;
00266 }
00267 Node new0()
00268 {
00269 XPClassOption node1 = null;
00270 AUnit node = new AUnit(node1);
00271 return node;
00272 }
00273 Node new1()
00274 {
00275 XPClassOption node1 = (XPClassOption) pop();
00276 AUnit node = new AUnit(node1);
00277 return node;
00278 }
00279 Node new10()
00280 {
00281 PMethodOption node1 = (PMethodOption) pop();
00282 X2PMethodOption node = new X2PMethodOption(node1);
00283 return node;
00284 }
00285 Node new11()
00286 {
00287 TRBrace node6 = (TRBrace) pop();
00288 XPMethodOption node5 = (XPMethodOption) pop();
00289 XPFieldOption node4 = (XPFieldOption) pop();
00290 TLBrace node3 = (TLBrace) pop();
00291 PName node2 = (PName) pop();
00292 TCls node1 = (TCls) pop();
00293 AClassOption node = new AClassOption(node1, node2, node3, node4, node5, node6);
00294 return node;
00295 }
00296 Node new12()
00297 {
00298 TId node1 = (TId) pop();
00299 ASimpleName node = new ASimpleName(node1);
00300 return node;
00301 }
00302 Node new13()
00303 {
00304 TId node3 = (TId) pop();
00305 TDot node2 = (TDot) pop();
00306 PName node1 = (PName) pop();
00307 AQualifiedName node = new AQualifiedName(node1, node2, node3);
00308 return node;
00309 }
00310 Node new14()
00311 {
00312 TSemicolon node3 = (TSemicolon) pop();
00313 PName node2 = (PName) pop();
00314 TId node1 = (TId) pop();
00315 AFieldOption node = new AFieldOption(node1, node2, node3);
00316 return node;
00317 }
00318 Node new15()
00319 {
00320 TRBrace node7 = (TRBrace) pop();
00321 XPLocalOption node6 = null;
00322 TLBrace node5 = (TLBrace) pop();
00323 TRParen node4 = (TRParen) pop();
00324 PParams node3 = null;
00325 TLParen node2 = (TLParen) pop();
00326 TId node1 = (TId) pop();
00327 AMethodOption node = new AMethodOption(node1, node2, node3, node4, node5, node6, node7);
00328 return node;
00329 }
00330 Node new16()
00331 {
00332 TRBrace node7 = (TRBrace) pop();
00333 XPLocalOption node6 = null;
00334 TLBrace node5 = (TLBrace) pop();
00335 TRParen node4 = (TRParen) pop();
00336 PParams node3 = (PParams) pop();
00337 TLParen node2 = (TLParen) pop();
00338 TId node1 = (TId) pop();
00339 AMethodOption node = new AMethodOption(node1, node2, node3, node4, node5, node6, node7);
00340 return node;
00341 }
00342 Node new17()
00343 {
00344 TRBrace node7 = (TRBrace) pop();
00345 XPLocalOption node6 = (XPLocalOption) pop();
00346 TLBrace node5 = (TLBrace) pop();
00347 TRParen node4 = (TRParen) pop();
00348 PParams node3 = null;
00349 TLParen node2 = (TLParen) pop();
00350 TId node1 = (TId) pop();
00351 AMethodOption node = new AMethodOption(node1, node2, node3, node4, node5, node6, node7);
00352 return node;
00353 }
00354 Node new18()
00355 {
00356 PLocalOption node2 = (PLocalOption) pop();
00357 XPLocalOption node1 = (XPLocalOption) pop();
00358 X1PLocalOption node = new X1PLocalOption(node1, node2);
00359 return node;
00360 }
00361 Node new19()
00362 {
00363 PLocalOption node1 = (PLocalOption) pop();
00364 X2PLocalOption node = new X2PLocalOption(node1);
00365 return node;
00366 }
00367 Node new2()
00368 {
00369 PClassOption node2 = (PClassOption) pop();
00370 XPClassOption node1 = (XPClassOption) pop();
00371 X1PClassOption node = new X1PClassOption(node1, node2);
00372 return node;
00373 }
00374 Node new20()
00375 {
00376 TRBrace node7 = (TRBrace) pop();
00377 XPLocalOption node6 = (XPLocalOption) pop();
00378 TLBrace node5 = (TLBrace) pop();
00379 TRParen node4 = (TRParen) pop();
00380 PParams node3 = (PParams) pop();
00381 TLParen node2 = (TLParen) pop();
00382 TId node1 = (TId) pop();
00383 AMethodOption node = new AMethodOption(node1, node2, node3, node4, node5, node6, node7);
00384 return node;
00385 }
00386 Node new21()
00387 {
00388 XTDim node2 = null;
00389 PName node1 = (PName) pop();
00390 AParamParams node = new AParamParams(node1, node2);
00391 return node;
00392 }
00393 Node new22()
00394 {
00395 XTDim node2 = (XTDim) pop();
00396 PName node1 = (PName) pop();
00397 AParamParams node = new AParamParams(node1, node2);
00398 return node;
00399 }
00400 Node new23()
00401 {
00402 TDim node2 = (TDim) pop();
00403 XTDim node1 = (XTDim) pop();
00404 X1TDim node = new X1TDim(node1, node2);
00405 return node;
00406 }
00407 Node new24()
00408 {
00409 TDim node1 = (TDim) pop();
00410 X2TDim node = new X2TDim(node1);
00411 return node;
00412 }
00413 Node new25()
00414 {
00415 XTDim node4 = null;
00416 PName node3 = (PName) pop();
00417 TComma node2 = (TComma) pop();
00418 PParams node1 = (PParams) pop();
00419 AParamsParams node = new AParamsParams(node1, node2, node3, node4);
00420 return node;
00421 }
00422 Node new26()
00423 {
00424 XTDim node4 = (XTDim) pop();
00425 PName node3 = (PName) pop();
00426 TComma node2 = (TComma) pop();
00427 PParams node1 = (PParams) pop();
00428 AParamsParams node = new AParamsParams(node1, node2, node3, node4);
00429 return node;
00430 }
00431 Node new27()
00432 {
00433 TSemicolon node3 = (TSemicolon) pop();
00434 PName node2 = (PName) pop();
00435 TId node1 = (TId) pop();
00436 ALocalOption node = new ALocalOption(node1, node2, node3);
00437 return node;
00438 }
00439 Node new3()
00440 {
00441 PClassOption node1 = (PClassOption) pop();
00442 X2PClassOption node = new X2PClassOption(node1);
00443 return node;
00444 }
00445 Node new4()
00446 {
00447 TRBrace node6 = (TRBrace) pop();
00448 XPMethodOption node5 = null;
00449 XPFieldOption node4 = null;
00450 TLBrace node3 = (TLBrace) pop();
00451 PName node2 = (PName) pop();
00452 TCls node1 = (TCls) pop();
00453 AClassOption node = new AClassOption(node1, node2, node3, node4, node5, node6);
00454 return node;
00455 }
00456 Node new5()
00457 {
00458 TRBrace node6 = (TRBrace) pop();
00459 XPMethodOption node5 = null;
00460 XPFieldOption node4 = (XPFieldOption) pop();
00461 TLBrace node3 = (TLBrace) pop();
00462 PName node2 = (PName) pop();
00463 TCls node1 = (TCls) pop();
00464 AClassOption node = new AClassOption(node1, node2, node3, node4, node5, node6);
00465 return node;
00466 }
00467 Node new6()
00468 {
00469 PFieldOption node2 = (PFieldOption) pop();
00470 XPFieldOption node1 = (XPFieldOption) pop();
00471 X1PFieldOption node = new X1PFieldOption(node1, node2);
00472 return node;
00473 }
00474 Node new7()
00475 {
00476 PFieldOption node1 = (PFieldOption) pop();
00477 X2PFieldOption node = new X2PFieldOption(node1);
00478 return node;
00479 }
00480 Node new8()
00481 {
00482 TRBrace node6 = (TRBrace) pop();
00483 XPMethodOption node5 = (XPMethodOption) pop();
00484 XPFieldOption node4 = null;
00485 TLBrace node3 = (TLBrace) pop();
00486 PName node2 = (PName) pop();
00487 TCls node1 = (TCls) pop();
00488 AClassOption node = new AClassOption(node1, node2, node3, node4, node5, node6);
00489 return node;
00490 }
00491 Node new9()
00492 {
00493 PMethodOption node2 = (PMethodOption) pop();
00494 XPMethodOption node1 = (XPMethodOption) pop();
00495 X1PMethodOption node = new X1PMethodOption(node1, node2);
00496 return node;
00497 }
00498 public Start parse() throws ParserException, LexerException, IOException
00499 {
00500 push(0, null, false);
00501
00502 List ign = null;
00503 while(true)
00504 {
00505 while(index(lexer.peek()) == -1)
00506 {
00507 if(ign == null)
00508 {
00509 ign = new TypedLinkedList(NodeCast.instance);
00510 }
00511
00512 ign.add(lexer.next());
00513 }
00514
00515 if(ign != null)
00516 {
00517 ignoredTokens.setIn(lexer.peek(), ign);
00518 ign = null;
00519 }
00520
00521 last_pos = lexer.peek().getPos();
00522 last_line = lexer.peek().getLine();
00523
00524 int index = index(lexer.peek());
00525 action[0] = actionTable[state()][0][1];
00526 action[1] = actionTable[state()][0][2];
00527
00528 int low = 1;
00529 int high = actionTable[state()].length - 1;
00530
00531 while(low <= high)
00532 {
00533 int middle = (low + high) / 2;
00534
00535 if(index < actionTable[state()][middle][0])
00536 {
00537 high = middle - 1;
00538 }
00539 else if(index > actionTable[state()][middle][0])
00540 {
00541 low = middle + 1;
00542 }
00543 else
00544 {
00545 action[0] = actionTable[state()][middle][1];
00546 action[1] = actionTable[state()][middle][2];
00547 break;
00548 }
00549 }
00550
00551 switch(action[0])
00552 {
00553 case SHIFT:
00554 push(action[1], lexer.next(), true);
00555 last_shift = action[1];
00556 break;
00557 case REDUCE:
00558 switch(action[1])
00559 {
00560 case 0: { Node node = new0(); push(goTo(0), node, true); } break;
00561 case 1: { Node node = new1(); push(goTo(0), node, true); } break;
00562 case 2: { Node node = new2(); push(goTo(7), node, false); } break;
00563 case 3: { Node node = new3(); push(goTo(7), node, false); } break;
00564 case 4: { Node node = new4(); push(goTo(1), node, true); } break;
00565 case 5: { Node node = new5(); push(goTo(1), node, true); } break;
00566 case 6: { Node node = new6(); push(goTo(8), node, false); } break;
00567 case 7: { Node node = new7(); push(goTo(8), node, false); } break;
00568 case 8: { Node node = new8(); push(goTo(1), node, true); } break;
00569 case 9: { Node node = new9(); push(goTo(9), node, false); } break;
00570 case 10: { Node node = new10(); push(goTo(9), node, false); } break;
00571 case 11: { Node node = new11(); push(goTo(1), node, true); } break;
00572 case 12: { Node node = new12(); push(goTo(2), node, true); } break;
00573 case 13: { Node node = new13(); push(goTo(2), node, true); } break;
00574 case 14: { Node node = new14(); push(goTo(3), node, true); } break;
00575 case 15: { Node node = new15(); push(goTo(4), node, true); } break;
00576 case 16: { Node node = new16(); push(goTo(4), node, true); } break;
00577 case 17: { Node node = new17(); push(goTo(4), node, true); } break;
00578 case 18: { Node node = new18(); push(goTo(10), node, false); } break;
00579 case 19: { Node node = new19(); push(goTo(10), node, false); } break;
00580 case 20: { Node node = new20(); push(goTo(4), node, true); } break;
00581 case 21: { Node node = new21(); push(goTo(5), node, true); } break;
00582 case 22: { Node node = new22(); push(goTo(5), node, true); } break;
00583 case 23: { Node node = new23(); push(goTo(11), node, false); } break;
00584 case 24: { Node node = new24(); push(goTo(11), node, false); } break;
00585 case 25: { Node node = new25(); push(goTo(5), node, true); } break;
00586 case 26: { Node node = new26(); push(goTo(5), node, true); } break;
00587 case 27: { Node node = new27(); push(goTo(6), node, true); } break;
00588 }
00589 break;
00590 case ACCEPT:
00591 {
00592 EOF node2 = (EOF) lexer.next();
00593 PUnit node1 = (PUnit) pop();
00594 Start node = new Start(node1, node2);
00595 return node;
00596 }
00597 case ERROR:
00598 throw new ParserException(
00599 "[" + last_line + "," + last_pos + "] " +
00600 errorMessages[errors[action[1]]]);
00601 }
00602 }
00603 }
00604 private Node pop()
00605 {
00606 return (Node) ((State) stack.previous()).node;
00607 }
00608 private void push(int state, Node node, boolean filter) throws ParserException, LexerException, IOException
00609 {
00610 this.node = node;
00611
00612 if(filter)
00613 {
00614 filter();
00615 }
00616
00617 if(!stack.hasNext())
00618 {
00619 stack.add(new State(state, this.node));
00620 return;
00621 }
00622
00623 State s = (State) stack.next();
00624 s.state = state;
00625 s.node = this.node;
00626 }
00627 private int state()
00628 {
00629 State s = (State) stack.previous();
00630 stack.next();
00631 return s.state;
00632 }
00633 }