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

📄 pl0.java

📁 编译原理课程设计 PL0编译器 java版本
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	}
	
    public int base(int l)  //用来寻找基址的方法
    {
        int b1;
        b1 = b;
        while(l>0)
        {
            b1=s[b1];
            l--;
        }
        return b1;
    }

	
    public void interpret(){
        s[1]=0;
        s[2]=0;
        //s[3]=0;
        System.out.println("start pl/0");
        do{
            i=(pcode)pcodeArray.get(p);
            p++;
            if(i.getF().equals("lit"))
            {
                t++;
                s[t]=i.getA();
            }else if(i.getF().equals("opr"))
            {
                if(i.getA()==0)
                {
                    t=b-1;
                    p=s[t+3];
                    b=s[t+2];
                }else if(i.getA()==1)//返回指令
                {
                    s[t]=-s[t];
                }else if(i.getA()==2)//加法操作
                {
                    t--;
                    s[t]=s[t]+s[t+1];
                }else if(i.getA()==3)//3号减法操作
                {
                    t--;
                    s[t]=s[t]-s[t+1];
                }else if(i.getA()==4)//4号乘法操作
                {
                    t--;
                    s[t]=s[t]*s[t+1];
                }else if(i.getA()==5)//5号除法操作
                {
                    t--;
                    s[t]=s[t]/s[t+1];
                }else if(i.getA()==6)//6号判断奇数操作
                {
                	  if(s[t]%2==0)  s[t]=0;
                	  else   s[t]=1;
                }
                else if(i.getA()==8)//8号判等操作
                {
                    t--;
                    if(s[t]==s[t+1]) s[t]=1;
                    else  s[t]=0;
                }else if(i.getA()==9)//9号判断不等操作
                {
                    t--;
                    if(s[t]==s[t+1]) s[t]=0;
                    else  s[t]=1;
                }else if(i.getA()==10)//10号判断小于操作
                {
                    t--;
                    if(s[t]<s[t+1]) s[t]=1;
                    else  s[t]=0;
                }else if(i.getA()==11)//11号判断大于等于操作 
                {
                    t--;
                      if(s[t]>=s[t+1]) s[t]=1;
                    else  s[t]=0;
                }else if(i.getA()==12)//12号判断大于操作
                {
                    t--;
                      if(s[t]>s[t+1]) s[t]=1;
                    else  s[t]=0;
                }else if(i.getA()==13)//13号判断小于等于操作
                {
                    t--;
                      if(s[t]<=s[t+1]) s[t]=1;
                    else  s[t]=0;
                }else if(i.getA()==14)//14号输出栈顶值操作
                {
                    System.out.println(s[t]);
                    t--;
                }else if(i.getA()==15)//15号输出换行操作
                {
                    System.out.println();
                }else if(i.getA()==16)//16号接受键盘输入值到栈顶操作
                {
                    System.out.println("请输入数字(0是退出):");
                    int age;
                    try {
                           InputStreamReader fin=new InputStreamReader(System.in);
  	      	               BufferedReader br=new   BufferedReader(fin);
                           String name=br.readLine();
                           age = Integer.parseInt(name);
                        }catch(Exception e){
                           age=0;
                        }
                    t++;
                    s[t] = age;
                 }

            }else if(i.getF().equals("lod"))
            {
                t++;
                s[t]=s[base(i.getL())+i.getA()];
            }else if(i.getF().equals("sto"))
            {   
                s[base(i.getL())+i.getA()] = s[t];
                t--;
            }else if(i.getF().equals("cal"))
            {
                s[t+1]=base(i.getL());
                s[t+2]=b;
                s[t+3]=p;
                b=t+1;
                p=i.getA();
            }else if(i.getF().equals("int"))
            {
                t = t+i.getA();
            }else if(i.getF().equals("jmp"))
            {
                p=i.getA();
            }else if(i.getF().equals("jpc"))
            {
                if(s[t]==0)
                    p=i.getA();
                t--;
            }
            else if(i.getF().equals("wrt"))
            {
                System.out.println(s[t]);
                t--;
            }
        }while(p!=0);
        System.out.println("end pl/0");
    }
}
 class CiFaAnalyse//词法分析类
{
	String line;  //string变量用于存放读取的一行源代码
	int lineLength;//存放line中的字符数
	int lineNum=0;//存放line的号码
	public char  Cha;  //存放读取的字符
  int index;//存放当前读取的字符位置
  int al=10;  //定义标识符长度
  int nmax=4;//定义整数位数     
  public int errorNumber=0;   //用于保存词法分析中的错误数目
  BufferedReader in;
  static	token word[];
	public CiFaAnalyse(String filename)
	{
		//定义保留字表word
			 word= new token[13];
		word[0]=new token("begin","beginsym");
		word[1]=new token("call","callsym");    
		word[2]=new token("const","constsym");
		word[3]=new token("do","dosym");
		word[4]=new token("end","endsym");
		word[5]=new token("if","ifsym");
		word[6]=new token("odd","oddsym");
		word[7]=new token("procedure","procsym");
		word[8]=new token("read","readsym");
		word[9]=new token("then","thensym");
		word[10]=new token("var","varsym");
		word[11]=new token("while","whilesym");
		word[12]=new token("write","writesym");
		//根据文件名建立文件输入流
		try{
		FileInputStream fis=new FileInputStream(filename);
	  in= new BufferedReader(new InputStreamReader(fis)); 
	  line=in.readLine().trim();   // 读取一行源代码.并去除开头和结尾的空格
	  lineLength=line.length();
	  lineNum=1;
	  while(lineLength==0){ //如果此行源代码为空,则继续读取下一行
		  line=in.readLine().trim();
		  lineLength=line.length();	  
			lineNum++;
			}
			index=0;
			  GetCh();
	}
	catch(FileNotFoundException a)
	{System.out.println("没有找到文件!!");}
	catch(IOException b)
	{System.out.println("文件读取错误");}
  }	
  public  int  getErroNumber()
  {return errorNumber;}
	public void GetCh()//利用方法GetCh()取得一个字符
	{
		try{
			if(index==lineLength)//如果到达行尾,则重新读取一行源码
			{                           //其实读取有效行可以写一个方法
				line=in.readLine().trim(); 
				lineLength=line.length();
				lineNum++;
			  while(lineLength==0){
			  	line=in.readLine().trim();
			    lineLength=line.length();
			    lineNum++;
			  }
			index=0;
			Cha=line.charAt(index);
			index++;	
			}
			else{
				Cha=line.charAt(index);
			  index++;	
			}
		}
			catch(NullPointerException e)
			{
			   System.out.println("*****************************源程序读取完毕*******************************");
			}
			catch(IOException c){  }
	}
	public token GetToken()//返回一个token对象 其中名字和类型已经赋值
	{
		
		int charNum1;//记录当前单词的开始索引
		int charNum2;//记录当前单词的结束索引
		String tokenName;//当前单词
		int n=0;  //用于记录保留字数组的下标
		while(Cha==' ')
		{
			GetCh();
		}
		if(Cha>='a'&&Cha<='z')
		{
			charNum1=index-1;
			while((Character.isLetter(Cha)||Character.isDigit(Cha))&&index<lineLength)
			{
			  GetCh();
			}
			charNum2=index;
			if((charNum2-charNum1)>al)
			{
				new   erro(lineNum,19);
				errorNumber++;
				tokenName=line.substring(charNum1,charNum1+al);
			}//标识符长度越界
			else{
			if(Character.isLetter(Cha)||Character.isDigit(Cha))
			{tokenName=line.substring(charNum1);GetCh();}
			else
			 {tokenName=line.substring(charNum1,charNum2-1);}
			}
			//判断保留字过程
				while(n<13&&(!word[n].getNam().equals(tokenName)))
				{					
					n++;                                           
				}			
			if(n==13)   //如果保留字表中没有找到 则为标识符
			{			
				return  new token(tokenName,"ident",lineNum);
			}
			else
			{
				word[n].setLineNum(lineNum);
			  return  word[n];
		  }
		}    //判断是否是整数
		else if(Character.isDigit(Cha))
		{
			charNum1=index-1;
			charNum2=index;
			while(Character.isDigit(Cha)&&index<lineLength)
			{  charNum2=index;
			  GetCh();
			}
			if(Character.isDigit(Cha))//判断while为什么停止循环的,如果cha还是数字,则表明是因为此行读完而停止的,如果cha不是数字,表示因为读到非数字而停止
			{tokenName=line.substring(charNum1);GetCh();}
			else tokenName=line.substring(charNum1,charNum2);
			if((charNum2-charNum1)>nmax)
			{
				new erro(lineNum,8);
				errorNumber++;
				tokenName=tokenName.substring(0,nmax);
			}
			return  new token(tokenName,"number",lineNum);
		}
		else if(Cha==':')
		{
			GetCh();
			if(Cha=='='){
						    GetCh();
			  return new token(":=","becomes",lineNum);

		    }
			else
			return new token(":","null",lineNum);
		}
		else if(Cha=='<')
		{
			GetCh();
			if(Cha=='='){
						GetCh();
			return new token("<=","leg",lineNum);

		}
			else
			return new token("<","lss",lineNum);
		}
		else if(Cha=='>')
		{
			GetCh();
			if(Cha=='='){
					  GetCh();
			return new token(">=","geq",lineNum);
		   }
			else
			return new token(">","gtr",lineNum);
		}
		else {
			String sym;
	    String sr=Character.toString(Cha);
			switch (Cha){
				case '+':sym="plus";break;
		    case '-':sym="minus";break;
		    case '*':sym="times";break;
		    case '/':sym="slash";break;
		    case '(':sym="lparen";break;
		    case ')':sym="rparen";break;
		    case '=':sym="eql";break;
		    case '#':sym="neq";break;
		    case ',':sym="comma";break;
		    case '.':sym="period";break;
		    case ';':sym="semicolon";break;
		    default: sym="nul";
		    }
		    GetCh();
		    return new token(sr,sym,lineNum);
		  }
	}
}		

⌨️ 快捷键说明

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