📄 accanalyse.java
字号:
package jywhu;
import java.io.*;
import java.util.*;
public class AccAnalyse {
private ArrayList al;
private static int i=0;
private static final String[] reserveWord={"if","else","while","read","write","int","real"};
private String str;
private static int lineCount=0;
private int index=0;
private String token;
public AccAnalyse(String str){
this.str=str;
al=new ArrayList();
}
/*public ArrayList getList(){
return al;
}*/
public void analyse(){
File f=new File(str);
try{
if(!f.exists()){
System.out.println("源文件不存在");
}else{
BufferedReader br=new BufferedReader(new FileReader(f));
String s;
//开始词法分析
while((s=br.readLine())!=null){
lineCount++;
//过滤行字符串
s=s.trim();
System.out.println(lineCount + ":" + s);
char[] c=s.toCharArray();
while(index<c.length){
token=" ";
char ch=this.getChar(c);
switch(ch){
//此字符为字母
case 'a':case 'b':case 'c':case 'd':case 'e':case 'f':case 'g':case 'h':case 'i':case 'j':case 'k':case 'l':
case 'm':case 'n':case 'o':case 'p':case 'q':case 'r':case 's':case 't':case 'u':case 'v':case 'w':case 'x':
case 'y':case 'z':case 'A':case 'B':case 'C':case 'D':case 'E':case 'F':case 'G':case 'H':case 'I':case 'J':
case 'K':case 'L':case 'M':case 'N':case 'O':case 'P':case 'Q':
case 'R':case 'S':case 'T':case 'U':case 'V':case 'W':case 'X':case 'Y':case 'Z':
token=this.contact(token, ch);
if(index==c.length){
//System.out.println(" " + " " + lineCount + ": " + "ID, name=" + token );
al.add(new Token("id",token,lineCount));
break;
}else{
ch=this.getNextChar(c);
while(this.isLetter(ch) || this.isDigit(ch) || ch=='_'){
token=this.contact(token, ch);
if(index==c.length)
break;
else
ch=this.getNextChar(c);
}
}
if(index<=c.length) this.retract();
//判断此字符串为标识符
token=token.trim();
if(token.charAt(token.length()-1)=='_'){
System.out.println(" " + " " + lineCount + ":" +
"[ERROR]在此行发现非法的标识符!" + " " + token);
}else{
//此字符串为保留字
if(this.isReserve(token)){
//System.out.println(" " + " " + lineCount + ": " + "reserved word:" + token);
al.add(new Token(token,token,lineCount));
}else{
//System.out.println(" " + " " + lineCount + ": " + "ID, name=" + token );
al.add(new Token("id",token,lineCount));
}
}
token=" ";
break;
//此字符为数字
case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9':
token=this.contact(token, ch);
if(index==c.length){
//System.out.println(" " + " " + lineCount + ": " + "NUM, val=" + token );
al.add(new Token("num",token,lineCount));
break;
}else{
ch=this.getNextChar(c);
while(this.isDigit(ch) || ch=='.'){
token=this.contact(token, ch);
if(index==c.length)
break;
else
ch=this.getNextChar(c);
}
}
if(index<=c.length) this.retract();
int count=0;
for(int i=0;i<token.length();i++){
if(token.charAt(i)=='.'){
count++;
}
}
//判断此字符串为数字
if(count==0){
//System.out.println(" " + " " + lineCount + ": " + "NUM, val=" + token );
al.add(new Token("intNum",token,lineCount));
}else if(count==1){
al.add(new Token("realNum",token,lineCount));
}else{
System.out.println(" " + " " + lineCount + ":" +
"[ERROR]在此行发现非法的数字!" + " " + token);
}
token=" ";
break;
case '=':
token=this.contact(token, ch);
ch=this.getNextChar(c);
if(ch=='='){
index++;
token=this.contact(token, ch);
// System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("relop",token,lineCount));
}else{
//System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("equalop",token,lineCount));
}
this.retract();
token=" ";
break;
case '+':
token=this.contact(token, ch);
//System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("addop",token,lineCount));
token=" ";
break;
case '*':
token=this.contact(token, ch);
//System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("mulop",token,lineCount));
token=" ";
break;
case '(':
token=this.contact(token, ch);
//System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("leftbracket",token,lineCount));
token=" ";
break;
case ')':
token=this.contact(token, ch);
// System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("rightbracket",token,lineCount));
token=" ";
break;
case '{':
token=this.contact(token, ch);
// System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("bleftbracket",token,lineCount));
token=" ";
break;
case '}':
token=this.contact(token, ch);
//System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("brightbracket",token,lineCount));
token=" ";
break;
case '[':
token=this.contact(token, ch);
//System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("mleftbracket",token,lineCount));
token=" ";
break;
case ']':
token=this.contact(token, ch);
// System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("mrightbracket",token,lineCount));
token=" ";
break;
case '<':
token=this.contact(token, ch);
ch=this.getNextChar(c);
if(ch=='>'){
index++;
token=this.contact(token, ch);
//System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("relop",token,lineCount));
}else{
//System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("relop",token,lineCount));
}
this.retract();
token=" ";
break;
case '-':
token=this.contact(token, ch);
// System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("addop",token,lineCount));
token=" ";
break;
case '/':
token=this.contact(token, ch);
//System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("mulop",token,lineCount));
token=" ";
break;
case ',':
token=this.contact(token, ch);
//System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("comma",token,lineCount));
token=" ";
break;
case ';':
token=this.contact(token, ch);
// System.out.println(" " + " " + lineCount + ":" + token );
al.add(new Token("semicolon",token,lineCount));
token=" ";
break;
default:
token=this.contact(token, ch);
System.out.println(" " + " " + lineCount + ":" +
"[ERROR]在此行发现不能识别的单词,此行分析终止!" + " " + token);
token=" ";
break;
}
}
index=0;
}
}
}catch(Exception e){
e.printStackTrace(); }
}
//得到一个字符
public char getChar(char[] c){
try{
while((c[index]) == ' '){
index++;
}
index++;
}catch(Exception e){
e.printStackTrace();
}
return c[index-1];
}
//得到先前字符的后一个字符
public char getNextChar(char[] c){
index++;
return c[index-1];
}
public boolean isLetter(char ch){
return Character.isLetter(ch);
}
public boolean isDigit(char ch){
return Character.isDigit(ch);
}
//判断此字符串是否为保留字
public boolean isReserve(String token){
for(int i=0;i<reserveWord.length;i++){
if(token.equals(reserveWord[i])){
return true;
}
}
return false;
}
public String contact(String token,char ch){
String tmp=token + String.valueOf(ch);
token=tmp;
return token;
}
public void retract(){
index--;
}
public Token getToken(){
i=i+1;
return (Token)al.get(i-1);
}
//判断arraylist是否操作越界
public boolean isOut(){
if(i<al.size())
return true;
else
return false;
}
}
class Token implements Serializable{
private String tokenType;
private String token;
private int lineno;
public Token(String tokenType,String token,int lineno){
this.tokenType=tokenType;
this.token=token;
this.lineno=lineno;
}
public String getTokenType(){
return tokenType;
}
public String getToken(){
return token;
}
public int getLineno(){
return lineno;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -