//---------------------------------------------------- // The following code was generated by CUP v0.10k // Wed May 03 15:16:06 NZST 2006 //---------------------------------------------------- package grammar; import node.*; import node.declNode.*; import node.typeNode.*; import node.declrNode.*; import node.exprNode.*; import text.*; import java.io.*; import java_cup.runtime.*; /** CUP v0.10k generated parser. * @version Wed May 03 15:16:06 NZST 2006 */ public class parser extends java_cup.runtime.lr_parser { /** Default constructor. */ public parser() {super();} /** Constructor which sets the default scanner. */ public parser(java_cup.runtime.Scanner s) {super(s);} /** Production table. */ protected static final short _production_table[][] = unpackFromStrings(new String[] { "\000\021\000\002\002\004\000\002\003\003\000\002\004" + "\002\000\002\004\004\000\002\005\005\000\002\005\004" + "\000\002\006\003\000\002\007\003\000\002\007\005\000" + "\002\010\005\000\002\010\003\000\002\010\003\000\002" + "\011\005\000\002\011\005\000\002\011\003\000\002\012" + "\003\000\002\012\005" }); /** Access to production table. */ public short[][] production_table() {return _production_table;} /** Parse-action table. */ protected static final short[][] _action_table = unpackFromStrings(new String[] { "\000\033\000\010\002\uffff\003\uffff\011\uffff\001\002\000" + "\004\002\035\001\002\000\010\002\000\003\007\011\010" + "\001\002\000\006\003\013\011\014\001\002\000\004\007" + "\012\001\002\000\006\003\ufffb\011\ufffb\001\002\000\010" + "\002\ufffe\003\ufffe\011\ufffe\001\002\000\010\002\ufffc\003" + "\ufffc\011\ufffc\001\002\000\006\007\ufff6\010\ufff6\001\002" + "\000\010\006\022\007\ufff7\010\ufff7\001\002\000\006\007" + "\ufffa\010\ufffa\001\002\000\006\007\020\010\017\001\002" + "\000\006\003\013\011\014\001\002\000\010\002\ufffd\003" + "\ufffd\011\ufffd\001\002\000\006\007\ufff9\010\ufff9\001\002" + "\000\006\004\023\011\025\001\002\000\010\003\027\004" + "\023\011\025\001\002\000\006\007\ufff8\010\ufff8\001\002" + "\000\010\005\ufff3\007\ufff3\010\ufff3\001\002\000\004\005" + "\034\001\002\000\004\005\033\001\002\000\006\005\ufff2" + "\010\031\001\002\000\006\004\023\011\025\001\002\000" + "\004\005\ufff1\001\002\000\010\005\ufff4\007\ufff4\010\ufff4" + "\001\002\000\010\005\ufff5\007\ufff5\010\ufff5\001\002\000" + "\004\002\001\001\002" }); /** Access to parse-action table. */ public short[][] action_table() {return _action_table;} /** reduce_goto table. */ protected static final short[][] _reduce_table = unpackFromStrings(new String[] { "\000\033\000\006\003\003\004\004\001\001\000\002\001" + "\001\000\006\005\010\006\005\001\001\000\006\007\015" + "\010\014\001\001\000\002\001\001\000\002\001\001\000" + "\002\001\001\000\002\001\001\000\002\001\001\000\002" + "\001\001\000\002\001\001\000\002\001\001\000\004\010" + "\020\001\001\000\002\001\001\000\002\001\001\000\004" + "\011\023\001\001\000\006\011\027\012\025\001\001\000" + "\002\001\001\000\002\001\001\000\002\001\001\000\002" + "\001\001\000\002\001\001\000\006\011\027\012\031\001" + "\001\000\002\001\001\000\002\001\001\000\002\001\001" + "\000\002\001\001" }); /** Access to reduce_goto table. */ public short[][] reduce_table() {return _reduce_table;} /** Instance of action encapsulation class. */ protected CUP$parser$actions action_obj; /** Action encapsulation object initializer. */ protected void init_actions() { action_obj = new CUP$parser$actions(this); } /** Invoke a user supplied parse action. */ public java_cup.runtime.Symbol do_action( int act_num, java_cup.runtime.lr_parser parser, java.util.Stack stack, int top) throws java.lang.Exception { /* call code in generated class */ return action_obj.CUP$parser$do_action(act_num, parser, stack, top); } /** Indicates start state. */ public int start_state() {return 0;} /** Indicates start production. */ public int start_production() {return 0;} /** EOF Symbol index. */ public int EOF_sym() {return 0;} /** error Symbol index. */ public int error_sym() {return 1;} /** Scan to get the next Symbol. */ public java_cup.runtime.Symbol scan() throws java.lang.Exception { return lexer.yylex(); } private Yylex lexer; private File file; public parser( File file ) { this(); this.file = file; try { lexer = new Yylex( new FileReader( file ) ); } catch ( IOException exception ) { throw new Error( "Unable to open file \"" + file + "\"" ); } } public int error_sync_size() { return 1; } public String terminal_name( int id ) { return sym.terminal_name( id ); } public String non_terminal_name( int id ) { return sym.non_terminal_name( id ); } public String rule_name( int id ) { return sym.rule_name( id ); } public void report_error( String message, Object info ) { Print.error().println( file + " ( " + lexer.lineNumber() + " ): " + message ); try { if ( info instanceof Symbol ) { Symbol symbol = ( Symbol ) info; printText( symbol.left, symbol.right ); } } catch ( IOException e ) { } } private void printText( int left, int right ) throws IOException { Reader sourceReader = new FileReader( file ); int veryLeft = Math.max( left - 50, 0 ), veryRight = Math.min( right + 20, ( int ) file.length() ); char[] text = new char[ veryRight - veryLeft ]; char[] underline = new char[ veryRight - veryLeft ]; sourceReader.skip( veryLeft ); sourceReader.read( text ); for ( int i = 0; i < text.length; i++ ) { if ( text[ i ] < ' ' ) { text[ i ] = '|'; underline[ i ] = '|'; } else underline[ i ] = ' '; if ( left <= veryLeft + i && veryLeft + i < right ) underline[ i ] = '^'; } printLine( text ); printLine( underline ); } private static void printLine( char[] text ) { for ( int i = 0; i < text.length; i++ ) Print.error().print( text[ i ] ); Print.error().println(); } public void syntax_error( Symbol currToken ) { report_error( "Syntax Error", currToken ); } } /** Cup generated class to encapsulate user supplied action code.*/ class CUP$parser$actions { private final parser parser; /** Constructor */ CUP$parser$actions(parser parser) { this.parser = parser; } /** Method with the actual generated action code. */ public final java_cup.runtime.Symbol CUP$parser$do_action( int CUP$parser$act_num, java_cup.runtime.lr_parser CUP$parser$parser, java.util.Stack CUP$parser$stack, int CUP$parser$top) throws java.lang.Exception { /* Symbol object for return from actions */ java_cup.runtime.Symbol CUP$parser$result; /* select the action based on the action number */ switch (CUP$parser$act_num) { /*. . . . . . . . . . . . . . . . . . . .*/ case 16: // ExprList ::= Expr COMMA ExprList { ExprListNode RESULT = null; int exprleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).left; int exprright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).right; ExprNode expr = (ExprNode)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-2)).value; int exprListleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int exprListright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; ExprListNode exprList = (ExprListNode)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = new ExprListNode( expr, exprList ); CUP$parser$result = new java_cup.runtime.Symbol(8/*ExprList*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 15: // ExprList ::= Expr { ExprListNode RESULT = null; int exprleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int exprright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; ExprNode expr = (ExprNode)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = new ExprListNode( expr, null ); CUP$parser$result = new java_cup.runtime.Symbol(8/*ExprList*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 14: // Expr ::= IDENT { ExprNode RESULT = null; int identleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int identright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; String ident = (String)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = new VariableExprNode( ident ); CUP$parser$result = new java_cup.runtime.Symbol(7/*Expr*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 13: // Expr ::= LEFTBRACE error RIGHTBRACE { ExprNode RESULT = null; RESULT = new ErrorExprNode( "{ ExprListError ... }" ); CUP$parser$result = new java_cup.runtime.Symbol(7/*Expr*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 12: // Expr ::= LEFTBRACE ExprList RIGHTBRACE { ExprNode RESULT = null; int exprListleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).left; int exprListright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).right; ExprListNode exprList = (ExprListNode)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-1)).value; RESULT = new CompoundExprNode( exprList ); CUP$parser$result = new java_cup.runtime.Symbol(7/*Expr*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 11: // Declr ::= error { DeclrNode RESULT = null; RESULT = new ErrorDeclrNode( "DeclrError..." ); CUP$parser$result = new java_cup.runtime.Symbol(6/*Declr*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 10: // Declr ::= IDENT { DeclrNode RESULT = null; int identleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int identright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; String ident = (String)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = new UninitDeclrNode( ident ); CUP$parser$result = new java_cup.runtime.Symbol(6/*Declr*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 9: // Declr ::= IDENT ASSIGN Expr { DeclrNode RESULT = null; int identleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).left; int identright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).right; String ident = (String)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-2)).value; int exprleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int exprright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; ExprNode expr = (ExprNode)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = new InitDeclrNode( ident, expr ); CUP$parser$result = new java_cup.runtime.Symbol(6/*Declr*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 8: // DeclrList ::= DeclrList COMMA Declr { DeclrListNode RESULT = null; int declrListleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).left; int declrListright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).right; DeclrListNode declrList = (DeclrListNode)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-2)).value; int declrleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int declrright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; DeclrNode declr = (DeclrNode)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; declrList.addElement( declr ); RESULT = declrList; CUP$parser$result = new java_cup.runtime.Symbol(5/*DeclrList*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 7: // DeclrList ::= Declr { DeclrListNode RESULT = null; int declrleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int declrright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; DeclrNode declr = (DeclrNode)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = new DeclrListNode( declr ); CUP$parser$result = new java_cup.runtime.Symbol(5/*DeclrList*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 6: // Type ::= IDENT { TypeNode RESULT = null; int identleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int identright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; String ident = (String)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = new TypeIdentNode( ident ); CUP$parser$result = new java_cup.runtime.Symbol(4/*Type*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 5: // Decl ::= error SEMICOLON { DeclNode RESULT = null; RESULT = new ErrorDeclNode( "DeclError...;" ); CUP$parser$result = new java_cup.runtime.Symbol(3/*Decl*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 4: // Decl ::= Type DeclrList SEMICOLON { DeclNode RESULT = null; int typeleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).left; int typeright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).right; TypeNode type = (TypeNode)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-2)).value; int declrListleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).left; int declrListright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).right; DeclrListNode declrList = (DeclrListNode)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-1)).value; RESULT = new VariableDeclNode( type, declrList ); CUP$parser$result = new java_cup.runtime.Symbol(3/*Decl*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 3: // DeclList ::= DeclList Decl { DeclListNode RESULT = null; int declListleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).left; int declListright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).right; DeclListNode declList = (DeclListNode)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-1)).value; int declleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int declright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; DeclNode decl = (DeclNode)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; declList.addElement( decl ); RESULT = declList; CUP$parser$result = new java_cup.runtime.Symbol(2/*DeclList*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 2: // DeclList ::= { DeclListNode RESULT = null; RESULT = new DeclListNode(); CUP$parser$result = new java_cup.runtime.Symbol(2/*DeclList*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 1: // Program ::= DeclList { ProgramNode RESULT = null; int declListleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int declListright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; DeclListNode declList = (DeclListNode)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = new ProgramNode( declList ); CUP$parser$result = new java_cup.runtime.Symbol(1/*Program*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 0: // $START ::= Program EOF { Object RESULT = null; int start_valleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).left; int start_valright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).right; ProgramNode start_val = (ProgramNode)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-1)).value; RESULT = start_val; CUP$parser$result = new java_cup.runtime.Symbol(0/*$START*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } /* ACCEPT */ CUP$parser$parser.done_parsing(); return CUP$parser$result; /* . . . . . .*/ default: throw new Exception( "Invalid action number found in internal parse table"); } } }