Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Parser.java

00001 package edu.ksu.cis.bandera.specification.assertion.parser;
00002 
00003 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00004  * Bandera, a Java(TM) analysis and transformation toolkit           *
00005  * Copyright (C) 1998-2001 SAnToS Laboratories (santos@cis.ksu.edu)  *
00006 
00007  * All rights reserved.                                              *
00008  *                                                                   *
00009  * This work was done as a project in the SAnToS Laboratory,         *
00010  * Department of Computing and Information Sciences, Kansas State    *
00011  * University, USA (http://www.cis.ksu.edu/santos).                  *
00012  * It is understood that any modification not identified as such is  *
00013  * not covered by the preceding statement.                           *
00014  *                                                                   *
00015  * This work is free software; you can redistribute it and/or        *
00016  * modify it under the terms of the GNU Library General Public       *
00017  * License as published by the Free Software Foundation; either      *
00018  * version 2 of the License, or (at your option) any later version.  *
00019  *                                                                   *
00020  * This work is distributed in the hope that it will be useful,      *
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of    *
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU *
00023  * Library General Public License for more details.                  *
00024  *                                                                   *
00025  * You should have received a copy of the GNU Library General Public *
00026  * License along with this toolkit; if not, write to the             *
00027  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,      *
00028  * Boston, MA  02111-1307, USA.                                      *
00029  *                                                                   *
00030  * Java is a trademark of Sun Microsystems, Inc.                     *
00031  *                                                                   *
00032  * To submit a bug report, send a comment, or get the latest news on *
00033  * this project and other SAnToS projects, please visit the web-site *
00034  *                http://www.cis.ksu.edu/santos                      *
00035  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00036 /* This file was generated by SableCC (http://www.sable.mcgill.ca/sablecc/). */
00037 
00038 import edu.ksu.cis.bandera.specification.assertion.lexer.*;
00039 import edu.ksu.cis.bandera.specification.assertion.node.*;
00040 import edu.ksu.cis.bandera.specification.assertion.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             {{-1, REDUCE, 0}, {2, SHIFT, 1}, {8, SHIFT, 2}, {9, SHIFT, 3}, {10, SHIFT, 4}, {11, SHIFT, 5}, },
00069             {{-1, REDUCE, 12}, },
00070             {{-1, ERROR, 2}, {11, SHIFT, 12}, },
00071             {{-1, ERROR, 3}, {11, SHIFT, 13}, },
00072             {{-1, ERROR, 4}, {3, SHIFT, 14}, },
00073             {{-1, REDUCE, 19}, },
00074             {{-1, ERROR, 6}, {12, ACCEPT, -1}, },
00075             {{-1, REDUCE, 4}, },
00076             {{-1, REDUCE, 8}, },
00077             {{-1, REDUCE, 1}, {2, SHIFT, 1}, {7, SHIFT, 15}, {8, SHIFT, 2}, {9, SHIFT, 3}, {10, SHIFT, 4}, },
00078             {{-1, REDUCE, 2}, {2, SHIFT, 1}, {8, SHIFT, 2}, {9, SHIFT, 3}, {10, SHIFT, 4}, },
00079             {{-1, REDUCE, 6}, {8, SHIFT, 2}, {9, SHIFT, 3}, {10, SHIFT, 4}, },
00080             {{-1, ERROR, 12}, {5, SHIFT, 21}, },
00081             {{-1, ERROR, 13}, {5, SHIFT, 22}, },
00082             {{-1, ERROR, 14}, {11, SHIFT, 23}, },
00083             {{-1, ERROR, 15}, {11, SHIFT, 24}, },
00084             {{-1, REDUCE, 5}, {2, SHIFT, 1}, {8, SHIFT, 2}, {9, SHIFT, 3}, {10, SHIFT, 4}, },
00085             {{-1, REDUCE, 9}, {8, SHIFT, 2}, {9, SHIFT, 3}, {10, SHIFT, 4}, },
00086             {{-1, REDUCE, 3}, },
00087             {{-1, REDUCE, 10}, {8, SHIFT, 2}, {9, SHIFT, 3}, {10, SHIFT, 4}, },
00088             {{-1, REDUCE, 7}, },
00089             {{-1, ERROR, 21}, {6, SHIFT, 26}, },
00090             {{-1, ERROR, 22}, {6, SHIFT, 27}, },
00091             {{-1, ERROR, 23}, {4, SHIFT, 28}, },
00092             {{-1, REDUCE, 20}, },
00093             {{-1, REDUCE, 11}, {8, SHIFT, 2}, {9, SHIFT, 3}, {10, SHIFT, 4}, },
00094             {{-1, REDUCE, 13}, {2, SHIFT, 1}, },
00095             {{-1, REDUCE, 15}, {2, SHIFT, 1}, },
00096             {{-1, ERROR, 28}, {11, SHIFT, 31}, },
00097             {{-1, REDUCE, 14}, {2, SHIFT, 1}, },
00098             {{-1, REDUCE, 16}, {2, SHIFT, 1}, },
00099             {{-1, ERROR, 31}, {5, SHIFT, 32}, },
00100             {{-1, ERROR, 32}, {6, SHIFT, 33}, },
00101             {{-1, REDUCE, 17}, {2, SHIFT, 1}, },
00102             {{-1, REDUCE, 18}, {2, SHIFT, 1}, },
00103         };*/
00104     private static int[][][] gotoTable;
00105 /*      {
00106             {{-1, 6}, },
00107             {{-1, 7}, {10, 18}, {16, 18}, {29, 18}, {30, 18}, {34, 18}, },
00108             {{-1, 8}, {11, 20}, {17, 20}, {19, 20}, {25, 20}, },
00109             {{-1, 9}, },
00110             {{-1, 10}, {9, 16}, {26, 29}, {27, 30}, {33, 34}, },
00111             {{-1, 11}, {9, 17}, {10, 19}, {16, 25}, },
00112         };*/
00113     private static String[] errorMessages;
00114 /*      {
00115             "TEndOfLineComment TPre TPost TLocation TId EOF expected.",
00116             "TEndOfLineComment TPre TPost TLocation EOF expected.",
00117             "TId expected.",
00118             "TLBracket expected.",
00119             "TEndOfLineComment TDot TPre TPost TLocation EOF expected.",
00120             "EOF expected.",
00121             "TPre TPost TLocation EOF expected.",
00122             "TColon expected.",
00123             "TSemicolon expected.",
00124             "TRBracket expected.",
00125         };*/
00126     private static int[] errors;
00127 /*      {
00128             0, 1, 2, 2, 3, 4, 5, 1, 6, 4, 1, 6, 7, 7, 2, 2, 1, 6, 1, 6, 6, 8, 8, 9, 4, 6, 1, 1, 2, 1, 1, 7, 8, 1, 1, 
00129         };*/
00130     public Parser(Lexer lexer)
00131     {
00132         this.lexer = lexer;
00133 
00134         if(actionTable == null)
00135         {
00136             try
00137             {
00138                 DataInputStream s = new DataInputStream(
00139                     new BufferedInputStream(
00140                     Parser.class.getResourceAsStream("parser.dat")));
00141 
00142                 // read actionTable
00143                 int length = s.readInt();
00144                 actionTable = new int[length][][];
00145                 for(int i = 0; i < actionTable.length; i++)
00146                 {
00147                     length = s.readInt();
00148                     actionTable[i] = new int[length][3];
00149                     for(int j = 0; j < actionTable[i].length; j++)
00150                     {
00151                         for(int k = 0; k < 3; k++)
00152                         {
00153                             actionTable[i][j][k] = s.readInt();
00154                         }
00155                     }
00156                 }
00157 
00158                 // read gotoTable
00159                 length = s.readInt();
00160                 gotoTable = new int[length][][];
00161                 for(int i = 0; i < gotoTable.length; i++)
00162                 {
00163                     length = s.readInt();
00164                     gotoTable[i] = new int[length][2];
00165                     for(int j = 0; j < gotoTable[i].length; j++)
00166                     {
00167                         for(int k = 0; k < 2; k++)
00168                         {
00169                             gotoTable[i][j][k] = s.readInt();
00170                         }
00171                     }
00172                 }
00173 
00174                 // read errorMessages
00175                 length = s.readInt();
00176                 errorMessages = new String[length];
00177                 for(int i = 0; i < errorMessages.length; i++)
00178                 {
00179                     length = s.readInt();
00180                     StringBuffer buffer = new StringBuffer();
00181 
00182                     for(int j = 0; j < length; j++)
00183                     {
00184                         buffer.append(s.readChar());
00185                     }
00186                     errorMessages[i] = buffer.toString();
00187                 }
00188 
00189                 // read errors
00190                 length = s.readInt();
00191                 errors = new int[length];
00192                 for(int i = 0; i < errors.length; i++)
00193                 {
00194                     errors[i] = s.readInt();
00195                 }
00196 
00197                 s.close();
00198             }
00199             catch(Exception e)
00200             {
00201                 throw new RuntimeException("Unable to read parser.dat.");
00202             }
00203         }
00204     }
00205     protected void filter() throws ParserException, LexerException, IOException
00206     {
00207     }
00208     private int goTo(int index)
00209     {
00210         int state = state();
00211         int low = 1;
00212         int high = gotoTable[index].length - 1;
00213         int value = gotoTable[index][0][1];
00214 
00215         while(low <= high)
00216         {
00217             int middle = (low + high) / 2;
00218 
00219             if(state < gotoTable[index][middle][0])
00220             {
00221                 high = middle - 1;
00222             }
00223             else if(state > gotoTable[index][middle][0])
00224             {
00225                 low = middle + 1;
00226             }
00227             else
00228             {
00229                 value = gotoTable[index][middle][1];
00230                 break;
00231             }
00232         }
00233 
00234         return value;
00235     }
00236     private int index(Switchable token)
00237     {
00238         converter.index = -1;
00239         token.apply(converter);
00240         return converter.index;
00241     }
00242     Node new0()
00243     {
00244         XPAssertion node3 = null;
00245         XPComment node2 = null;
00246         PName node1 = null;
00247         AUnit node = new AUnit(node1, node2, node3);
00248         return node;
00249     }
00250     Node new1()
00251     {
00252         XPAssertion node3 = null;
00253         XPComment node2 = null;
00254         PName node1 = (PName) pop();
00255         AUnit node = new AUnit(node1, node2, node3);
00256         return node;
00257     }
00258     Node new10()
00259     {
00260         XPAssertion node3 = (XPAssertion) pop();
00261         XPComment node2 = (XPComment) pop();
00262         PName node1 = null;
00263         AUnit node = new AUnit(node1, node2, node3);
00264         return node;
00265     }
00266     Node new11()
00267     {
00268         XPAssertion node3 = (XPAssertion) pop();
00269         XPComment node2 = (XPComment) pop();
00270         PName node1 = (PName) pop();
00271         AUnit node = new AUnit(node1, node2, node3);
00272         return node;
00273     }
00274     Node new12()
00275     {
00276         TEndOfLineComment node1 = (TEndOfLineComment) pop();
00277         AEndOfLineComment node = new AEndOfLineComment(node1);
00278         return node;
00279     }
00280     Node new13()
00281     {
00282         XPComment node5 = null;
00283         TSemicolon node4 = (TSemicolon) pop();
00284         TColon node3 = (TColon) pop();
00285         TId node2 = (TId) pop();
00286         TPre node1 = (TPre) pop();
00287         APreAssertion node = new APreAssertion(node1, node2, node3, node4, node5);
00288         return node;
00289     }
00290     Node new14()
00291     {
00292         XPComment node5 = (XPComment) pop();
00293         TSemicolon node4 = (TSemicolon) pop();
00294         TColon node3 = (TColon) pop();
00295         TId node2 = (TId) pop();
00296         TPre node1 = (TPre) pop();
00297         APreAssertion node = new APreAssertion(node1, node2, node3, node4, node5);
00298         return node;
00299     }
00300     Node new15()
00301     {
00302         XPComment node5 = null;
00303         TSemicolon node4 = (TSemicolon) pop();
00304         TColon node3 = (TColon) pop();
00305         TId node2 = (TId) pop();
00306         TPost node1 = (TPost) pop();
00307         APostAssertion node = new APostAssertion(node1, node2, node3, node4, node5);
00308         return node;
00309     }
00310     Node new16()
00311     {
00312         XPComment node5 = (XPComment) pop();
00313         TSemicolon node4 = (TSemicolon) pop();
00314         TColon node3 = (TColon) pop();
00315         TId node2 = (TId) pop();
00316         TPost node1 = (TPost) pop();
00317         APostAssertion node = new APostAssertion(node1, node2, node3, node4, node5);
00318         return node;
00319     }
00320     Node new17()
00321     {
00322         XPComment node8 = null;
00323         TSemicolon node7 = (TSemicolon) pop();
00324         TColon node6 = (TColon) pop();
00325         TId node5 = (TId) pop();
00326         TRBracket node4 = (TRBracket) pop();
00327         TId node3 = (TId) pop();
00328         TLBracket node2 = (TLBracket) pop();
00329         TLocation node1 = (TLocation) pop();
00330         ALocationAssertion node = new ALocationAssertion(node1, node2, node3, node4, node5, node6, node7, node8);
00331         return node;
00332     }
00333     Node new18()
00334     {
00335         XPComment node8 = (XPComment) pop();
00336         TSemicolon node7 = (TSemicolon) pop();
00337         TColon node6 = (TColon) pop();
00338         TId node5 = (TId) pop();
00339         TRBracket node4 = (TRBracket) pop();
00340         TId node3 = (TId) pop();
00341         TLBracket node2 = (TLBracket) pop();
00342         TLocation node1 = (TLocation) pop();
00343         ALocationAssertion node = new ALocationAssertion(node1, node2, node3, node4, node5, node6, node7, node8);
00344         return node;
00345     }
00346     Node new19()
00347     {
00348         TId node1 = (TId) pop();
00349         ASimpleName node = new ASimpleName(node1);
00350         return node;
00351     }
00352     Node new2()
00353     {
00354         XPAssertion node3 = null;
00355         XPComment node2 = (XPComment) pop();
00356         PName node1 = null;
00357         AUnit node = new AUnit(node1, node2, node3);
00358         return node;
00359     }
00360     Node new20()
00361     {
00362         TId node3 = (TId) pop();
00363         TDot node2 = (TDot) pop();
00364         PName node1 = (PName) pop();
00365         AQualifiedName node = new AQualifiedName(node1, node2, node3);
00366         return node;
00367     }
00368     Node new3()
00369     {
00370         PComment node2 = (PComment) pop();
00371         XPComment node1 = (XPComment) pop();
00372         X1PComment node = new X1PComment(node1, node2);
00373         return node;
00374     }
00375     Node new4()
00376     {
00377         PComment node1 = (PComment) pop();
00378         X2PComment node = new X2PComment(node1);
00379         return node;
00380     }
00381     Node new5()
00382     {
00383         XPAssertion node3 = null;
00384         XPComment node2 = (XPComment) pop();
00385         PName node1 = (PName) pop();
00386         AUnit node = new AUnit(node1, node2, node3);
00387         return node;
00388     }
00389     Node new6()
00390     {
00391         XPAssertion node3 = (XPAssertion) pop();
00392         XPComment node2 = null;
00393         PName node1 = null;
00394         AUnit node = new AUnit(node1, node2, node3);
00395         return node;
00396     }
00397     Node new7()
00398     {
00399         PAssertion node2 = (PAssertion) pop();
00400         XPAssertion node1 = (XPAssertion) pop();
00401         X1PAssertion node = new X1PAssertion(node1, node2);
00402         return node;
00403     }
00404     Node new8()
00405     {
00406         PAssertion node1 = (PAssertion) pop();
00407         X2PAssertion node = new X2PAssertion(node1);
00408         return node;
00409     }
00410     Node new9()
00411     {
00412         XPAssertion node3 = (XPAssertion) pop();
00413         XPComment node2 = null;
00414         PName node1 = (PName) pop();
00415         AUnit node = new AUnit(node1, node2, node3);
00416         return node;
00417     }
00418     public Start parse() throws ParserException, LexerException, IOException
00419     {
00420         push(0, null, false);
00421 
00422         List ign = null;
00423         while(true)
00424         {
00425             while(index(lexer.peek()) == -1)
00426             {
00427                 if(ign == null)
00428                 {
00429                     ign = new TypedLinkedList(NodeCast.instance);
00430                 }
00431 
00432                 ign.add(lexer.next());
00433             }
00434 
00435             if(ign != null)
00436             {
00437                 ignoredTokens.setIn(lexer.peek(), ign);
00438                 ign = null;
00439             }
00440 
00441             last_pos = lexer.peek().getPos();
00442             last_line = lexer.peek().getLine();
00443 
00444             int index = index(lexer.peek());
00445             action[0] = actionTable[state()][0][1];
00446             action[1] = actionTable[state()][0][2];
00447 
00448             int low = 1;
00449             int high = actionTable[state()].length - 1;
00450 
00451             while(low <= high)
00452             {
00453                 int middle = (low + high) / 2;
00454 
00455                 if(index < actionTable[state()][middle][0])
00456                 {
00457                     high = middle - 1;
00458                 }
00459                 else if(index > actionTable[state()][middle][0])
00460                 {
00461                     low = middle + 1;
00462                 }
00463                 else
00464                 {
00465                     action[0] = actionTable[state()][middle][1];
00466                     action[1] = actionTable[state()][middle][2];
00467                     break;
00468                 }
00469             }
00470 
00471             switch(action[0])
00472             {
00473                 case SHIFT:
00474                     push(action[1], lexer.next(), true);
00475                     last_shift = action[1];
00476                     break;
00477                 case REDUCE:
00478                     switch(action[1])
00479                     {
00480                     case 0: { Node node = new0(); push(goTo(0), node, true); } break;
00481                     case 1: { Node node = new1(); push(goTo(0), node, true); } break;
00482                     case 2: { Node node = new2(); push(goTo(0), node, true); } break;
00483                     case 3: { Node node = new3(); push(goTo(4), node, false); } break;
00484                     case 4: { Node node = new4(); push(goTo(4), node, false); } break;
00485                     case 5: { Node node = new5(); push(goTo(0), node, true); } break;
00486                     case 6: { Node node = new6(); push(goTo(0), node, true); } break;
00487                     case 7: { Node node = new7(); push(goTo(5), node, false); } break;
00488                     case 8: { Node node = new8(); push(goTo(5), node, false); } break;
00489                     case 9: { Node node = new9(); push(goTo(0), node, true); } break;
00490                     case 10: { Node node = new10(); push(goTo(0), node, true); } break;
00491                     case 11: { Node node = new11(); push(goTo(0), node, true); } break;
00492                     case 12: { Node node = new12(); push(goTo(1), node, true); } break;
00493                     case 13: { Node node = new13(); push(goTo(2), node, true); } break;
00494                     case 14: { Node node = new14(); push(goTo(2), node, true); } break;
00495                     case 15: { Node node = new15(); push(goTo(2), node, true); } break;
00496                     case 16: { Node node = new16(); push(goTo(2), node, true); } break;
00497                     case 17: { Node node = new17(); push(goTo(2), node, true); } break;
00498                     case 18: { Node node = new18(); push(goTo(2), node, true); } break;
00499                     case 19: { Node node = new19(); push(goTo(3), node, true); } break;
00500                     case 20: { Node node = new20(); push(goTo(3), node, true); } break;
00501                     }
00502                     break;
00503                 case ACCEPT:
00504                     {
00505                         EOF node2 = (EOF) lexer.next();
00506                         PUnit node1 = (PUnit) pop();
00507                         Start node = new Start(node1, node2);
00508                         return node;
00509                     }
00510                 case ERROR:
00511                     throw new ParserException(
00512                         "[" + last_line + "," + last_pos + "] " +
00513                         errorMessages[errors[action[1]]]);
00514             }
00515         }
00516     }
00517     private Node pop()
00518     {
00519         return (Node) ((State) stack.previous()).node;
00520     }
00521     private void push(int state, Node node, boolean filter) throws ParserException, LexerException, IOException
00522     {
00523         this.node = node;
00524 
00525         if(filter)
00526         {
00527             filter();
00528         }
00529 
00530         if(!stack.hasNext())
00531         {
00532             stack.add(new State(state, this.node));
00533             return;
00534         }
00535 
00536         State s = (State) stack.next();
00537         s.state = state;
00538         s.node = this.node;
00539     }
00540     private int state()
00541     {
00542         State s = (State) stack.previous();
00543         stack.next();
00544         return s.state;
00545     }
00546 }

Generated at Thu Feb 7 06:51:31 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001