📄 lexicalanalyer.java
字号:
package com.lxx.compiler;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.lang.reflect.Array;
class TokenValue
{
static int KEY=1;
static int SYMBOL=2;
static int CONST=3;
static int OPERATOR=4;
static int LIMIT=5;
}
class Token
{
public int tokenValue;
public String stringValue;
int numValue;
}
class LexicalAnalyer {
private String key[]; //关键字表
private String limitAndOperator[]; //界符和操作符表
private int limitEndIndex; //界符和操作符表表示界符的下标上限,也就是说下标大于这个就表示操作符
private LinkedList tokenTable;//记号表
private LinkedList symbolTable;//符号表
private LinkedList constTable;//常数表
public BufferedReader inputReader;//读文件Reader
public String currentLine;//当前工作的行的字符串
private int startIndex;//在当前工作行的工作起始索引
private int currentIndex;//当前索引
public LexicalAnalyer(String inputFile,String keyFile,String limitFile,String operatorFile) throws Exception
{
inputReader=new BufferedReader(new FileReader(inputFile));
symbolTable=new LinkedList();
constTable=new LinkedList();
tokenTable=new LinkedList();
ReadKeyFromFile(keyFile);
ReadLimitAndOperator(limitFile,operatorFile);
currentLine=inputReader.readLine();
currentLine.replace('\n',' ');
currentLine.replace('\r',' ');
currentLine.replace('\t',' ');
currentLine=currentLine.trim();
startIndex=currentIndex=0;
}
//从文件读入关键字
private void ReadKeyFromFile(String filePath) throws Exception
{
BufferedReader fsReader=new BufferedReader(new FileReader(filePath));
String temp;
int lines=0;
while((temp=fsReader.readLine())!=null&&temp.trim().length()!=0)
{
lines++;
}
fsReader.close();
fsReader=new BufferedReader(new FileReader(filePath));
key=new String[lines];
for(int i=0;i<lines;i++)
{
if((temp=fsReader.readLine().trim()).length()!=0)
key[i]=temp;
}
}
//从文件读入界符和操作符
private void ReadLimitAndOperator(String filePath,String ofilePath) throws Exception
{
BufferedReader fsReader=new BufferedReader(new FileReader(filePath));
BufferedReader ofsReader=new BufferedReader(new FileReader(ofilePath));
int lines=0;
while(fsReader.readLine()!=null)
{
lines++;
}
limitEndIndex=lines;
while(ofsReader.readLine()!=null)
{
lines++;
}
limitAndOperator=new String[lines];
fsReader.close();
ofsReader.close();
fsReader=new BufferedReader(new FileReader(filePath));
ofsReader=new BufferedReader(new FileReader(ofilePath));
int i;
for(i=0;i<limitEndIndex;i++)
{
limitAndOperator[i]=fsReader.readLine().trim();
}
for(;i<lines;i++)
{
limitAndOperator[i]=ofsReader.readLine().trim();
}
fsReader.close();
ofsReader.close();
}
//获得当前流的下一个记号,若到输入流尾,返回null
public Token NextToken() throws Exception
{
int index=-1;
String word;
String tempString;
int i;
int temp;
int tempIndex;
Token token=new Token();
//去掉注释
/*去注释的算法,就是如果遇到"/*"则找寻下一个匹配的"* /",
*并把工作指针移到此处,这样,它们之间的代码就跳过不处理了
*/
if((index=currentLine.indexOf("/*",startIndex))!=-1)
{
startIndex=index+2;
index=currentLine.indexOf("*/",startIndex);
while(index==-1)
{
currentLine=inputReader.readLine();
startIndex=0;
if(currentLine==null)
return null;
index=currentLine.indexOf("*/",startIndex);
}
startIndex=index+2;
}
if(currentLine.substring(startIndex).trim().length()==0)
{
currentLine=inputReader.readLine();
if(currentLine==null)
{
inputReader.close();
return null;
}
currentLine.replace('\n',' ');
currentLine.replace('\r',' ');
currentLine.replace('\t',' ');
currentLine=currentLine.trim();
startIndex=0;
while(currentLine.length()==0)
{
currentLine=inputReader.readLine();
if(currentLine==null)
{
inputReader.close();
return null;
}
currentLine.replace('\n',' ');
currentLine.replace('\r',' ');
currentLine.replace('\t',' ');
currentLine=currentLine.trim();
startIndex=0;
}
}
if(currentLine==null)
{
inputReader.close();
return null;
}
i=65535;
temp=65535;
for(int j=0;j<limitAndOperator.length;j++)
{
index=currentLine.indexOf(limitAndOperator[j],startIndex);
if(index!=-1)
{
if(index<temp)
{
temp=index;
i=j;
}
if(index==temp&&limitAndOperator[j].length()>limitAndOperator[i].length())
{
temp=index;
i=j;
}
}
}
tempIndex=currentLine.trim().indexOf(" ",startIndex);
if(tempIndex!=-1)
{
tempString=currentLine.substring(tempIndex);
tempString=tempString.trim();
if(tempString.length()!=0)
{
tempIndex=currentLine.indexOf(tempString,startIndex);
tempIndex--;
tempString=currentLine.substring(startIndex,tempIndex).trim();
if(tempString.length()!=0)
if(tempIndex<temp)
{
temp=tempIndex;
i=65535;
}
}
}
if(temp!=65535)
index=temp;
if(currentLine.substring(startIndex,index).trim().length()==0)
startIndex=index;
if(index==startIndex&&i>limitEndIndex&&i<limitAndOperator.length)
{
token.tokenValue=TokenValue.OPERATOR;
token.stringValue=limitAndOperator[i];
token.numValue=-1;
startIndex+=limitAndOperator[i].length();
return token;
}
else if(index==startIndex&&i<=limitEndIndex)
{
token.tokenValue=TokenValue.LIMIT;
token.stringValue=limitAndOperator[i];
token.numValue=-1;
startIndex+=limitAndOperator[i].length();
return token;
}
if(index!=-1)
{
word=currentLine.substring(startIndex,index).trim();
if(word.length()==0)
{
startIndex=index;
}
startIndex=index;
}
else
{
word=currentLine.substring(startIndex).trim();
startIndex+=word.length();
}
if(isKey(word))
{
token.tokenValue=TokenValue.KEY;
token.numValue=-1;
token.stringValue=word;
return token;
}
if(isConst(word))
{
token.tokenValue=TokenValue.CONST;
token.numValue=Integer.parseInt(word);
token.stringValue=word;
return token;
}
if(isSymbol(word))
{
token.tokenValue=TokenValue.SYMBOL;
token.stringValue=word;
token.numValue=-1;
return token;
}
return null;
}
//扫描程序
public void Scan(String tokenFileName,String symbolFileName,String constFileName) throws Exception
{
Token token;
String tempString;
BufferedWriter tokenWriter=new BufferedWriter(new FileWriter(tokenFileName));
BufferedWriter symbolWriter=new BufferedWriter(new FileWriter(symbolFileName));
BufferedWriter constWriter=new BufferedWriter(new FileWriter(constFileName));
this.constTable.clear();
this.symbolTable.clear();
this.tokenTable.clear();
while((token=NextToken())!=null)
{
tokenTable.add(token);
if(token.tokenValue==TokenValue.CONST&&(!constTable.contains(token.stringValue)))
constTable.add(token.stringValue);
else if(token.tokenValue==TokenValue.SYMBOL&&(!symbolTable.contains(token.stringValue)))
symbolTable.add(token.stringValue);
}
while(!tokenTable.isEmpty())
{
token=(Token)tokenTable.remove();
tokenWriter.write(token.stringValue+" "+token.tokenValue+" "+Integer.toString(token.numValue)+"\r\n");
}
while(!symbolTable.isEmpty())
{
tempString=(String)symbolTable.remove();
symbolWriter.write(tempString+"\r\n");
}
while(!constTable.isEmpty())
{
tempString=(String)constTable.remove();
constWriter.write(tempString+"\r\n");
}
tokenWriter.close();
constWriter.close();
symbolWriter.close();
this.inputReader.close();
}
private boolean isKey(String word)
{
for(int i=0;i<key.length;i++)
{
if(word.equals(key[i]))
return true;
}
return false;
}
private boolean isSymbol(String word)
{
if(isKey(word))
return false;
if(isConst(word))
return false;
return true;
}
private boolean isConst(String word)
{
try
{
Integer.parseInt(word);
}
catch(NumberFormatException e)
{
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -