词法分析器java_java编写词法分析器

该程序是一个简单的Java词法分析器,用于读取文件中的字符并进行分类。它能识别关键字、标识符、数字、运算符、分界符,并对它们进行输出。关键词包括常见的控制语句如`break`, `include`, `begin`, `end`, `if`, `else`, `while`, `switch`等。程序还能识别字母、数字以及各种运算符和分隔符,例如括号、分号等。
摘要由CSDN通过智能技术生成

/*** 此程序是通过将文件的字符读取到字符数组中去,然后遍历数组,将读取的字符进行

* 分类并输出

*@author**/

public classWordAnalyze {private String keyWord[] = {"break","include","begin","end","if","else","while","switch"};private charch;//判断是否是关键字

booleanisKey(String str)

{for(int i = 0;i < keyWord.length;i++)

{if(keyWord[i].equals(str))return true;

}return false;

}//判断是否是字母

boolean isLetter(charletter)

{if((letter >= 'a' && letter <= 'z')||(letter >= 'A' && letter <= 'Z'))return true;else

return false;

}//判断是否是数字

boolean isDigit(chardigit)

{if(digit >= '0' && digit <= '9')return true;else

return false;

}//词法分析

void analyze(char[] chars)

{

String arr= "";for(int i = 0;i< chars.length;i++) {

ch=chars[i];

arr= "";if(ch == ' '||ch == '\t'||ch == '\n'||ch == '\r'){}else if(isLetter(ch)){while(isLetter(ch)||isDigit(ch)){

arr+=ch;

ch= chars[++i];

}//回退一个字符

i--;if(isKey(arr)){//关键字

System.out.println(arr+"\t4"+"\t关键字");

}else{//标识符

System.out.println(arr+"\t4"+"\t标识符");

}

}else if(isDigit(ch)||(ch == '.'))

{while(isDigit(ch)||(ch == '.'&&isDigit(chars[++i])))

{if(ch == '.') i--;

arr= arr +ch;

ch= chars[++i];

}//属于无符号常数

System.out.println(arr+"\t5"+"\t常数");

}else switch(ch){//运算符

case '+':System.out.println(ch+"\t2"+"\t运算符");break;case '-':System.out.println(ch+"\t2"+"\t运算符");break;case '*':System.out.println(ch+"\t2"+"\t运算符");break;case '/':System.out.println(ch+"\t2"+"\t运算符");break;//分界符

case '(':System.out.println(ch+"\t3"+"\t分界符");break;case ')':System.out.println(ch+"\t3"+"\t分界符");break;case '[':System.out.println(ch+"\t3"+"\t分界符");break;case ']':System.out.println(ch+"\t3"+"\t分界符");break;case ';':System.out.println(ch+"\t3"+"\t分界符");break;case '{':System.out.println(ch+"\t3"+"\t分界符");break;case '}':System.out.println(ch+"\t3"+"\t分界符");break;//运算符

case '=':{

ch= chars[++i];if(ch == '=')System.out.println("=="+"\t2"+"\t运算符");else{

System.out.println("="+"\t2"+"\t运算符");

i--;

}

}break;case ':':{

ch= chars[++i];if(ch == '=')System.out.println(":="+"\t2"+"\t运算符");else{

System.out.println(":"+"\t2"+"\t运算符");

i--;

}

}break;case '>':{

ch= chars[++i];if(ch == '=')System.out.println(">="+"\t2"+"\t运算符");else{

System.out.println(">"+"\t2"+"\t运算符");

i--;

}

}break;case '

ch= chars[++i];if(ch == '=')System.out.println("<="+"\t2"+"\t运算符");else{

System.out.println("

i--;

}

}break;//无识别

default: System.out.println(ch+"\t6"+"\t无识别符");

}

}

}public static void main(String[] args) throwsException {

File file= new File("E:\\data.txt");//定义一个file对象,用来初始化FileReader

FileReader reader = new FileReader(file);//定义一个fileReader对象,用来初始化BufferedReader

int length = (int) file.length();//这里定义字符数组的时候需要多定义一个,因为词法分析器会遇到超前读取一个字符的时候,如果是最后一个//字符被读取,如果在读取下一个字符就会出现越界的异常

char buf[] = new char[length+1];

reader.read(buf);

reader.close();newWordAnalyze().analyze(buf);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值