>
Java实现词法分析
2021-06-28 16:56
JAVA
  • 5721
  • 527
  • 64
  • 51

Java实现词法分析:

一、 实验目的

设计、编制并调试一个词法分析程序,加深对词法分析原理的理解。

二、 实验内容

(1)关键字:所有的关键字都是小写

(2)运算符和界符::= + - * / < <= <> > >= = ; ( ) #

(3)其他单词是标识符和整型常数

(4)词法分析的内容:int num,str;

三、代码实现

public static TableResult<data> TResult=new TableResult();
public static List<data> tok=new ArrayList<>();
public static boolean ifier=false;
    char w[]=new char[1000];//保存有效字符
    String rwtab[]={" ","main","if","then","while","do","static","int","double","struct",
                    "break","else","long","switch","case","typedef","char","return","const",
                    "float","short","continue","for","void","sizeof","end","true"};
    String fileAddress;
    char rd[]=new char[1000];
    FileReader fr=null;
    int n,m=0,t;
    analyzeWord(String fileAddress)
    {
        this.fileAddress=fileAddress;
       // this.t=t;
    }
    
    public void clear()
    {
        int i,j=0;
        for(i=0;i<t;i++)
        {
            if(rd[i]==' ')//去掉多余空格
            {
                if (w[j-1]!=' ') {
                    w[j++]=' ';
                }
                while(rd[++i]==' ')
                {
                }
                i--;
            }
            else if(rd[i]=='\t') //去掉制表符
            {
                if (w[j-1]!=' ') {
                    w[j++]=' ';
                }
                while(rd[++i]=='\t')
                {
                }
                i--;
            }
            else if(rd[i]=='\n') //去掉换行
            {
                if (w[j-1]!=' ') {
                    w[j++]=' ';
                }
                while(rd[++i]=='\n')
                {
                }
                i--;
            }
            else if(rd[i]=='/' && rd[i+1]=='/')   //去掉注释
            {
                while (rd[++i]!='\n')
                {
                }
                i--;
            }
            else if(rd[i]=='\r')   //去掉回车
            {
                if (w[j-1]!=' ') {
                    w[j++]=' ';
                }
            //    while (r[++i]!='\n')
            //    {
            //    }
            //    i--;
            }
            else{
                w[j++]=rd[i];    //其他字符
            }
        }
    }
    public void readChar()
    {
        try {
          fr=new FileReader(fileAddress);
              int n;
              while ((n=fr.read(rd))!=-1)
              {
                
                  //将文本数据读入字符数组
                  t=n;
              }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fr.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    public void analyze()
    {
    tok.clear();
        int i,j=0;
        String syn="";
        int c=0;
        char token[];
        char r[];
        String rds=new String(w).trim();
        r=rds.toCharArray();
        System.out.println(r);
        for(i=0;i<r.length;i++)
        {
        
            token = new char[100];
            if((r[i]>='A'&& r[i]<='Z') || (r[i]<='z' && r[i]>='a'))
            {
                while ((r[i]>='A'&& r[i]<='Z') || (r[i]<='z' && r[i]>='a') ||(r[i]>='0'&&r[i]<='9')||r[i]==',')
                {
                
                    token[j++]=r[i];
                    i++;
                    if(i>=r.length) {
                    break;
                    }
                    
                    
                }
                i--;
                syn="未定义";     //先将syn赋值
                String temp=new String(token).trim();
                if(temp.length()==1) {
                  if((r[i]>='A'&& r[i]<='Z') || (r[i]<='z' && r[i]>='a')) 
                      {
                          syn="字母";
                      }
                }
                if(ifier) {
                ifier=false;
                 syn="标识符";
                }
                for(j=1;j<rwtab.length;j++)
                {
                    if(temp.equals(rwtab[j]))
                    {
                        syn="关键词";  //如果存在关键字则重新赋值
                        ifier=true;
                        break;
                    }
                }
               
               
            }//识别字母开头的字母数字串
            else if(r[i]>='0'&&r[i]<='9')
            {
            System.out.println(ifier);
                if(ifier) {
                ifier=false;
                   token[j++]=r[i];
                syn="未能识别标识符";
                
                }else { 
                while ((r[i]>='0'&&r[i]<='9') )
                {
                    token[j++]=r[i];
                    i++;
                }
                syn="数字";
                i--;
                }
            }//识别数字NUM
            else if(r[i]==' ')
            {
                continue;   //识别空格并直接进入下一次循环
            }
            else if(r[i]=='*'&&r[i+1]=='*') //识别**
            {
                token[j++]='*';
                token[j]='*';
                syn="运算符**";
                i++;
            }
            else if(r[i]=='='&&r[i+1]=='=') //识别==
            {
                token[j++]='=';
                token[j]='=';
                syn="运算符==";
                i++;
            }
            else if(r[i]=='<'&&r[i+1]=='>') //识别<>
            {
                token[j++]='<';
                token[j]='>';
                syn="运算符<>";
                i++;
            }
            else if(r[i]=='<'&&r[i+1]=='=') //识别<=
            {
                token[j++]='<';
                token[j]='=';
                i++;  syn="运算符<=";
            }
            else if(r[i]=='='&&r[i+1]=='>') //识别>=
            {
                token[j++]='>';
                token[j]='=';
                syn=">=";
                i++;
            }
            else {  //其他符号
                token[j]=r[i];
                switch (token[j])
                {
                    case '+':
                      syn="运算符+";
                        break;
                    case '-':
                      syn="运算符-";
                        break;
                    case '*':
                      syn="运算符*";
                        break;
                    case '/':
                      syn="运算符/";
                        break;
                    case '<':
                      syn="运算符<";
                        break;
                    case '>':
                      syn="运算符>";
                        break;
                    case '=':
                      syn="运算符=";
                        break;
                    case '[':
                      syn="操作符[";
                        break;
                    case ']':
                      syn="操作符]";
                        break;
                    case ';':
                      syn="分隔符;";
                        break;
                    case '(':
                      syn="分隔符(";
                        break;
                    case ')':
                      syn="分隔符)";
                        break;
                    case '#':
                      syn="分隔符#";
                        break;
                    case '}':
                    syn="分隔符}";
                      break;
                    case '{':
                    syn="分隔符{";
                      break;
                    default:
                      syn="未定义";
                        break;
                }
            }
            j=0;
            c++;
            data s = new data();
            String tokens = new String(token);
            s.setPosition(i+1+"");
            if ("未定义".equals(syn))
            {
                s.setTypes("无法识别该字符!");
            }
            else {
                s.setTypes(syn);
               
            }
            if(tokens.indexOf(",")>=0) {
            tokens=tokens.replace(","," ");
               // s.setTypes("关键词");
            }
           
            s.setCode(tokens);
            tok.add(s);
            if("未能识别标识符".equals(syn)) {
            break;
            }
        }
      
        //System.out.println("词法分析完毕!");
    }
 

四、实验结果:

词法分析结果.png

如需要源码的我也上传在资源中啦:词法分析源码


总结

感谢各位博友的阅读 ,欢迎您们提出意见 让我们一起交流。感谢你们对CYBLOG的支持理解,本次讲解到此结束!

全部留言 ()
返回
顶部