c词法分析程序(java实现)原代码3

本文介绍了一个基于C语言的词法分析器实现过程。该分析器能够从源文件中读取并解析出关键字、标识符、整型常数、实型常数、字符串常数等元素,并对非法字符进行标记。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package wordanalyse;

import wordanalyse.*;
import java.util.Vector;
public class MainControl {

 private InputFile inputFile;
 private OutPutFile outputFile;
 private Vector temp=new Vector();
 private static final int LENGTHOFKEYWORDS=32;/*关键字或者标识符的长度。*/
 private static final int LENGTHOFINT=32; /*整型数据的长度。*/
 private static final int LENGTHOFSTRING=1024;/*字符串常量长度。*/
 /**
  * @param args
  */
 public MainControl()
 {
  inputFile=new InputFile();
  inputFile.getPathFromKeys();
  if(inputFile.getPath()==null)  /*如果没有获得路径就使用默认路径。*/
   inputFile.setPath("D:\\Documents and Settings\\Administrator\\workspace\\WordAnalyse\\wordanalyse\\demo.c");
  outputFile=new OutPutFile(inputFile.getPath());
 }
 public void control()
 {
  String content=inputFile.getContentFromFile();
  String result[]=new String[2];
  result[1]=content;
  int counter=0;
  do{
   result=inputFile.getOneLineString(result[1]);
   if(result!=null&&result[0]!=null)
   {
    counter++;
    System.out.println("读入的第"+counter+"行是:"+result[0]);
    process(result[0]);
   }
  }while(result!=null); 
 }
 public void process(String oneLineString)
 {
  byte temp[];
  byte[] word=new byte[LENGTHOFKEYWORDS],number=new byte[LENGTHOFINT],symbol=new byte[32],string=new byte[LENGTHOFSTRING];
  temp=oneLineString.getBytes();
  for(int i=0;i   {
   if(temp[i]!=' '&&temp[i]!='\t') /*滤掉空格、制表键。*/
   {
    if((temp[i]>=65&&temp[i]<=90)
      ||(temp[i]>=97&&temp[i]<=122)
      ||temp[i]=='_')
    {
     int j=-1;
     do{
      if(j       {
       j++;
       word[j]=temp[i];
      }
      i++;
      if(i>=temp.length) /*如果已经超过界限就终止循环。*/
       return;
     }while((temp[i]>=65&&temp[i]<=90)
       ||(temp[i]>=97&&temp[i]<=122)
       ||temp[i]=='_'
       ||(temp[i]>=48&&temp[i]<=57));
     String checkdWord=new String(word,0,j+1);
     if(KeyWord.isKeyWord(checkdWord))
      this.temp.addElement(checkdWord+"             关键字\n");
     else this.temp.addElement(checkdWord+"             标识符\n");
     i--;    //将指针向前移一个,因为上面是已经移动到后面一个才结束的。而for循环还要++,否则将无法检察当前字符。
    }
    else
    {
     if(temp[i]>=48&&temp[i]<=57)      //如果是数字。
     {
      int k=-1;
      do{
       k++;
       number[k]=temp[i];
       i++;
       if(i>=temp.length)  //如果已经超过界限就终止循环。
        return;
      }while(temp[i]>='0'&&temp[i]<='9'||temp[i]=='.');
      String checkdNumber=new String(number,0,k+1);
      if(hasDot(checkdNumber))
      {  
       try{
       double num=Double.parseDouble(checkdNumber);
       this.temp.addElement(num+"             实型常数\n");
       }
       catch(Exception e)
       {
        this.temp.addElement(checkdNumber+"             一个错误\n");
       }
      }
      else
      {
       try{
        long num=Long.parseLong(checkdNumber);
        this.temp.addElement(num+"             整型常数\n");
       }
       catch(Exception e)
       {
        this.temp.addElement(checkdNumber+"             一个错误\n");
       }
      }
      i--;    //将指针向前移一个,因为上面是已经移动到后面一个才结束的。而for循环还要++,否则将无法检察当前字符。
     }
     else if(Symbol.isLimitedSymble(new String(temp,i,1)))
     {
      if(temp[i]=='"')    //过滤掉字符串常数。
      {
       int n=-1;
       do{
        n++;
        string[n]=temp[i];
        i++;
        if(i>=temp.length)  //如果已经超过界限就终止循环。
        {
         this.temp.addElement(new String(string,0,n+1)+"             一个错误\n");
         return;
        }
       }while(temp[i]!='"');
       n++;    
       string[n]='"';     //要把最后一个"给过滤掉,所以n要向后移一个位置。
       String checkdNumber=new String(string,0,n+1);
       this.temp.addElement(checkdNumber+"             字符串常数\n");
      }
      else if(temp[i]=='\'')    /*过滤掉字符常数。*/
      {
       int n=-1;
       do{
        n++;
        string[n]=temp[i];
        i++;
        if(i>=temp.length)  /*如果已经超过界限就终止循环。*/
        {
         this.temp.addElement(new String(string,0,n+1)+"             一个错误\n");
         return;
        }
       }while(temp[i]!='\'');
       n++;    
       string[n]='\'';     /*要把最后一个"给过滤掉,所以n要向后移一个位置。*/
       String checkdNumber=new String(string,0,n+1);
       if(n+1<=3)     /*c语言语法规定字符常量的字符数只能是1,再加上2个’,刚好应该是3。*/
        this.temp.addElement(checkdNumber+"             字符常数\n");
       else
        this.temp.addElement(checkdNumber+"             一个错误!\n");
      }
      else{
       this.temp.addElement(new String(temp,i,1)+"             界符\n"); /*过滤掉界符。*/
      }
     }
     else if(Symbol.isSingleSymble(new String(temp,i,1)))
     {
      int m=-1;
      do{
       m++;
       if(m>=2)
       {
        this.temp.addElement(new String(symbol,0,m+1)+"             一个错误\n");
        break;
       }
       symbol[m]=temp[i];
       i++;
       if(i>=temp.length)  /*如果已经超过界限就终止循环。*/
        return; 
      }while(Symbol.isSingleSymble(new String(temp,i,1)));
      String checkdWord=new String(symbol,0,m+1);
      if(Symbol.isCalSymbol(checkdWord))
       this.temp.addElement(checkdWord+"             运算符\n");
      else if(checkdWord.equals("/*")) /*将注释给滤掉*/
      {
       do
       {
        i++;
        if(i>=temp.length)  /*如果已经超过界限就终止循环*/
         return; 
       }while(temp[i]!='*');
       i++;
       if(i>=temp.length)  /*如果已经超过界限就终止循环。*/
        return; 
       if(temp[i]=='/');
       else this.temp.addElement("注释             缺少结束符'/'\n");
      }
      else
       this.temp.addElement(checkdWord+"             一个错误\n");
      i--;    /*将指针向前移一个,因为上面是已经移动到后面一个才结束的。而for循环还要++,否则将无法检察当前字符。*/
     }
     else {
      this.temp.addElement(temp[i]+"             非法字符\n");
     }
    }
   }
  }
 }
 /*
  * 检查字符串中是否有小数点,以便于判断是否是浮点数。
  */
 public boolean hasDot(String checkdString)
 {
  byte []temp=checkdString.getBytes();
  for(int i=0;i    if(temp[i]==46)
    return true;
  return false;
 }
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String result[];
  MainControl mainControl=new MainControl();
  //mainControl.inputFile.getPathFromKeys();
  mainControl.control();
  result=new String[mainControl.temp.size()];
  for(int i=0;i   {
   result[i]=(String)mainControl.temp.elementAt(i);
   System.out.print(result[i]);
  }
  mainControl.outputFile.writeToFile(result);
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值