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

📄 parser.java

📁 我开发的一个用java语言实现的编译器,内含词法分析器,语法分析器,而且可以实现中间代码生成.用到了SLR算法和LR(1)算法
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	      /* build a production_part and put it in the symbols table */ 	      symbols.put(non_term_id, new symbol_part(this_nt));	    }	              CUP$parser$result = new java_cup.runtime.Symbol(26/*new_non_term_id*/, ((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 79: // new_term_id ::= ID             {              Object RESULT = null;		int term_idleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left;		int term_idright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right;		String term_id = (String)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value;		 	  /* see if this terminal has been declared before */	  if (symbols.get(term_id) != null)	    {	      /* issue a message */	      lexer.emit_error("java_cup.runtime.Symbol \"" + term_id + 			   "\" has already been declared");	    }	  else	    {	      /* if no type declared, declare one */	      if (multipart_name.equals("")) {		append_multipart("Object");	      }	      /* build a production_part and put it in the table */ 	      symbols.put(term_id, 	        new symbol_part(new terminal(term_id, multipart_name)));	    }	              CUP$parser$result = new java_cup.runtime.Symbol(25/*new_term_id*/, ((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 78: // type_id ::= type_id LBRACK RBRACK             {              Object RESULT = null;		 multipart_name = multipart_name.concat("[]");               CUP$parser$result = new java_cup.runtime.Symbol(19/*type_id*/, ((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 77: // type_id ::= multipart_id             {              Object RESULT = null;              CUP$parser$result = new java_cup.runtime.Symbol(19/*type_id*/, ((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 76: // import_id ::= multipart_id             {              Object RESULT = null;              CUP$parser$result = new java_cup.runtime.Symbol(15/*import_id*/, ((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 75: // import_id ::= multipart_id DOT STAR             {              Object RESULT = null;		 append_multipart("*");               CUP$parser$result = new java_cup.runtime.Symbol(15/*import_id*/, ((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 74: // multipart_id ::= robust_id             {              Object RESULT = null;		int an_idleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left;		int an_idright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right;		String an_id = (String)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value;		 append_multipart(an_id);               CUP$parser$result = new java_cup.runtime.Symbol(13/*multipart_id*/, ((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 73: // multipart_id ::= multipart_id DOT robust_id             {              Object RESULT = null;		int another_idleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left;		int another_idright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right;		String another_id = (String)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value;		 append_multipart(another_id);               CUP$parser$result = new java_cup.runtime.Symbol(13/*multipart_id*/, ((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 72: // opt_label ::= empty             {              String RESULT = null;		 RESULT = null;               CUP$parser$result = new java_cup.runtime.Symbol(39/*opt_label*/, ((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 71: // opt_label ::= COLON label_id             {              String RESULT = null;		int labidleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left;		int labidright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right;		String labid = (String)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value;		 RESULT = labid;               CUP$parser$result = new java_cup.runtime.Symbol(39/*opt_label*/, ((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 70: // prod_part ::= CODE_STRING             {              Object RESULT = null;		int code_strleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left;		int code_strright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right;		String code_str = (String)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value;		 	  /* add a new production part */	  add_rhs_part(new action_part(code_str));	              CUP$parser$result = new java_cup.runtime.Symbol(24/*prod_part*/, ((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 69: // prod_part ::= symbol_id opt_label             {              Object RESULT = null;		int symidleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).left;		int symidright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-1)).right;		String symid = (String)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-1)).value;		int labidleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left;		int labidright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right;		String labid = (String)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value;		 	  /* try to look up the id */	  production_part symb = (production_part)symbols.get(symid);	  /* if that fails, symbol is undeclared */	  if (symb == null)	    {	      if (lexer.error_count == 0)	        lexer.emit_error("java_cup.runtime.Symbol \"" + symid + 			       "\" has not been declared");	    }	  else	    {	      /* add a labeled production part */	      add_rhs_part(add_lab(symb, labid));	    }	              CUP$parser$result = new java_cup.runtime.Symbol(24/*prod_part*/, ((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 68: // prod_part_list ::= empty             {              Object RESULT = null;              CUP$parser$result = new java_cup.runtime.Symbol(23/*prod_part_list*/, ((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 67: // prod_part_list ::= prod_part_list prod_part             {              Object RESULT = null;              CUP$parser$result = new java_cup.runtime.Symbol(23/*prod_part_list*/, ((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 66: // rhs ::= prod_part_list             {              Object RESULT = null;		 	  if (lhs_nt != null) 	    {	      /* build the production */	      production p = new production(lhs_nt, rhs_parts, rhs_pos);	      /* if we have no start non-terminal declared and this is 		 the first production, make its lhs nt the start_nt 		 and build a special start production for it. */              if (start_nt == null)		{		  start_nt = lhs_nt;	          /* build a special start production */	          new_rhs();	          add_rhs_part(add_lab(new symbol_part(start_nt),"start_val"));	          add_rhs_part(new symbol_part(terminal.EOF));		  add_rhs_part(new action_part("RESULT = start_val;"));	          emit.start_production = 		     new production(non_terminal.START_nt, rhs_parts, rhs_pos);	          new_rhs();		}	    }	  	  /* reset the rhs accumulation in any case */	  new_rhs();	              CUP$parser$result = new java_cup.runtime.Symbol(28/*rhs*/, ((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 65: // rhs ::= prod_part_list PERCENT_PREC term_id             {              Object RESULT = null;		int term_nameleft = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).left;		int term_nameright = ((java_cup.runtime.Symbol)CUP$parser$stack.elementAt(CUP$parser$top-0)).right;		String term_name = (String)((java_cup.runtime.Symbol) CUP$parser$stack.elementAt(CUP$parser$top-0)).value;			  java_cup.symbol sym = null;	  if (lhs_nt != null) 	    {	      /* Find the precedence symbol */	      if (term_name == null) {		System.err.println("No terminal for contextual precedence");		sym = null;	      } else {		sym = ((symbol_part)symbols.get(term_name)).the_symbol();	      }	      /* build the production */	      production p;	      if ((sym!=null) && (sym instanceof terminal)) {		p = new production(lhs_nt, rhs_parts, rhs_pos,				   ((terminal)sym).precedence_num(),				   ((terminal)sym).precedence_side());		((symbol_part)symbols.get(term_name)).the_symbol().note_use();	      } else {		System.err.println("Invalid terminal " + term_name + 				   " for contextual precedence assignment");		p = new production(lhs_nt, rhs_parts, rhs_pos);	      }	      /* if we have no start non-terminal declared and this is 		 the first production, make its lhs nt the start_nt 		 and build a special start production for it. */              if (start_nt == null)		{		  start_nt = lhs_nt;	          /* build a special start production */	          new_rhs();	          add_rhs_part(add_lab(new symbol_part(start_nt),"start_val"));	          add_rhs_part(new symbol_part(terminal.EOF));		  add_rhs_part(new action_part("RESULT = start_val;"));		  if ((sym!=null) && (sym instanceof terminal)) {

⌨️ 快捷键说明

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