编译原理——变量声明语句(一)

代码:

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());
	}

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值