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

📄 parser.cup

📁 用Java实现的编译器。把源代码编译成SPARC汇编程序
💻 CUP
📖 第 1 页 / 共 2 页
字号:
				{:  RESULT = new MdParaList((MdPara)md_para,null); :}				;method_param::=	type:t ID:id		{: //if(debug) System.out.println("At Line ("+lineleft+"): method_param -> int/boolean ");				IDDescriptor idd = new IDDescriptor(id, parser.curType); idd.setOffset(parser.curOffset); parser.curOffset += 4;				idd.assertParamDescriptor();				parser.curST.addDescriptor(idd);				//parser.curST.addDescriptor(new IDDescriptor(id, parser.curType));			 RESULT = new MdPara((Typ)t,new Identifier(id, sym.NO_TYPE));		:}		;/**************************************************************************************/block		::= 	LCURLY:line			{:				if(debug) System.out.println("At Line ("+lineleft+"): block ->  { }");				parser.curScopeST = parser.curST;				parser.curST = new SymbolTable(); parser.curST.setParent(parser.curScopeST);			:}				var_decl_list:var_decl_lst {:  parser.curScopeST.addChild(parser.curST); :}/*parser.curScopeST = parser.curST; :}*/				statement_list:stm_lst			RCURLY:blkline			{:				parser.curST = parser.curScopeST; parser.curScopeST = parser.curST.getParent();				RESULT = new Block(var_decl_lst==null?null:((VarDeclList)var_decl_lst).reverse(),stm_lst==null?null:((StmtList)stm_lst).reverse(), blklineleft);			:};var_decl_list	::=	var_decl_list:var_lst var_decl:var                        {:  RESULT = new VarDeclList((VarDecl)var,(VarDeclList)var_lst); :}			|	empty				;var_decl		::=	type:t local_var_list:lc_var SEMI:line                                {:					if(debug) System.out.println("At Line ("+lineleft+"): var_decl ->  int/boolean");					RESULT = new VarDecl((Typ)t,((LocVarList)lc_var).reverse());				:};local_var_list	::=	local_var_list:lc_var_lst COMMA local_var:lc_var                        {:  RESULT = new LocVarList((LocVar)lc_var,(LocVarList)lc_var_lst); :}			|	local_var:lc_var                        {:  RESULT = new LocVarList((LocVar)lc_var,null); :};local_var ::= ID:id		{:	IDDescriptor idd = new IDDescriptor(id, parser.curType); idd.setOffset(parser.curOffset + 4); parser.curOffset += 4;			parser.curST.addDescriptor(idd);			RESULT = new LocVar(new Identifier(id, sym.NO_TYPE));		:};/*************************************************************************************************/statement_list	::=	statement_list:stm_lst statement:stm {:  RESULT = new StmtList((Stmt)stm,(StmtList)stm_lst); :}			|	empty				;statement	::=	location:l ASSIGN expr:e SEMI:line                        {:				if(debug) System.out.println("At Line ("+lineleft+"): statement ->  location=expr");				if(l instanceof LocationId){					IDDescriptor idd = parser.curST.lookup_var(((LocationId)l).identifier_.identifier_);					if(idd.getType() != sym.ERROR_TYPE)						idd.assertInitialized();				}				RESULT = new StmtLocation((Location)l, parser.convPos((Exp)e,eleft));//				System.out.println("Done with StmtLocation");			:}			|	method_call:m SEMI:line                        {:				if(debug) System.out.println("At Line ("+lineleft+"): statement ->  method_call");				RESULT = new StmtMdCall((MdCall)m);			:}			|	IF:line                        {:				if(debug) System.out.println("At Line ("+lineleft+"): statement ->  if_elsel");			:}					LPAREN expr:e RPAREN			{:					   Exp exp_ = (Exp)e;				  	   if(exp_.noError() &&  exp_.getType()!=sym.BOOLEAN)  	   				   	ErrorLog.log("Boolean type expected for IF condition");			:}				block:b1 else_content:b2			{:				RESULT = new StmtIfElse(parser.convPos((Exp)e,eleft), (Block)b1, (Block)b2);			:}			|	WHILE:line                        {:				if(debug) System.out.println("At Line ("+lineleft+"): statement ->  while_loop");			:}					LPAREN expr:e RPAREN			{:	Exp exp_ = (Exp)e;	  	   		if(exp_.noError() && exp_.getType()!=sym.BOOLEAN)  	   				ErrorLog.log("Boolean type expected in WHILE loop condition");			:}			block:b			{:				RESULT = new StmtWhileLoop(parser.convPos((Exp)e,eleft), (Block)b);			:}			|	FOR:line                        {:				if(debug) System.out.println("At Line ("+lineleft+"): statement ->  for_loop");			:}					LPAREN ID:idt ASSIGN expr:e1						{:	IDDescriptor idd = parser.curST.lookup_var(idt);							if(idd.getType() != sym.ERROR_TYPE)	idd.assertInitialized();							//check only, not instantiation							Identifier identifier1_ = new Identifier(idt, idd.getType()); //redundant just for semantic check						  	if(identifier1_.noError() && identifier1_.getType()!=sym.INT)						  	   	ErrorLog.log("for loop: '"+identifier1_.identifier_+"' must be an Integer type");						  	Exp exp1_ = (Exp)e1;						  	if(exp1_.noError() && exp1_.getType()!=sym.INT)						  	   	ErrorLog.log("for loop: Integer assignment expected for '"+identifier1_.identifier_+"'");						:}					SEMI expr:e2 SEMI					{:							Exp exp2_ = (Exp)e2;					  	   	if(exp2_.noError() && exp2_.getType()!=sym.BOOLEAN)					  	   		ErrorLog.log("for loop: Boolean type expected for FOR loop condition");					:}					ID:id2 ASSIGN expr:e3						{:	IDDescriptor idd = parser.curST.lookup_var(id2);							if(idd.getType() != sym.ERROR_TYPE)	idd.assertInitialized();							//check only, not instantiation							Identifier identifier3_ = new Identifier(idt, idd.getType()); //redundant just for semantic check						  	if(identifier3_.noError() && identifier3_.getType()!=sym.INT)						  	   	ErrorLog.log("for loop: '"+identifier3_.identifier_+"' must be an Integer type");						  	Exp exp3_ = (Exp)e3;  	   						if(exp3_.noError() && exp3_.getType()!=sym.INT)  	   							ErrorLog.log("for loop: Integer assignment expected for '"+identifier3_.identifier_+"'");						:}					RPAREN  block:b			{:				//int t1 = parser.curST.lookup_var(idt);				IDDescriptor idd1 = parser.curST.lookup_var(idt);				//int t2 = parser.curST.lookup_var(id2);				IDDescriptor idd2 =parser.curST.lookup_var(id2);				StmtForLoop sfl = new StmtForLoop(new Identifier(idt, idd1.getType(), idd1.getOffset(), idd1.isGlobal(), idd1.isParam()), parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left), new Identifier(id2, idd2.getType(), idd2.getOffset(), idd2.isGlobal(), idd2.isParam()), parser.convPos((Exp)e3,e3left), (Block)b);				sfl.idd1_ = idd1; sfl.idd3_ = idd2;				RESULT = sfl;			:}			|	RETURN:line                        {:				if(debug) System.out.println("At Line ("+lineleft+"): statement ->  return");			:}					return_content:e SEMI                        {:				RESULT = new StmtReturn((Exp)e);			:}			|	block:b			{:				RESULT = new StmtBlock((Block)b);			:}			|	error				;else_content	::=	ELSE:line				{:					if(debug) System.out.println("At Line ("+lineleft+"): statement ->  else");				:}				block:b {:  RESULT = b; :}			|	empty				;return_content::=	expr:e {:					parser.curMD.checkMethodType(((Exp)e).getType());					RESULT = parser.convPos((Exp)e,eleft); :}			|	empty {:					parser.curMD.checkMethodType(sym.VOID);				:}				;location		::=	ID:id {:  //int t = parser.curST.lookup_var(id);				IDDescriptor idd = parser.curST.lookup_var(id);			  LocationId lid = new LocationId( new Identifier(id, idd.getType(), idd.getOffset(), idd.isGlobal(), idd.isParam()));			  lid.idd = idd; //setting the IDDescriptor to do global constant propagation			  RESULT = lid;			 //System.out.println("Catched type: " + id + ": " + SymbolTable.getTypeName(t));			 :}			|	ID:idtd	LSQUARE expr:e RSQUARE {:					int i = 0;					Exp e_cov =parser.convPos((Exp)e,eleft);					if (e_cov instanceof LtrInt){						int index = ((LtrInt)e_cov).ltrInt_;						//System.out.println("Index = "+i);						//Vinh The's table lookup here!!!!!!!!!!!					}					//int t = parser.curST.lookup_array(idtd, i);					IDDescriptor idd = parser.curST.lookup_array(idtd, i);					//System.out.println("===============>> Perform an array index range check for direct integers");					RESULT = new LocationArray(new Identifier(idtd, idd.getType(),idd.getOffset(), true, false), e_cov); :}				;expr			::=	location:l                                {:                                 // System.out.println("Catched expr -> location: " + ((Location)l).getType());                                 	if(l instanceof LocationId){							IDDescriptor idd = parser.curST.lookup_var(((LocationId)l).identifier_.identifier_);							if(idd.getType() != sym.ERROR_TYPE){								if(!idd.isInitialized()){									System.out.println(parser.infile+":" + Scanner.lineno +										": WARNING: local variable '" + ((LocationId)l).identifier_.identifier_ + "' used without initialization");									ClassProgram.numOfWarning++;								}							}						}						((Exp)l).setExpLineNo(Scanner.lineno);                                RESULT = (Location)l;                                :}			|	method_call:mdc                                {:  RESULT = (MdCall)mdc; :}			|	literal:lit                                {: ((Exp)lit).setExpLineNo(Scanner.lineno); RESULT = (Literal)lit; :}			|	expr_bin_op_expr:e                                {:  ((Exp)e).setExpLineNo(Scanner.lineno); RESULT = (Exp)e; :}		        |     MINUS expr:e {:  ((Exp)e).setExpLineNo(Scanner.lineno); RESULT = parser.convNeg((Exp)e, eleft); :}		              %prec UMINUS			|	LPAREN expr:e1 RPAREN                                {:  ((Exp)e1).setExpLineNo(Scanner.lineno); RESULT = parser.convPos((Exp)e1,e1left); :}				;literal		::=	INTLIT:i {:  RESULT = new LtrInt(i); :}			| 	CHARLIT:c {:  //RESULT = new LtrChar((Character)c); 							RESULT = new LtrInt(""+Character.digit(((Character)c).charValue(), 10));							:}			| 	bool_literal:bl {: RESULT = bl; :}				;expr_bin_op_expr			::=	expr:e1 SHIFTLEFT expr:e2                                {:  RESULT = new ExpShiftLeft(parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left)); :}			|	expr:e1 SHIFTRIGHT expr:e2                                {:  RESULT = new ExpShiftRight(parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left)); :}			|	expr:e1 PLUS expr:e2                                {:  RESULT = new ExpPlus(parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left)); :}			|	expr:e1 MINUS expr:e2                                {:  RESULT = new ExpMinus(parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left)); :}			|	expr:e1 MULT expr:e2                                {:  RESULT = new ExpTimes(parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left)); :}			|	expr:e1 DIVIDE expr:e2                                {:  RESULT = new ExpDivide(parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left)); :}			|	expr:e1 MOD expr:e2                                {:  RESULT = new ExpMod(parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left)); :}			|	expr:e1 LESSTHAN expr:e2                                {:  RESULT = new ExpLessThan(parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left)); :}			|	expr:e1 GREATERTHAN expr:e2                                {:  RESULT = new ExpGreaterThan(parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left)); :}			|	expr:e1 LTOE expr:e2                                {:  RESULT = new ExpLessThEql(parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left)); :}			|	expr:e1 GTOE expr:e2                                {:  RESULT = new ExpGreaterThEql(parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left)); :}			|	expr:e1 EQUAL expr:e2                                {:  RESULT = new ExpEqualTo(parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left)); :}			|	expr:e1 NOTEQUAL expr:e2                                {:  RESULT = new ExpNotEqualTo(parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left)); :}			|	expr:e1 AND expr:e2                                {:  RESULT = new ExpAndOp(parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left)); :}                        |	expr:e1 OR expr:e2                                {:  RESULT = new ExpOrOp(parser.convPos((Exp)e1,e1left), parser.convPos((Exp)e2,e2left)); :};bool_literal	::=	BOOLTRUE  {:  RESULT = new LtrBoolTrue(); :}                      | BOOLFALSE {:  RESULT = new LtrBoolFalse(); :};method_call	::=	ID:id LPAREN method_call_content:mcl RPAREN                        {:                        	ExpList mcl_rev = mcl==null?null:((ExpList)mcl).reverse();                        	MethodDescriptor mdd = parser.classProgram.methods.lookup_methodDescriptor(id, mcl_rev);                        	if(parser.curMD.getMaxCallInParamSize() < mdd.getParamSize())                        		parser.curMD.setMaxCallInParamSize(mdd.getParamSize());                        	RESULT = new MdCallIn(new Identifier(id, sym.NO_TYPE), mcl_rev, mdd.getType(),mdd.getParamSize());                        	:}			|	CALLOUT LPAREN STRINGLIT:stlit callout_content:clc RPAREN			{:    int callOutParamSize = clc==null?0:((CallOutArgList)clc).callOutArg_.getIndex()+1;                        	RESULT = new MdCallOut(new LtrString(stlit), clc==null?null:((CallOutArgList)clc).reverse(),callOutParamSize); :}			;method_call_content			::=	expr_list:el {:  RESULT = el; :}			|	empty				;callout_content::=	COMMA callout_arg_list:cal {:  RESULT = cal; :}			|	empty				;expr_list		::=	expr_list:el COMMA expr:e {:  RESULT = new ExpList(parser.convPos((Exp)e,eleft),(ExpList)el); :}			|	expr:e {:  RESULT = new ExpList(parser.convPos((Exp)e,eleft),null); :}				;callout_arg_list::=	callout_arg_list:calst COMMA callout_arg:cal {:  RESULT = new CallOutArgList((CallOutArg)cal,(CallOutArgList)calst); :}			|	callout_arg:c {:  RESULT = new CallOutArgList((CallOutArg)c,null); :}				;callout_arg	::=	expr:e {:  RESULT = new CallOutArgExp(parser.convPos((Exp)e,eleft)); :}			| STRINGLIT:stlit {:  RESULT = new CallOutArgStr(new LtrString(stlit)); :}				;empty 		::= /* nothing */;

⌨️ 快捷键说明

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