Java 实现词法分析器(编译原理)

时间:2022-03-02 16:47:03

不多说,直接贴代码

package edu.software.compile;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
* 1 基本字
* 2 标识符
* 3 常数
* 4 算符
* 5 界符
*/

public class Compile2 {

public int ch;
public int code;//保留字状态码

public StringBuffer strToken = new StringBuffer();//存放构成单词符号的字符串

public String [] retainWord = new String[]{"int","if","else","return","main","void","while","break"};//保留字

//判断是否是字母
public boolean IsLetter(){
if((ch>=65 && ch <= 90) || (ch >= 97 && ch <=122)){
return true;
}
return false;
}

//判断是否是数字
public boolean IsDigit(){
if(ch>=48 && ch <= 57){
return true;
}
return false;
}

//判断是否是空格
public boolean IsBC(int ch){
if(ch == 32){
return true;
}
return false;
}

//连接字符
public void Concat(char ch){
strToken.append(ch);
}

//判断是否是保留字
public int Reserve(){
for(int i = 0;i < retainWord.length;i++){
if(strToken.toString().equals(retainWord[i])){
return 1;
}
}
if(strToken.length() != 0){
if(strToken.charAt(0)>='0' && strToken.charAt(0)<='9'){
return 3;
}
}

return 2;
}

//
public void Retract(){
code = Reserve();
if(code == 1){
System.out.println("('"+1+"','"+strToken+"')");
}else if(code == 2){
System.out.println("('"+2+"','"+strToken+"')");
}
else if(code == 3){
System.out.println("('"+3+"','"+strToken+"')");
}
strToken.delete(0, strToken.length());
}

/**
* 读取文件
*/
public void scanner(){
BufferedReader br;
try {
br = new BufferedReader(new FileReader("tests.txt"));
while((ch = br.read()) != -1){
//System.out.println("======="+(char)ch);
if(IsBC(ch) == false){
if(IsLetter()){
if(IsLetter() == true || IsDigit() == true){
Concat((char) ch);
}
}else if(IsDigit() == true){
Concat((char)ch);
}else if(IsDigit()){
Concat((char) ch);
}else if(ch == 61){
if((strToken.length() != 0 )&& (strToken.charAt(0) == '=')){
strToken.append((char)ch);
System.out.println("('"+4+"','"+strToken+"')");
strToken.delete(0, strToken.length());
}else{
strToken.append((char)ch);
}
}else if(ch == 43){
Retract();
System.out.println("('"+4+"','"+(char) ch+"')");
}else if(ch == 45){
Retract();
System.out.println("('"+4+"','"+(char) ch+"')");
}else if(ch == 42){
Retract();
System.out.println("('"+4+"','"+(char) ch+"')");
}else if(ch == 47){
Retract();
System.out.println("('"+4+"','"+(char) ch+"')");
}else if((char) ch == ';'){
Retract();
System.out.println("('"+5+"','"+(char) ch+"')");
}else if((char) ch == '('){
Retract();
System.out.println("('"+5+"','"+(char) ch+"')");
}else if((char) ch == ')'){
Retract();
System.out.println("('"+5+"','"+(char) ch+"')");
}else if((char) ch == '{'){
Retract();
System.out.println("('"+5+"','"+(char) ch+"')");
}else if((char) ch == '}'){
Retract();
System.out.println("('"+5+"','"+(char) ch+"')");
}else if((char) ch == ','){
Retract();
System.out.println("('"+5+"','"+(char) ch+"')");
}

}else{
Retract();
}

}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args) {
Compile2 compile2 = new Compile2();
compile2.scanner();
}

}

测试文件1

int main(int x){
int a = 10;
int b = 2;
int c = 0;
int d1 ;
if(c == 0){
c = a + b;
}
return c;
}

输出1

('1','int')
('1','main')
('5','(')
('1','int')
('2','x')
('5',')')
('2','')
('5','{')
('1','int')
('2','a')
('2','=')
('3','10')
('5',';')
('1','int')
('2','b')
('2','=')
('3','2')
('5',';')
('1','int')
('2','c')
('2','=')
('3','0')
('5',';')
('1','int')
('2','d1')
('2','')
('5',';')
('1','if')
('5','(')
('2','c')
('4','==')
('2','')
('3','0')
('5',')')
('2','')
('5','{')
('2','c')
('2','=')
('2','a')
('2','')
('4','+')
('2','')
('2','b')
('5',';')
('2','')
('5','}')
('1','return')
('2','c')
('5',';')
('2','')
('5','}')

测试文件2

void ss(){
while(1){
break;
}
}

输出2

('1','void')
('2','ss')
('5','(')
('2','')
('5',')')
('2','')
('5','{')
('1','while')
('5','(')
('3','1')
('5',')')
('2','')
('5','{')
('1','break')
('5',';')
('2','')
('5','}')
('2','')
('5','}')