/* Generated By:JavaCC: Do not edit this line. ParserCode.java */ package kc; import java.util.*; import java.io.*; /** * Macro statements are as follows : *
::= "main" "(" ")" "{" { | } "}" EOF * ::= "int" { "," } ";" * ::= NAME * ::= | | | | "{" { } "}" * ::= "if" "(" ")" * ::= "while" "(" ")" * ::= "outputint" "(" ")" ";" * ::= Name "=" ";" * ::= { ( "+" | "-" ) } * ::= { ( "*" | "/" ) } * ::= NAME | INTEGER | "inputint" * * Execute as follows : * $ javac kc/ParserCode.java * $ java kc.ParserCode inputfile */ public class ParserCode implements ParserCodeConstants { static VarTable varTable; // 変数表 static PseudoIseg iseg; // 疑似ISEG public static void main (String[] args) { varTable = new VarTable(); iseg = new PseudoIseg(); try { ParserCode parser = new ParserCode (new FileReader (args[0])); parser.enable_tracing(); // デバグ情報収集 parser.Main(); } catch (Exception err_mes) { // 構文エラーがあった場合 System.out.println (err_mes); // エラーメッセージを出力 } iseg.dump2file(); // アセンブラコード出力 System.out.println ("finished"); } /** *
::= "main" "(" "{" { } { } "}" */ final public void Main() throws ParseException { jj_consume_token(MAIN); jj_consume_token(LPAREN); jj_consume_token(RPAREN); jj_consume_token(LBRACE); label_1: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case INT: ; break; default: jj_la1[0] = jj_gen; break label_1; } Decl(); } label_2: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case LBRACE: case IF: case WHILE: case OUTPUTINT: case NAME: ; break; default: jj_la1[1] = jj_gen; break label_2; } State(); } jj_consume_token(RBRACE); jj_consume_token(0); iseg.appendCode (Operator.HALT); } /** * ::= "int" { "," } */ final public void Decl() throws ParseException { jj_consume_token(INT); Name(); label_3: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case COMMA: ; break; default: jj_la1[2] = jj_gen; break label_3; } jj_consume_token(COMMA); Name(); } jj_consume_token(SEMICOLON); } /** * ::= NAME */ final public void Name() throws ParseException { String name; token = jj_consume_token(NAME); name = token.image; varTable.registerNewVariable (Type.INT, name, 1); } /** * ::= { | | | | "{" { } "} } */ final public void State() throws ParseException { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case IF: If(); break; case WHILE: While(); break; case OUTPUTINT: Output(); break; case NAME: Assgn(); break; case LBRACE: jj_consume_token(LBRACE); label_4: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case LBRACE: case IF: case WHILE: case OUTPUTINT: case NAME: ; break; default: jj_la1[3] = jj_gen; break label_4; } State(); } jj_consume_token(RBRACE); break; default: jj_la1[4] = jj_gen; jj_consume_token(-1); throw new ParseException(); } } /** * ::= "if" "(" ")" */ final public void If() throws ParseException { int beqAddr, nextAddr; jj_consume_token(IF); jj_consume_token(LPAREN); Exp(); beqAddr = iseg.appendCode (Operator.BEQ); jj_consume_token(RPAREN); State(); nextAddr = iseg.getLastCodeAddress() + 1; iseg.replaceCode (beqAddr, nextAddr); } /** * ::= "while" "(" ")" */ final public void While() throws ParseException { int expAddr, beqAddr, nextAddr; jj_consume_token(WHILE); jj_consume_token(LPAREN); expAddr = iseg.getLastCodeAddress() + 1; Exp(); beqAddr = iseg.appendCode (Operator.BEQ); jj_consume_token(RPAREN); State(); nextAddr = iseg.appendCode (Operator.JUMP, expAddr) + 1; iseg.replaceCode (beqAddr, nextAddr); } /** * ::= "outputint" "(" ")" ; */ final public void Output() throws ParseException { jj_consume_token(OUTPUTINT); jj_consume_token(LPAREN); Exp(); iseg.appendCode (Operator.OUTPUT); jj_consume_token(RPAREN); jj_consume_token(SEMICOLON); } /** * ::= NAME "=" ";" */ final public void Assgn() throws ParseException { String name; int addr; token = jj_consume_token(NAME); name = token.image; addr = varTable.getAddress(name); iseg.appendCode (Operator.PUSHI, addr); jj_consume_token(ASSGN); Exp(); iseg.appendCode (Operator.ASSGN); jj_consume_token(SEMICOLON); iseg.appendCode (Operator.REMOVE); } /** * ::= { ( "+" | "-" ) } */ final public void Exp() throws ParseException { Operator opcode; Term(); label_5: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case ADD: case SUB: ; break; default: jj_la1[5] = jj_gen; break label_5; } switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case ADD: jj_consume_token(ADD); opcode = Operator.ADD; break; case SUB: jj_consume_token(SUB); opcode = Operator.SUB; break; default: jj_la1[6] = jj_gen; jj_consume_token(-1); throw new ParseException(); } Term(); iseg.appendCode (opcode); } } /** * ::= { ( "*" | "/" ) } */ final public void Term() throws ParseException { Operator opcode; Factor(); label_6: while (true) { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case MUL: case DIV: ; break; default: jj_la1[7] = jj_gen; break label_6; } switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case MUL: jj_consume_token(MUL); opcode = Operator.MUL; break; case DIV: jj_consume_token(DIV); opcode = Operator.DIV; break; default: jj_la1[8] = jj_gen; jj_consume_token(-1); throw new ParseException(); } Factor(); iseg.appendCode (opcode); } } /** * ::= NAME | INTEGER | "inputint" */ final public void Factor() throws ParseException { int val, addr; String name; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case NAME: token = jj_consume_token(NAME); name = token.image; addr = varTable.getAddress (name); iseg.appendCode (Operator.PUSH, addr); break; case INTEGER: token = jj_consume_token(INTEGER); val = Integer.parseInt (token.image); iseg.appendCode (Operator.PUSHI, val); break; case INPUTINT: jj_consume_token(INPUTINT); iseg.appendCode (Operator.INPUT); break; default: jj_la1[9] = jj_gen; jj_consume_token(-1); throw new ParseException(); } } /** Generated Token Manager. */ public ParserCodeTokenManager token_source; SimpleCharStream jj_input_stream; /** Current token. */ public Token token; /** Next token. */ public Token jj_nt; private int jj_ntk; private int jj_gen; final private int[] jj_la1 = new int[10]; static private int[] jj_la1_0; static { jj_la1_init_0(); } private static void jj_la1_init_0() { jj_la1_0 = new int[] {0x100000,0x14c0200,0x800,0x14c0200,0x14c0200,0x6000,0x6000,0x18000,0x18000,0x1820000,}; } /** Constructor with InputStream. */ public ParserCode(java.io.InputStream stream) { this(stream, null); } /** Constructor with InputStream and supplied encoding */ public ParserCode(java.io.InputStream stream, String encoding) { try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } token_source = new ParserCodeTokenManager(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 10; i++) jj_la1[i] = -1; } /** Reinitialise. */ public void ReInit(java.io.InputStream stream) { ReInit(stream, null); } /** Reinitialise. */ public void ReInit(java.io.InputStream stream, String encoding) { try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } token_source.ReInit(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 10; i++) jj_la1[i] = -1; } /** Constructor. */ public ParserCode(java.io.Reader stream) { jj_input_stream = new SimpleCharStream(stream, 1, 1); token_source = new ParserCodeTokenManager(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 10; i++) jj_la1[i] = -1; } /** Reinitialise. */ public void ReInit(java.io.Reader stream) { jj_input_stream.ReInit(stream, 1, 1); token_source.ReInit(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 10; i++) jj_la1[i] = -1; } /** Constructor with generated Token Manager. */ public ParserCode(ParserCodeTokenManager tm) { token_source = tm; token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 10; i++) jj_la1[i] = -1; } /** Reinitialise. */ public void ReInit(ParserCodeTokenManager tm) { token_source = tm; token = new Token(); jj_ntk = -1; jj_gen = 0; for (int i = 0; i < 10; i++) jj_la1[i] = -1; } private Token jj_consume_token(int kind) throws ParseException { Token oldToken; if ((oldToken = token).next != null) token = token.next; else token = token.next = token_source.getNextToken(); jj_ntk = -1; if (token.kind == kind) { jj_gen++; return token; } token = oldToken; jj_kind = kind; throw generateParseException(); } /** Get the next Token. */ final public Token getNextToken() { if (token.next != null) token = token.next; else token = token.next = token_source.getNextToken(); jj_ntk = -1; jj_gen++; return token; } /** Get the specific Token. */ final public Token getToken(int index) { Token t = token; for (int i = 0; i < index; i++) { if (t.next != null) t = t.next; else t = t.next = token_source.getNextToken(); } return t; } private int jj_ntk() { if ((jj_nt=token.next) == null) return (jj_ntk = (token.next=token_source.getNextToken()).kind); else return (jj_ntk = jj_nt.kind); } private java.util.List jj_expentries = new java.util.ArrayList(); private int[] jj_expentry; private int jj_kind = -1; /** Generate ParseException. */ public ParseException generateParseException() { jj_expentries.clear(); boolean[] la1tokens = new boolean[25]; if (jj_kind >= 0) { la1tokens[jj_kind] = true; jj_kind = -1; } for (int i = 0; i < 10; i++) { if (jj_la1[i] == jj_gen) { for (int j = 0; j < 32; j++) { if ((jj_la1_0[i] & (1<