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

📄 compiler.java

📁 类c语言编译器
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		lrTable[53][2]=214;
		lrTable[53][8]=214;
		lrTable[53][9]=214;
		lrTable[54][1]=105;
		lrTable[54][6]=103;
		lrTable[54][11]=104;
		lrTable[54][24]=102;
		lrTable[54][26]=58;
		lrTable[55][4]=225;
		lrTable[55][21]=225;
		lrTable[56][3]=208;
		lrTable[56][4]=208;
		lrTable[56][21]=208;
		lrTable[56][25]=208;
		lrTable[57][1]=228;
		lrTable[57][6]=228;
		lrTable[57][11]=228;
		lrTable[57][24]=228;
		lrTable[57][36]=59;
		lrTable[58][3]=211;
		lrTable[58][4]=211;
		lrTable[58][21]=211;
		lrTable[58][25]=211;
		lrTable[59][1]=105;
		lrTable[59][6]=103;
		lrTable[59][11]=104;
		lrTable[59][24]=102;
		lrTable[59][26]=60;
		lrTable[60][3]=209;
		lrTable[60][4]=209;
		lrTable[60][21]=209;
		lrTable[60][25]=209;
	}
}
//===产生式
class genFormula
{
	Vector genCode;		//===非终结符代码
	Vector genSymbol;	//===非终结符字符
	Vector genOut;		//===转换后的产生式
	Vector genAll;		//===完整产生式
	public genFormula()
	{
		genCode=new Vector();
		genSymbol=new Vector();
		genOut=new Vector();
		genAll=new Vector();
		//===经过转换的token串
		addFormula("18","T","S");
		addFormula("1","S","yqC");
		addFormula("2","C","CwD");
		addFormula("2","C","D");
		addFormula("3","D","DxF");
		addFormula("3","D","F");
		addFormula("4","F","mCn");
		addFormula("4","F","y");
		addFormula("1","S","gEjASV");
		addFormula("17","V","dBS");
		addFormula("17","V","z");
		addFormula("1","S","lMEcNS");
		addFormula("5","E","EiOH");
		addFormula("5","E","H");
		addFormula("6","H","HaRI");
		addFormula("6","H","I");
		addFormula("7","I","hJ");
		addFormula("7","I","J");
		addFormula("8","J","yoy");
		addFormula("8","J","ypy");
		addFormula("8","J","yry");
		addFormula("8","J","ysy");
		addFormula("8","J","yty");
		addFormula("8","J","yuy");
		addFormula("1","S","bKe");
		addFormula("9","K","KvPS");
		addFormula("9","K","S");
		addFormula("10","A","z");
		addFormula("11","B","z");
		addFormula("12","M","z");
		addFormula("13","N","z");
		addFormula("14","O","z");
		addFormula("15","R","z");
		addFormula("16","P","z");
		//===完整的token串
		addFormulaAll("T->S");
		addFormulaAll("S->id=C");
		addFormulaAll("C->C+D");
		addFormulaAll("C->D");
		addFormulaAll("D->D*F");
		addFormulaAll("D->F");
		addFormulaAll("F->(C)");
		addFormulaAll("F->id");
		addFormulaAll("S->if E then A S V");
		addFormulaAll("V->else B S");
		addFormulaAll("V->e");
		addFormulaAll("S->while M E do N S");
		addFormulaAll("E->E or O H");
		addFormulaAll("E->H");
		addFormulaAll("H->H and RI");
		addFormulaAll("H->I");
		addFormulaAll("I->not J");
		addFormulaAll("I->J");
		addFormulaAll("J->id < id");
		addFormulaAll("J->id > id");
		addFormulaAll("J->id <= id");
		addFormulaAll("J->id >= id");
		addFormulaAll("J->id <> id");
		addFormulaAll("J->id == id");
		addFormulaAll("S->begin K end");
		addFormulaAll("K->K ; P S");
		addFormulaAll("K->S");
		addFormulaAll("A->e");
		addFormulaAll("B->e");
		addFormulaAll("M->e");
		addFormulaAll("N->e");
		addFormulaAll("O->e");
		addFormulaAll("R->e");
		addFormulaAll("P->e");
	}
	//===添加完整产生式
	void addFormulaAll(String formula)
	{
		genAll.addElement(formula);
	}
	//===添加转换产生式
	void addFormula(String code,String symbol,String formula)
	{
		genCode.addElement(code);
		genSymbol.addElement(symbol);
		genOut.addElement(formula);
	}
}
class symbolTable
{
	Vector	symbol;				//===符号
	Vector	code;				//===编码
	Vector	tokenLine;			//===token串行号
	Vector  tokenColumn;		//===token串列号
	Vector	tokenSym;			//===token串字符
	Vector	tokenCode;			//===token串编码
	int line;					//===行号
	int column;					//===列号
	int errorLine;				//===出错行号
	int errorColumn;			//===出错列号
	boolean isError;			//===是否出错
	
	public symbolTable()
	{
		tokenLine=new Vector();
		tokenColumn=new Vector();
		line=1;
		column=0;
		errorLine=0;
		errorColumn=0;
		isError=false;
		symbol=new Vector();
		code=new Vector();
		//===初始化符号表
		addPair("and","0");
		addPair("begin","1");
		addPair("do","2");
		addPair("else","3");
		addPair("end","4");
		addPair("false","5");
		addPair("if","6");
		addPair("not","7");
		addPair("or","8");
		addPair("then","9");
		addPair("true","10");
		addPair("while","11");
		addPair("(","12");
		addPair(")","13");
		addPair("<","14");
		addPair(">","15");
		addPair("=","16");
		addPair("<=","17");
		addPair(">=","18");
		addPair("<>","19");
		addPair("==","20");
		addPair(";","21");
		addPair("+","22");
		addPair("*","23");
		addPair("标识符","24");
		
		tokenSym=new Vector();
		tokenCode=new Vector();
	}
	//===添加非终结符及编码
	void addPair(String symString,String codeString)
	{
		symbol.addElement(symString);
		code.addElement(codeString);
	}	
	//===添加token串
	void addTokenPair(String tokString,String tokCode,int line,int column)
	{
		tokenSym.addElement(tokString);
		tokenCode.addElement(tokCode);
		tokenLine.addElement(Integer.toString(line));
		tokenColumn.addElement(Integer.toString(column));
	}
	//===是否为字母
	boolean isAlpha(char ch)
	{
		if((ch>64 && ch<91) || (ch>96 && ch<123))
		{
			return true;
		}
		return false;
	}
	//===是否为数字
	boolean isDigit(char ch)
	{
		if(ch>47 && ch<58)
		{
			return true;
		}
		return false;
		
	}
	//===是否为<、>、=(为识别<、>、<=、>=、<>、==)
	boolean isSymbol(char ch)
	{
		if(ch>59 && ch<63)
		{
			return true;
		}
		return false;
	}
	//===添加符号表,在符号表中不存在则添加
	void checkAdd(String strTemp,String strCode,int line,int column)
	{
		for(int i=0;i<symbol.size();i++)
		{
			if(((String)symbol.elementAt(i)).compareTo(strTemp)==0)
			{
				addTokenPair(strTemp,(String)code.elementAt(i),line,column);
				return;
			}
		}
		addPair(strTemp,strCode);
		addTokenPair(strTemp,strCode,line,column);
	}
	//===测试用
	void display()
	{
		System.out.println("符号表\n");
		for(int i=0;i<symbol.size();i++)
		{
			System.out.println((String)symbol.elementAt(i)+"\t"+(String)code.elementAt(i)+"\n");
		}
		System.out.println("token串\n");
		for(int j=0;j<tokenSym.size();j++)
		{
			System.out.println((String)tokenSym.elementAt(j)+"\t"+(String)tokenCode.elementAt(j)+"\t"+(String)tokenLine.elementAt(j)+"\t"+(String)tokenColumn.elementAt(j)+"\n");
		}
		System.out.println("token change串\n");
	}
	//===进行词法分析,添加符号表及token串
	void recognize(String source) throws StringIndexOutOfBoundsException 
	{
		char charTemp;
		String identi;
		String strTemp;
		int strIndex=0;
		int indexTemp=0;
		boolean isIdenti;
		charTemp=source.charAt(strIndex++);
		while(true)
		{
			switch(charTemp)
			{
			case '\n':line++;
					  column=0;
					  break;
			case '(':column++;
					  checkAdd("(","24",line,column);
					  break;
			case ')':column++;
					  checkAdd(")","24",line,column);
					 break;
			case ';':column++;
					 checkAdd(";","24",line,column);
					 break;
			case '+':column++;
					 checkAdd("+","24",line,column);
					 break;
			case '*':column++;
					 checkAdd("*","24",line,column);
					 break;
			default:column++;
					//================================================================================
					//==============================遇到无效字符出错[词法错误]========================
					//================================================================================
					if((charTemp>32 && charTemp<40) || (charTemp>43 && charTemp<48) || (charTemp==58) || (charTemp==63) ||(charTemp==64) || (charTemp>90 && charTemp<97) || (charTemp>122))
					{
						errorLine=line;
						errorColumn=column;
						isError=true;
						break;
					}
					//===如果是字母
					 if(isAlpha(charTemp))
					 {  		
						indexTemp=strIndex-1;
						if(strIndex<source.length())
						{
							charTemp=source.charAt(strIndex++);
						}
						else
						{
							strIndex--;
							strTemp=source.substring(indexTemp);
							checkAdd(strTemp,"24",line,column);
							checkAdd("$","25",line,column);
							return;								
						}
						while(isAlpha(charTemp) || isDigit(charTemp))
						{
							if(strIndex<source.length())
							{
								charTemp=source.charAt(strIndex++);
							}
							else
							{
								strIndex--;
								strTemp=source.substring(indexTemp);
								checkAdd(strTemp,"24",line,column);
								checkAdd("$","25",line,column);
								return;
							}
						}
						strIndex--;
						strTemp=source.substring(indexTemp,strIndex);
						checkAdd(strTemp,"24",line,column);
						if(strIndex==source.length()-1)
						{
							strTemp=source.substring(strIndex);
							char chartemp=strTemp.charAt(0);
							if(charTemp!='\r' && charTemp!='\n')
							{
								checkAdd(strTemp,"24",line,++column);
							}
							checkAdd("$","25",line,column);
							if((chartemp>32 && chartemp<40) || (chartemp>43 && chartemp<48) || (chartemp==58) || (chartemp==63) ||(chartemp==64) || (chartemp>90 && chartemp<97) || (chartemp>122))
							{
							errorLine=line;
							errorColumn=++column;
							isError=true;
							}
							return;
						}
					}			
					//===如果是数字
					else if(isDigit(charTemp))
					{
						indexTemp=strIndex-1;						
						if(strIndex<source.length())
						{
							charTemp=source.charAt(strIndex++);
						}
						else
						{
							strIndex--;
							strTemp=source.substring(indexTemp);
							checkAdd(strTemp,"24",line,column);
							checkAdd("$","25",line,column);
							return;
						}
						while(isDigit(charTemp))
						{
							if(strIndex<source.length())
							{
								charTemp=source.charAt(strIndex++);
							}
							else
							{
								strIndex--;
								strTemp=source.substring(indexTemp);
								checkAdd(strTemp,"24",line,column);
								checkAdd("$","25",line,column);
								return;
							}
						}
						strIndex--;
						strTemp=source.substring(indexTemp,strIndex);
						checkAdd(strTemp,"24",line,column);
						if(strIndex==source.length()-1)
						{
							strTemp=source.substring(strIndex);
							char chartemp=strTemp.charAt(0);
							if(charTemp!='\r' && charTemp!='\n')
							{
								checkAdd(strTemp,"24",line,++column);
							}
							checkAdd("$","25",line,column);
							if((chartemp>32 && chartemp<40) || (chartemp>43 && chartemp<48) || (chartemp==58) || (chartemp==63) ||(chartemp==64) || (chartemp>90 && chartemp<97) || (chartemp>122))
							{
							errorLine=line;
							errorColumn=++column;
							isError=true;
							}
							return;
						}
					}
					//===如果是<、>或=
					else if(isSymbol(charTemp))
					{
						indexTemp=strIndex-1;
						if(strIndex<source.length())
						{
							charTemp=source.charAt(strIndex++);
						}
						else
						{
							strIndex--;
							strTemp=source.substring(indexTemp);
							checkAdd(strTemp,"24",line,column);
							checkAdd("$","25",line,column);
							return;
						}
						while(isSymbol(charTemp))
						{
							if(strIndex<source.length())
							{
								charTemp=source.charAt(strIndex++);
							}
							else
							{
								strIndex--;
								strTemp=source.substring(indexTemp);
								checkAdd(strTemp,"24",line,column);
								checkAdd("$","25",line,column);
								return;
							}
						}
						strIndex--;
						strTemp=source.substring(indexTemp,strIndex);
						checkAdd(strTemp,"",line,column);
						if(strIndex==source.length()-1)
						{
							strTemp=source.substring(strIndex);
							char chartemp=strTemp.charAt(0);
							if(charTemp!='\r' && charTemp!='\n')
							{
								checkAdd(strTemp,"24",line,++column);
							}
							checkAdd("$","25",line,column);
							if((chartemp>32 && chartemp<40) || (chartemp>43 && chartemp<48) || (chartemp==58) || (chartemp==63) ||(chartemp==64) || (chartemp>90 && chartemp<97) || (chartemp>122))
							{
							errorLine=line;
							errorColumn=++column;
							isError=true;
							}
							return;
						}
					}
					break;
			}
			if(strIndex<source.length())
			{
				charTemp=source.charAt(strIndex++);
			}
			else
			{
				if(charTemp!=' ')
				{
					strIndex--;
					strTemp=source.substring(indexTemp);
					checkAdd(strTemp,"24",line,column);
				}
				checkAdd("$","25",line,column);
				return;
			}
		}
	}
}

⌨️ 快捷键说明

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