?? yufa.java
字號:
/*
* Created on 2005-5-22
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.*;
public class YuFa {
int singleXFlag=1;
private int counterObj;
private String expressFour="",returnObj;
private String expression="",errMsg="error詳情:";
private boolean flagOver;
private boolean yufaFlag=true;
private String type,preType;
private String typeValue="",preTypeValue;
private String inputFile;
private int expressionValue;
FileReader fr;
BufferedReader br;
YuFa() //讀入文件
throws java.io.IOException{
inputFile="Target.txt";
int counter=0;
fr= new FileReader(inputFile);
br=new BufferedReader(fr);
}
protected void finalize()
throws java.io.IOException{
br.close();
}
private boolean readNext()
throws java.io.IOException{
String tempStr=br.readLine();
//System.out.println(tempStr.length());
if(tempStr!=null){
preType=type;preTypeValue=typeValue;
int i=tempStr.indexOf(',');
String tempStr1=tempStr.substring(1,i);
typeValue=tempStr1;
String tempStr2=tempStr.substring(i+1,tempStr.length()-1);
type=tempStr2;
System.out.println("YuFa.readNext()方法已經從"+inputFile+"中讀入一行,內容:"+tempStr);
System.out.print("已經分解成 Type:"+type+" TypeValue:"+typeValue+" ");
}
else{
type="#";
typeValue="#";
return false;
}
//System.out.println(type);
//System.out.println(typeValue);
//System.exit(1);
return true;
}
private int bds() throws java.io.IOException{//表達式
System.out.println("inside bds()");
int bdsValue=0;
String a,b;
//System.out.println(type.equals("plus"));
//System.out.println(type);
if(type.equals("plus")&&!flagOver){
advance();
bdsValue=x();
a=returnObj;
}
else if(type.equals("minus")&&!flagOver){
advance();
bdsValue=x();
bdsValue=0-bdsValue;
a=returnObj;
}else {
bdsValue=x();
a=returnObj;
}
while(true){
if(!type.equals("plus")&&!type.equals("minus"))
break;
singleXFlag=1;
if(type.equals("plus")&&!flagOver){
jfysf();
bdsValue=bdsValue+x();
b=returnObj;
emit("+",a,b,newobj(1));
}
else if(type.equals("minus")&&!flagOver){
jfysf();
bdsValue=bdsValue-x();
b=returnObj;
emit("-",a,b,newobj(1));
}
a=newobj(0);
}
returnObj=newobj(0);
return bdsValue;
}
private void bsf() throws java.io.IOException{//標示符
System.out.println("inside bsf()");
if(!flagOver){
advance();
while(type.equals("ident") || type.equals("number")){
advance();
}
}
}
private int x() throws java.io.IOException{//項
System.out.println("inside x()");
int xValue=0;
String a,b;
xValue=yz(1);
a=returnObj;
while(true){
//System.out.println(!type.equals("times"));
//System.exit(1);
if(!type.equals("slash")&&!type.equals("times")){
break;
}
singleXFlag=0;
if(type.equals("slash")){
cfysf();
xValue=xValue/yz(0);
b=returnObj;
emit("/",a,b,newobj(1));
}else if(type.equals("times")){
cfysf();
xValue=xValue*yz(0);
b=returnObj;
emit("*",a,b,newobj(1));
}
}
if(singleXFlag==1){
//System.out.println("sssssss1");
returnObj=String.valueOf(xValue);
}else{
returnObj=newobj(0);
//System.out.println("sssssss2");
}
return xValue;
}
private int yz(int xFlag) throws java.io.IOException{//因子
System.out.println("inside yz()");
int yzValue=1;
if(type.equals("ident")&&!flagOver){
bsf();
}
else if(type.equals("number")&&!flagOver){
returnObj=typeValue;
yzValue=Integer.parseInt(typeValue);
advance();
}
else{
if(type.equals("lparen")&&!flagOver){
advance();
yzValue=bds();
//returnObj=newobj(1);
if(type.equals("rparen")&&!flagOver){
advance();
singleXFlag=0;
}else{
error("在符號'"+typeValue+"'處有錯誤 可能期盼一個左括號')'");
}
}else if(xFlag==1){
error("在符號'"+typeValue+"'之前應該跟項 或者 期盼一個左括號'('");
//System.exit(1);
}
else{
error("在符號'"+typeValue+"'之前應該跟因子 或者 期盼一個左括號'('");
}
}
return yzValue;
}
private void jfysf()//加法運算符
throws java.io.IOException{
System.out.println("inside jfysf()");
if(type.equals("plus")&&!flagOver){
advance();
}
else if(type.equals("minus")&&!flagOver){
advance();
}
}
private void cfysf() throws java.io.IOException{//乘法運算符
System.out.println("inside cfysf()");
if(type.equals("times")&&!flagOver){
advance();
}
else if(type.equals("slash")&&!flagOver){
advance();
}
}
private void wfhzs() throws java.io.IOException{//無符號整數
System.out.println("inside wfhzs()");
sz();
}
private void sz()//數字
throws java.io.IOException{
System.out.println("inside sz()");
if(type.equals("number")&&!flagOver){
advance();
}
}
private void emit(String op,String a,String b,String obj){//生成4元式
expressFour+="("+op+","+a+","+b+","+obj+")"+"\n";
}
private String newobj(int flag){
String temp="";
if(flag==1){
counterObj++;
temp="T" +counterObj;
//System.out.println("YuFa.newobj(1)創建了目標變量"+temp);
}else if(flag==0) {
temp="T"+counterObj;
//System.out.println("YuFa.newobj(0)使用了一次當前目標變量"+temp);
}
return temp;
}
private void advance()
throws java.io.IOException{//進一步
expression+=typeValue;
boolean condition=readNext();
//System.out.println(condition);
if(condition){
System.out.println("繼續執行advance(),現在指向"+typeValue);
}
else{
flagOver=true;//表示結束
System.out.println(inputFile+"文件讀完");
}
}
private void error(String errmsg) throws java.io.IOException{
yufaFlag=false;
System.out.println(errmsg);
errMsg+=errmsg;
expression+=" <此處error> ";
while(!flagOver){
advance();
}
}
public String analays() throws java.io.IOException{
//表達式部分語法分析過程開始
System.out.println("\n########## 下面輸出調試信息 #########\n");
advance();
expressionValue=bds();
if(!type.equals("#")){
error("錯誤,在符號"+typeValue+"附近,可能沒有跟一個項");
}
System.out.println("\n########## 調試信息輸出結束 #########\n\n\n\n\n########## 以下為表達式語法分析結果 #########\n");
if(yufaFlag){
System.out.println("表達式為: "+expression+"#");
System.out.println("分析結果:此表達式語法正確!");
System.out.println("表達式值為:"+expressionValue);
System.out.println("\n4元式為:\n"+expressFour);
System.out.println("Expression:="+newobj(0)+" Over!");
}
else {
System.out.println(expression+"#");
System.out.println("分析結果:此表達式語法有錯誤!");
System.out.println(errMsg);
}
System.out.println("\n########## 表達式語法分析結果結束 ###########\n\n\n\n\n");
//分析過程結束
System.out.println("方法Yufa.analays() 成功執行!");
return "1";
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -