代码:
Compile.java:
package per.eyuan.compile;
import per.eyuan.util.Id;
import per.eyuan.util.IdTable;
public class Compile {
IO io=new IO();//读取源代码
// Id id=new Id();//标识符
IdTable idt=new IdTable();//标识符表
Lexical lex=new Lexical();//词法分析
Syntax syn=new Syntax();//语法分析
public static void main(String[] args) {
Compile com=new Compile();
com.lex.setIdt(com.idt);//传递标识符表给词法分析
com.lex.setStatement(com.io.sb.toString());//传递读取的源代码
com.lex.analyse();//转换源代码形式,得到二元式,修改标识符表(name)
//测试词法分析后的标识符表
System.out.println("词法分析后");
for(int i=0;i<com.lex.getIdt().getCount();i++){
System.out.print(com.lex.getIdt().getAllId()[i].getIndex()+"\t");
System.out.print(com.lex.getIdt().getAllId()[i].getName()+"\t");
System.out.print(com.lex.getIdt().getAllId()[i].getType()+"\t");
System.out.println();
}
com.syn.setTi(com.lex.getTi());//将二元式(有效)传递到语法分析
com.syn.setIdt(com.idt);//将标识符表传递给语法分析
// com.syn.setIdt(com.lex.getIdt());
//com.idt和com.lex.getIdt()是同一个标识符表
com.syn.analyse();//对待分析语句进行分析,修改标识符表(type)
//测试语法分析后的标识符表
System.out.println("语法分析后");
for(int i=0;i<com.syn.getIdt().getCount();i++){
System.out.print(com.syn.getIdt().getAllId()[i].getIndex()+"\t");
System.out.print(com.syn.getIdt().getAllId()[i].getName()+"\t");
System.out.print(com.syn.getIdt().getAllId()[i].getType()+"\t");
System.out.println();
}
//com.test();
}
public void test(){
System.out.println(io.sb.toString());
}
}