⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 emit.java

📁 我开发的一个用java语言实现的编译器,内含词法分析器,语法分析器,而且可以实现中间代码生成.用到了SLR算法和LR(1)算法
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
					     act.kind() + " found in parse table");		}	    }	  /* now we know how big to make the row */	  action_table[i] = new short[nentries + 2];	  System.arraycopy(temp_table, 0, action_table[i], 0, nentries);	  /* finish off the row with a default entry */	  action_table[i][nentries++] = -1;	  if (row.default_reduce != -1)	    action_table[i][nentries++] = (short) (-(row.default_reduce+1));	  else	    action_table[i][nentries++] = 0;	}      /* finish off the init of the table */      out.println();      out.println("  /** Parse-action table. */");      out.println("  protected static final short[][] _action_table = ");       out.print  ("    unpackFromStrings(");      do_table_as_string(out, action_table);      out.println(");");      /* do the public accessor method */      out.println();      out.println("  /** Access to parse-action table. */");      out.println("  public short[][] action_table() {return _action_table;}");      action_table_time = System.currentTimeMillis() - start_time;    }  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/  /** Emit the reduce-goto table.    * @param out     stream to produce output on.   * @param red_tab the internal representation of the reduce-goto table.   */  protected static void do_reduce_table(    PrintWriter out,     parse_reduce_table red_tab)    {      lalr_state       goto_st;      parse_action     act;      long start_time = System.currentTimeMillis();      /* collect values for reduce-goto table */      short[][] reduce_goto_table = new short[red_tab.num_states()][];      /* do each row of the reduce-goto table */      for (int i=0; i<red_tab.num_states(); i++)	{	  /* make temporary table for the row. */	  short[] temp_table = new short[2*red_tab.under_state[i].size()];	  int nentries = 0;	  /* do each entry in the row */	  for (int j=0; j<red_tab.under_state[i].size(); j++)	    {	      /* get the entry */	      goto_st = red_tab.under_state[i].under_non_term[j];	      /* if we have none, skip it */	      if (goto_st != null)		{		  /* make entries for the index and the value */		  temp_table[nentries++] = (short) j;		  temp_table[nentries++] = (short) goto_st.index();		}	    }	  /* now we know how big to make the row. */	  reduce_goto_table[i] = new short[nentries+2];	  System.arraycopy(temp_table, 0, reduce_goto_table[i], 0, nentries);	  /* end row with default value */	  reduce_goto_table[i][nentries++] = -1;	  reduce_goto_table[i][nentries++] = -1;	}      /* emit the table. */      out.println();      out.println("  /** <code>reduce_goto</code> table. */");      out.println("  protected static final short[][] _reduce_table = ");       out.print  ("    unpackFromStrings(");      do_table_as_string(out, reduce_goto_table);      out.println(");");      /* do the public accessor method */      out.println();      out.println("  /** Access to <code>reduce_goto</code> table. */");      out.println("  public short[][] reduce_table() {return _reduce_table;}");      out.println();      goto_table_time = System.currentTimeMillis() - start_time;    }  // print a string array encoding the given short[][] array.  protected static void do_table_as_string(PrintWriter out, short[][] sa) {    out.println("new String[] {");    out.print("    \"");    int nchar=0, nbytes=0;    nbytes+=do_escaped(out, (char)(sa.length>>16));    nchar  =do_newline(out, nchar, nbytes);    nbytes+=do_escaped(out, (char)(sa.length&0xFFFF));    nchar  =do_newline(out, nchar, nbytes);    for (int i=0; i<sa.length; i++) {	nbytes+=do_escaped(out, (char)(sa[i].length>>16));	nchar  =do_newline(out, nchar, nbytes);	nbytes+=do_escaped(out, (char)(sa[i].length&0xFFFF));	nchar  =do_newline(out, nchar, nbytes);	for (int j=0; j<sa[i].length; j++) {	  // contents of string are (value+2) to allow for common -1, 0 cases	  // (UTF-8 encoding is most efficient for 0<c<0x80)	  nbytes+=do_escaped(out, (char)(2+sa[i][j]));	  nchar  =do_newline(out, nchar, nbytes);	}    }    out.print("\" }");  }  // split string if it is very long; start new line occasionally for neatness  protected static int do_newline(PrintWriter out, int nchar, int nbytes) {    if (nbytes > 65500)  { out.println("\", "); out.print("    \""); }    else if (nchar > 11) { out.println("\" +"); out.print("    \""); }    else return nchar+1;    return 0;  }  // output an escape sequence for the given character code.  protected static int do_escaped(PrintWriter out, char c) {    StringBuffer escape = new StringBuffer();    if (c <= 0xFF) {      escape.append(Integer.toOctalString(c));      while(escape.length() < 3) escape.insert(0, '0');    } else {      escape.append(Integer.toHexString(c));      while(escape.length() < 4) escape.insert(0, '0');      escape.insert(0, 'u');    }    escape.insert(0, '\\');    out.print(escape.toString());    // return number of bytes this takes up in UTF-8 encoding.    if (c == 0) return 2;    if (c >= 0x01 && c <= 0x7F) return 1;    if (c >= 0x80 && c <= 0x7FF) return 2;    return 3;  }  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/  /** Emit the parser subclass with embedded tables.    * @param out             stream to produce output on.   * @param action_table    internal representation of the action table.   * @param reduce_table    internal representation of the reduce-goto table.   * @param start_st        start state of the parse machine.   * @param start_prod      start production of the grammar.   * @param compact_reduces do we use most frequent reduce as default?   * @param suppress_scanner should scanner be suppressed for compatibility?   */  public static void parser(    PrintWriter        out,     parse_action_table action_table,    parse_reduce_table reduce_table,    int                start_st,    production         start_prod,    boolean            compact_reduces,    boolean            suppress_scanner)    throws internal_error    {      long start_time = System.currentTimeMillis();      /* top of file */      out.println();      out.println("//----------------------------------------------------");       out.println("// The following code was generated by " + 							version.title_str);      out.println("// " + new Date());      out.println("//----------------------------------------------------");       out.println();      emit_package(out);      /* user supplied imports */      for (int i = 0; i < import_list.size(); i++)	out.println("import " + import_list.elementAt(i) + ";");      /* class header */      out.println();      out.println("/** "+version.title_str+" generated parser.");      out.println("  * @version " + new Date());      out.println("  */");      out.println("public class " + parser_class_name + 		  " extends java_cup.runtime.lr_parser {");      /* constructors [CSA/davidm, 24-jul-99] */      out.println();      out.println("  /** Default constructor. */");      out.println("  public " + parser_class_name + "() {super();}");      if (!suppress_scanner) {	  out.println();	  out.println("  /** Constructor which sets the default scanner. */");	  out.println("  public " + parser_class_name + 		      "(java_cup.runtime.Scanner s) {super(s);}");      }      /* emit the various tables */      emit_production_table(out);      do_action_table(out, action_table, compact_reduces);      do_reduce_table(out, reduce_table);      /* instance of the action encapsulation class */      out.println("  /** Instance of action encapsulation class. */");      out.println("  protected " + pre("actions") + " action_obj;");      out.println();      /* action object initializer */      out.println("  /** Action encapsulation object initializer. */");      out.println("  protected void init_actions()");      out.println("    {");      out.println("      action_obj = new " + pre("actions") + "(this);");      out.println("    }");      out.println();      /* access to action code */      out.println("  /** Invoke a user supplied parse action. */");      out.println("  public java_cup.runtime.Symbol do_action(");      out.println("    int                        act_num,");      out.println("    java_cup.runtime.lr_parser parser,");      out.println("    java.util.Stack            stack,");      out.println("    int                        top)");      out.println("    throws java.lang.Exception");      out.println("  {");      out.println("    /* call code in generated class */");      out.println("    return action_obj." + pre("do_action(") +                  "act_num, parser, stack, top);");      out.println("  }");      out.println("");      /* method to tell the parser about the start state */      out.println("  /** Indicates start state. */");      out.println("  public int start_state() {return " + start_st + ";}");      /* method to indicate start production */      out.println("  /** Indicates start production. */");      out.println("  public int start_production() {return " + 		     start_production.index() + ";}");      out.println();      /* methods to indicate EOF and error symbol indexes */      out.println("  /** <code>EOF</code> Symbol index. */");      out.println("  public int EOF_sym() {return " + terminal.EOF.index() + 					  ";}");      out.println();      out.println("  /** <code>error</code> Symbol index. */");      out.println("  public int error_sym() {return " + terminal.error.index() +					  ";}");      out.println();      /* user supplied code for user_init() */      if (init_code != null)	{          out.println();	  out.println("  /** User initialization code. */");	  out.println("  public void user_init() throws java.lang.Exception");	  out.println("    {");	  out.println(init_code);	  out.println("    }");	}      /* user supplied code for scan */      if (scan_code != null)	{          out.println();	  out.println("  /** Scan to get the next Symbol. */");	  out.println("  public java_cup.runtime.Symbol scan()");	  out.println("    throws java.lang.Exception");	  out.println("    {");	  out.println(scan_code);	  out.println("    }");	}      /* user supplied code */      if (parser_code != null)	{	  out.println();          out.println(parser_code);	}      /* end of class */      out.println("}");      /* put out the action code class */      emit_action_code(out, start_prod);      parser_time = System.currentTimeMillis() - start_time;    }    /*-----------------------------------------------------------*/}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -