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

📄 my.cup

📁 非常好的编译源代码 将一个l语言转换为中间代码 java版本
💻 CUP
📖 第 1 页 / 共 2 页
字号:
package parser;//import java_cup.runtime.*;import java.util.*;/****** @author 何一鸣** 包括了 赋值语句 ,数组操作 ,普通运算,if-then,* if-then-else , while do, case ,...********//****具体分析的动作中包含*/action code {://生成的四元式列表private CodeTable codeTable=new CodeTable();//存放各个数组的信息private SymTable symTable = new SymTable();//存放各个数组的信息private HashMap array=new HashMap();//数组信息的暂时存放private ArrayList tempArray;//调用参数信息的暂时存放private ArrayList callList;/****数组方面计算 详见 p180 7.6,7.7*///第row位的大小private  int limit(String arrayName, int row) {	ArrayList cur=(ArrayList)array.get(arrayName);	return ((Integer)cur.get(row-1)).intValue();}//书中 p180 7.7 我的下界认定为 0!private int C(String arrayName) {	return 0;}//数组元素的宽度private int W(String arrayName) {		return 1;}:};/****外部分析表提供函数 包含*出错处理:*/parser code  {: 	  public void report_error(String message, Object info) {    StringBuffer m = new StringBuffer("Error ");    if (info instanceof java_cup.runtime.Symbol)       m.append( "("+info.toString()+")" );         m.append(" : "+message);       System.out.println(m);  }     public void report_fatal_error(String message, Object info) {    report_error(message, info);    throw new RuntimeException("Fatal Syntax Error");  }:};/****终结符 表*/terminal BOOLEAN; // primitive_typeterminal BYTE,   LONG,INTEGER,SHORT, CHAR; // integral_typeterminal FLOAT, REAL; // floating_point_typeterminal ARRAY,LBRACK, RBRACK; // array_typeterminal DOT; // qualified_nameterminal SEMICOLON, MULT, COMMA, LBRACE, RBRACE, EQ, LPAREN, RPAREN, COLON;terminal REPEAT,UNTIL;terminal PROGRAM ;terminal BEGIN,END; terminal VOID;  terminal CALL,PROCEDURE; terminal IF, ELSE,THEN; // if_then_statement, if_then_else_statementterminal SWITCH; // switch_statementterminal CASE, DEFAULT; // switch_labelterminal DO, WHILE; // while_statement, do_statementterminal FOR,TO; // for_statementterminal BREAK; // break_statementterminal CONTINUE; // continue_statementterminal RETURN; // return_statementterminal PLUSPLUS; // postincrement_expressionterminal MINUSMINUS; // postdecrement_expressionterminal PLUS, MINUS, COMP, NOT, DIV, MOD;terminal LSHIFT, RSHIFT; // shift_expressionterminal LT, GT, LTEQ, GTEQ; // relational_expressionterminal EQEQ, NOTEQ; // equality_expressionterminal AND; // and_expressionterminal XOR; // exclusive_or_expressionterminal OR;  // inclusive_or_expressionterminal ANDAND; // conditional_and_expressionterminal OROR; // conditional_or_expressionterminal QUESTION; // conditional_expressionterminal MULTEQ, DIVEQ, MODEQ, PLUSEQ, MINUSEQ; // assignment_operatorterminal LSHIFTEQ, RSHIFTEQ; // assignment_operatorterminal ANDEQ, XOREQ, OREQ,ASSIGN; // assignment_operatorterminal READ,WRITE;terminal java.lang.Number INTEGER_LITERAL;terminal java.lang.Number FLOATING_POINT_LITERAL;terminal java.lang.Boolean BOOLEAN_LITERAL;terminal java.lang.Character CHARACTER_LITERAL;terminal java.lang.String STRING_LITERAL;terminal java.lang.String IDENTIFIER; // nameterminal CONST, GOTO;/****非终结符 表*///  The Syntactic Grammarnon terminal goal;//  Lexical Structurenon terminal  Number literal;//  Types, Values, and Variablesnon terminal type,  numeric_type;non terminal integral_type, floating_point_type;non terminal NonTerminal array_type,array_type_list,array_types;//  Namesnon terminal NonTerminal names,define;non terminal explanations,explanation;non terminal NonTerminal statements,statement,functionstatements,funinstatements;non terminal functionexplanation,functiondefine,variablexplanation,procedurexplanation,proceduredefine,funvariablexplanation;non terminal dims;non terminal parameters,parameter;non terminal inexplanations,inexplanation;non terminal NonTerminal if_then_statement,for_pre;//  Expressionsnon terminal NonTerminal method_invocation, array_access;non terminal NonTerminal postfix_expression;non terminal NonTerminal postincrement_expression, postdecrement_expression;non terminal NonTerminal unary_expression, unary_expression_not_plus_minus;non terminal NonTerminal preincrement_expression, predecrement_expression;non terminal NonTerminal cast_expression;non terminal NonTerminal multiplicative_expression, additive_expression;non terminal NonTerminal shift_expression, relational_expression, equality_expression;non terminal NonTerminal and_expression, exclusive_or_expression, inclusive_or_expression;non terminal NonTerminal conditional_and_expression, conditional_or_expression;non terminal NonTerminal conditional_expression, assignment_expression;non terminal NonTerminal assignment;non terminal NonTerminal left_hand_side;non terminal NonTerminal assignment_operator;non terminal NonTerminal expression,expressions,statement_expression;non terminal NonTerminal Elist,M,N;non terminal NonTerminal call_list,call_lists;non terminal NonTerminal case_pre,case_list,case_end;/****文法 :*/start with goal;// The Syntactic Grammargoal	::=	PROGRAM IDENTIFIER SEMICOLON explanations BEGIN statements:e1 M:e2 END  {:										codeTable.backpath(e1.nextlist,e2.quad);										codeTable.print();										:}	;//说明explanations	::=	explanation explanations 		|			;explanation	::=	CONST type define  		|	functionexplanation 		|	functiondefine 		|	variablexplanation 		|	procedurexplanation 		|	proceduredefine		;//数组说明array_types	::=	array_type_list COMMA array_types 		|	array_type_list		;array_type ::=	IDENTIFIER:e1 LBRACK INTEGER_LITERAL:e2	{:								RESULT=new NonTerminal();								RESULT.ndim=1;RESULT.array=e1;								tempArray=new ArrayList();								tempArray.add(new Integer(e2.intValue()));							:}		;array_type ::= array_type:e1 COMMA INTEGER_LITERAL:e2 {:								tempArray.add(new Integer(e2.intValue()));								RESULT=new NonTerminal();								RESULT.array=e1.array;							:}		;array_type_list ::= array_type:e RBRACK {:								array.put(e.array,tempArray);					:}		;//变量说明variablexplanation	::=	type COLON names SEMICOLON 			|	type COLON array_types SEMICOLON 			;//标志符表names	::=	IDENTIFIER:e1 COMMA names {:						RESULT =new NonTerminal();						RESULT.place=e1;						symTable.enter(RESULT);						:}	|	IDENTIFIER:e1 {:						RESULT =new NonTerminal();						RESULT.place=e1;						symTable.enter(RESULT);						:}	;//常量定义define	::=	IDENTIFIER:e1 EQ literal SEMICOLON define {:						RESULT =new NonTerminal();						RESULT.place=e1;						symTable.enter(RESULT);						:}	|	IDENTIFIER:e1 EQ literal SEMICOLON {:						RESULT =new NonTerminal();						RESULT.place=e1;						symTable.enter(RESULT);						:}	;//函数说明functionexplanation	::=	type IDENTIFIER LPAREN parameters RPAREN SEMICOLON			;//函数参数parameter		::=	type IDENTIFIER			;parameters		::=	parameter COMMA parameters			|	parameter			|				;funinstatements		::=	statements  RETURN expression SEMICOLON 			;functionstatements	::=	funinstatements functionstatements 			|	statements			;//函数定义functiondefine		::=	type IDENTIFIER LPAREN parameters RPAREN inexplanations BEGIN functionstatements END 			;//函数或过程内 说明inexplanations	::=	inexplanation	inexplanations 		|			;inexplanation	::=	CONST	define SEMICOLON 		|	funvariablexplanation 		;funvariablexplanation	::=	type COLON names SEMICOLON 			|	type COLON array_types SEMICOLON 			;//过程说明procedurexplanation	::=	PROCEDURE IDENTIFIER SEMICOLON			;//过程定义proceduredefine	::=	PROCEDURE IDENTIFIER inexplanations BEGIN statements END		;//语句说明statements	::=	statements:e1 M:e3 statement:e2 {:							codeTable.backpath(e1.nextlist,e3.quad);							RESULT=new NonTerminal();							RESULT.nextlist=e2.nextlist;							:}		|	statement:e {:					RESULT=new NonTerminal();					RESULT.nextlist=e.nextlist;					:}	 		;	case_pre	::=	 literal:e COLON {:						codeTable.gen("case",e.toString(),CodeTable.nextquad+1+"","_");								:} 		;case_list	::=	case_list:e1 case_pre statement:e2 {:						RESULT=new NonTerminal();						RESULT.nextlist=CodeTable.nextquad;						codeTable.gen(" j ","_","_","0");						RESULT.nextlist=codeTable.merge(codeTable.merge(RESULT.nextlist,e1.nextlist),e2.nextlist);						:}		|	case_pre statement:e {:						RESULT=new NonTerminal();						RESULT.nextlist=CodeTable.nextquad;						codeTable.gen(" j ","_","_","0");						RESULT.nextlist=codeTable.merge(RESULT.nextlist,e.nextlist);						:}		;case_end	::=  CASE expression:e1 COLON case_list:e2 DEFAULT COLON {:										RESULT=new NonTerminal();										RESULT.nextlist = e2.nextlist;											codeTable.gen("case",e1.place,CodeTable.nextquad+1+"","_");									:}		;statement_expression	::=			assignment:e {:				RESULT=new NonTerminal();				RESULT.nextlist=e.nextlist;				:}		|	preincrement_expression 	|	predecrement_expression 	|	postincrement_expression 	|	postdecrement_expression 	|	method_invocation {:RESULT=new NonTerminal();:}	|	CALL IDENTIFIER:e {:RESULT=new NonTerminal();					codeTable.gen("call",e,"_","_");					:}	;N	::= ELSE {:		RESULT=new NonTerminal();		RESULT.nextlist= CodeTable.nextquad;		codeTable.gen(" j ","_","_","0");		:}	;if_then_statement	::=	IF expression:e1 THEN M:e3 statement:e2 {:									codeTable.backpath(e1.truelist,e3.quad);									RESULT=new NonTerminal();									RESULT.nextlist=codeTable.merge(e1.falselist,e2.nextlist);									:} 			;for_pre	::=	FOR  IDENTIFIER:e1 ASSIGN expression:e2 TO expression:e3 {:									RESULT=new NonTerminal();									RESULT.place=e1;									codeTable.gen(":=",e2.place,"_",RESULT.place);									String final_=NonTerminal.generateName();									codeTable.gen(":=",e3.place,"_",final_);									RESULT.nextlist=RESULT.quad=CodeTable.nextquad;									codeTable.gen(" J> ",RESULT.place,final_,"0" );								:}	;statement	::=	statement_expression:e SEMICOLON {:								RESULT=new NonTerminal();								RESULT.nextlist=e.nextlist;								:} 		|	IF expression:e1 THEN M:e3 statement:e2 N:e4  M:e6 statement:e5 {:											codeTable.backpath(e1.truelist,e3.quad);											codeTable.backpath(e1.falselist,e6.quad);											RESULT=new NonTerminal();											RESULT.nextlist=codeTable.merge(codeTable.merge(e2.nextlist,e4.nextlist),e5.nextlist);											:}		|	if_then_statement:e {:						RESULT=new NonTerminal();						RESULT.nextlist=e.nextlist;										:}				|	WHILE M:e1 expression:e2 DO M:e3 statement:e4 {:									codeTable.backpath(e4.nextlist,e1.quad);									codeTable.backpath(e2.truelist,e3.quad);									RESULT=new NonTerminal();									RESULT.nextlist=e2.falselist;									codeTable.gen(" j ","_","_",e1.quad+"");									:} 		|	 case_end:e1 statement:e2 END {:							RESULT=new NonTerminal();							RESULT.nextlist=codeTable.merge(e1.nextlist,e2.nextlist);							:}		|	REPEAT statements UNTIL expression SEMICOLON 		|	 for_pre:e1 DO statement:e2{:							codeTable.backpath(e2.nextlist,CodeTable.nextquad);							codeTable.gen("+",e1.place,"1",e1.place);							codeTable.gen(" J ","_","_",e1.quad+"");							RESULT=new NonTerminal();							RESULT.nextlist=e1.nextlist;							:} 		|	BEGIN statements:e END {:						RESULT=new NonTerminal();						RESULT.nextlist=e.nextlist;						:}		|	READ names SEMICOLON 		|	WRITE expressions SEMICOLON 		|	SEMICOLON {:					RESULT=new NonTerminal();				:}		;/****表达式*/expressions	::=	expression 		|	expression COMMA expressions

⌨️ 快捷键说明

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