} /*
// change to public { }
int ch = (int) Char;
if (ch == 9 || ch == 11 || ch == 12 || ch == 32 || ch == 10 || ch == 13) else
return false; return true;
* List
* br = new BufferReader(new FileReader(path)); while((temp = br.readLine()) != * null){ strs.add(temp); } }catch(FileNotFoundException e) {
* System.out.println(\ * } finally{ try{ if(br != null){ br.close(); br = null; } }catch(IOException * e) { e.printStackTrace(); } } */
5. viewer中代码
package analyser;
import java.io.*;
import java.lang.reflect.Method; import java.lang.*; /*
* import java.io.FileReader; import java.io.IOException; * * */ /*
* 由于我的gettoken函数编的有点问题??所以,在开始后面的语法和词法分析中最好 * 避免用.lexeme * **/
public class viewer {
public static void main(String[] args)throws Exception {
Token token;
scanner2 myscanner = new scanner2();
//myscanner.InitScanner(\myscanner.InitScanner(\/*FileReader fos; try{ }
catch(IOException E) { } */
System.out.println(\记号类别----字符串--------常数表-------函数指针-------\System.out.println(\while(true) {
token = myscanner.GetToken();
if(token.type != Token_Type.NONTOKEN) {
//System.out.println(\System.out.print(token.type);
//System.out.printf(\System.out.print(\ \//System.out.print(token.lexeme); switch(token.type){
case SEMICO:System.out.print(';');break; case L_BRACKET:System.out.print('(');break; case R_BRACKET:System.out.print(')');break; case COMMA:System.out.print(',');break; case PLUS:System.out.print('+');break; case MINUS:System.out.print('-');break; case MUL:System.out.print('*');break; case DIV:System.out.print('/');break; case POWER:System.out.print(\
default:System.out.print(token.lexeme.toString());break; }
//have a try
//System.out.print(token.lexeme);替代了 System.out.print(\ \System.out.println(E.toString()); return;
fos = new FileReader(\
}
}
}
}
System.out.print(token.value); System.out.print(\ \if(token.FunPtr == null) else
System.out.println(token.FunPtr.getName());
System.out.println(token.FunPtr); //or System.out.println(\
else{ }
System.out.println(\break;
System.out.println(\myscanner.CloseScanner();
二、 parser中的代码
1. ExprNode中的代码
package parser;
import analyser.*;
import java.lang.reflect.Method; ;
//here,we use the Method~~
public class ExprNode {
// here!
// what is ignored is how it's stored, // we care about what it has. public Token_Type OpCode; public Content content; public class Content {
public caseOp CaseOperator; public caseFunc CaseFunc; public double CaseConst;
// here,we use the Double class to instead the T public Double CaseParmPtr;
// 在初始化函数中,我们将它初始化为0 // public double[] CaseParmPtr = new double[1]; // here is very important!!!!!!!!!!!!!!!!!!!!! /**
* Double类型是double的包装类,在JDK1.5以后, 二者可以直接相互赋值,称为自动 * 看你的提示,我推测你的jdk版本在1.5以前。 如果是这样,可以用Double中的方法, * amount = rec.getAmount().doubleValue() ; * * */
// 我们用数组来代替指针 public class caseOp {
public ExprNode Left;
// here is the advantage of java; // no pointer,but class public ExprNode Right; public caseOp() { }
Left = null; Right = null;
拆箱和自动装箱。
将包装类转为 基本数据类型,如: double
}// end of class caseOp public class caseFunc {
public ExprNode child; public Method MathFunPtr; public caseFunc() { }
child = null; MathFunPtr = null;
}// end of class caseFunc public Content() {
CaseOperator = new caseOp(); CaseFunc = new caseFunc(); CaseConst = 0.0;
// System.out.println(\CaseParmPtr = new Double(0);
}
}
}
// CaseParmPtr. // CaseParmPtr. // CaseParmPtr[0] = 0;
// System.out.println(\// before the program define a T,we define it is 0;
public ExprNode()
// 千万不要忘记初始化啊?? // 很悲催的哦???? { }
// System.out.println(\OpCode = Token_Type.ERRTOKEN;
// System.out.println(\
// 和词法分析一致,最开始初始化为Token_Type.ERRORTOKEN content = new Content();
// System.out.println(\
2. parser中的代码
package parser;
//import myreflect_perfect; import analyser.*;
import java.lang.reflect.*;
//it just for Method!! public class parser {
protected Double parameter;
//protected double[] parameter; protected Token token;
protected scanner2 myscanner = new scanner2(); //we init it at definition. public parser() //the constructor.
//in order to make it visible,we set it public {