//---------------------------------------------------- // The following code was generated by CUP v0.10k // Fri Mar 16 08:36:05 NZDT 2007 //---------------------------------------------------- package grammar; import java.util.*; import java.io.*; import java_cup.runtime.*; /** CUP v0.10k generated parser. * @version Fri Mar 16 08:36:05 NZDT 2007 */ 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\002\000\002\003" + "\004\000\002\004\006\000\002\004\004\000\002\004\004" + "\000\002\004\003\000\002\005\005\000\002\005\005\000" + "\002\005\004\000\002\005\003\000\002\006\005\000\002" + "\006\005\000\002\006\003\000\002\007\005\000\002\007" + "\003\000\002\007\003" }); /** Access to production table. */ public short[][] production_table() {return _production_table;} /** Parse-action table. */ protected static final short[][] _action_table = unpackFromStrings(new String[] { "\000\036\000\020\002\000\003\000\004\000\006\000\010" + "\000\014\000\015\000\001\002\000\020\002\012\003\015" + "\004\010\006\017\010\005\014\011\015\006\001\002\000" + "\010\004\010\014\011\015\024\001\002\000\016\006\ufff1" + "\007\ufff1\010\ufff1\011\ufff1\012\ufff1\013\035\001\002\000" + "\010\006\034\007\030\010\031\001\002\000\012\004\010" + "\010\005\014\011\015\024\001\002\000\016\005\ufff2\006" + "\ufff2\007\ufff2\010\ufff2\011\ufff2\012\ufff2\001\002\000\004" + "\002\001\001\002\000\020\002\uffff\003\uffff\004\uffff\006" + "\uffff\010\uffff\014\uffff\015\uffff\001\002\000\016\005\ufff7" + "\006\ufff7\007\ufff7\010\ufff7\011\022\012\021\001\002\000" + "\004\006\020\001\002\000\016\005\ufff4\006\ufff4\007\ufff4" + "\010\ufff4\011\ufff4\012\ufff4\001\002\000\020\002\ufffb\003" + "\ufffb\004\ufffb\006\ufffb\010\ufffb\014\ufffb\015\ufffb\001\002" + "\000\020\002\ufffc\003\ufffc\004\ufffc\006\ufffc\010\ufffc\014" + "\ufffc\015\ufffc\001\002\000\010\004\010\014\011\015\024" + "\001\002\000\010\004\010\014\011\015\024\001\002\000" + "\016\005\ufff6\006\ufff6\007\ufff6\010\ufff6\011\ufff6\012\ufff6" + "\001\002\000\016\005\ufff1\006\ufff1\007\ufff1\010\ufff1\011" + "\ufff1\012\ufff1\001\002\000\016\005\ufff5\006\ufff5\007\ufff5" + "\010\ufff5\011\ufff5\012\ufff5\001\002\000\010\005\027\007" + "\030\010\031\001\002\000\016\005\ufff3\006\ufff3\007\ufff3" + "\010\ufff3\011\ufff3\012\ufff3\001\002\000\010\004\010\014" + "\011\015\024\001\002\000\010\004\010\014\011\015\024" + "\001\002\000\016\005\ufff9\006\ufff9\007\ufff9\010\ufff9\011" + "\022\012\021\001\002\000\016\005\ufffa\006\ufffa\007\ufffa" + "\010\ufffa\011\022\012\021\001\002\000\020\002\ufffd\003" + "\ufffd\004\ufffd\006\ufffd\010\ufffd\014\ufffd\015\ufffd\001\002" + "\000\012\004\010\010\005\014\011\015\024\001\002\000" + "\010\006\037\007\030\010\031\001\002\000\020\002\ufffe" + "\003\ufffe\004\ufffe\006\ufffe\010\ufffe\014\ufffe\015\ufffe\001" + "\002\000\016\005\ufff8\006\ufff8\007\ufff8\010\ufff8\011\022" + "\012\021\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\036\000\004\003\003\001\001\000\012\004\012\005" + "\006\006\013\007\015\001\001\000\006\006\037\007\015" + "\001\001\000\002\001\001\000\002\001\001\000\010\005" + "\025\006\013\007\015\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\007\024\001\001\000\004\007\022\001\001\000" + "\002\001\001\000\002\001\001\000\002\001\001\000\002" + "\001\001\000\002\001\001\000\006\006\032\007\015\001" + "\001\000\006\006\031\007\015\001\001\000\002\001\001" + "\000\002\001\001\000\002\001\001\000\010\005\035\006" + "\013\007\015\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 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 ) { System.err.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++ ) System.err.print( text[ i ] ); System.err.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 { Hashtable< String, Integer > table = new Hashtable< String, Integer >(); 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: // Factor ::= IDENT { Integer 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; Integer value = ( Integer ) table.get( ident ); if ( value == null ) { parser.report_error( "Undeclared Identifier " + ident, new Symbol( sym.IDENT, identleft, identright, ident ) ); value = new Integer( 0 ); } RESULT = value; CUP$parser$result = new java_cup.runtime.Symbol(5/*Factor*/, ((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 15: // Factor ::= NUMBER { Integer RESULT = null; int valueleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int valueright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; String value = (String)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = new Integer( value ); CUP$parser$result = new java_cup.runtime.Symbol(5/*Factor*/, ((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: // Factor ::= LEFT Expr RIGHT { Integer RESULT = null; int exprleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).left; int exprright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).right; Integer expr = (Integer)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-1)).value; RESULT = expr; CUP$parser$result = new java_cup.runtime.Symbol(5/*Factor*/, ((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 13: // Term ::= Factor { Integer RESULT = null; int factorleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int factorright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; Integer factor = (Integer)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = factor; CUP$parser$result = new java_cup.runtime.Symbol(4/*Term*/, ((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 12: // Term ::= Term DIVIDE Factor { Integer RESULT = null; int termleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).left; int termright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).right; Integer term = (Integer)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-2)).value; int factorleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int factorright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; Integer factor = (Integer)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = new Integer( term.intValue() / factor.intValue() ); CUP$parser$result = new java_cup.runtime.Symbol(4/*Term*/, ((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: // Term ::= Term TIMES Factor { Integer RESULT = null; int termleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).left; int termright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-2)).right; Integer term = (Integer)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-2)).value; int factorleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int factorright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; Integer factor = (Integer)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = new Integer( term.intValue() * factor.intValue() ); CUP$parser$result = new java_cup.runtime.Symbol(4/*Term*/, ((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 10: // Expr ::= Term { Integer RESULT = null; int termleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int termright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; Integer term = (Integer)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = term; CUP$parser$result = new java_cup.runtime.Symbol(3/*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 9: // Expr ::= MINUS Term { Integer RESULT = null; int termleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int termright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; Integer term = (Integer)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = new Integer( - term.intValue() ); CUP$parser$result = new java_cup.runtime.Symbol(3/*Expr*/, ((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 8: // Expr ::= Expr MINUS Term { Integer 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; Integer expr = (Integer)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-2)).value; int termleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int termright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; Integer term = (Integer)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = new Integer( expr.intValue() - term.intValue() ); CUP$parser$result = new java_cup.runtime.Symbol(3/*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 7: // Expr ::= Expr PLUS Term { Integer 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; Integer expr = (Integer)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-2)).value; int termleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left; int termright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right; Integer term = (Integer)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value; RESULT = new Integer( expr.intValue() + term.intValue() ); CUP$parser$result = new java_cup.runtime.Symbol(3/*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 6: // Stmt ::= NEWLINE { Object RESULT = null; CUP$parser$result = new java_cup.runtime.Symbol(2/*Stmt*/, ((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: // Stmt ::= error NEWLINE { Object RESULT = null; CUP$parser$result = new java_cup.runtime.Symbol(2/*Stmt*/, ((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: // Stmt ::= Expr NEWLINE { Object RESULT = null; int exprleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).left; int exprright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).right; Integer expr = (Integer)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-1)).value; System.out.println( expr.intValue() ); CUP$parser$result = new java_cup.runtime.Symbol(2/*Stmt*/, ((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 3: // Stmt ::= IDENT ASSIGN Expr NEWLINE { Object RESULT = null; int identleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-3)).left; int identright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-3)).right; String ident = (String)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-3)).value; int exprleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).left; int exprright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).right; Integer expr = (Integer)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-1)).value; table.put( ident, expr ); CUP$parser$result = new java_cup.runtime.Symbol(2/*Stmt*/, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-3)).left, ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right, RESULT); } return CUP$parser$result; /*. . . . . . . . . . . . . . . . . . . .*/ case 2: // StmtList ::= StmtList Stmt { Object RESULT = null; CUP$parser$result = new java_cup.runtime.Symbol(1/*StmtList*/, ((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 1: // StmtList ::= { Object RESULT = null; CUP$parser$result = new java_cup.runtime.Symbol(1/*StmtList*/, ((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 0: // $START ::= StmtList 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; Object start_val = (Object)((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"); } } }