📄 cmm.java
字号:
ID.offer("Com");
output.append("\n"+"特殊符号: /");
state =done;
}else{
char b = getNext();
if(b=='*'){
state=note;
goon=true;
state =note;
}else{
output.append("\n"+"特殊符号: /");
tokens.offer(a);
ID.offer("Com");
state =done;
backup();
}
}
}else if (a=='='){
if(!finish){
char b = getNext();
if(b=='='){
tokens.offer("==");
ID.offer("FZ");
output.append("\n"+"特殊符号: ==");
state=done;
}else{
tokens.offer(a);
ID.offer("Com");
output.append("\n"+"特殊符号: =");
state=done;
backup();
}
}else{
tokens.offer(a);
ID.offer("Com");
output.append("\n"+"特殊符号: =");
state=done;
}
}else if (Character.isDigit(a)){
if(finish){
tokens.offer(a);
ID.offer("Com");
output.append("\n"+"num: "+a);
state=done;
}
else
state = num;
}else if(a=='<'){
if(!finish){
char b = getNext();
if(b=='>'){
tokens.offer("<>");
ID.offer("PD");
output.append("\n"+"特殊符号: <>");
state=done;
}else{
tokens.offer(a);
ID.offer("PD");
output.append("\n"+"特殊符号: <");
state=done;
backup();
}
}else{
tokens.offer(a);
ID.offer("PD");
output.append("\n"+"特殊符号: <");
state=done;
}
}else if(Character.isLetter(a)){
if(!finish){
state=id;
}else{
tokens.offer(a);
ID.offer("ID");
output.append("\n"+"标示符: "+a);
state=done;
}
}else{
errorA= true;
output.append("\n"+"错误 未知符号:"+a);
state=done;
}
break;
case 1:
break;
case 2:
if(a=='*'&&finish==false){
char b = getNext();
if(b=='/'){
state=done;
output.append("\n"+"注释");
}
break;
}else if (a=='*'&&finish==true){
errorA=true;
state =done;
output.append("\n"+"错误:注释缺少*/");
}else if(finish){
errorA=true;
state =done;
output.append("\n"+"错误:注释缺少*/");
}else if(a=='\n')
{
tokens.offer(a);
ID.offer("FG");
col++;
output.append("\n"+"第"+col+"行:"+line[col-1]);
}
break;
case 3:
break;
case 4:
if (a==' '||a=='\n'||a=='+'||a=='-'||a=='*'||a=='/'||a=='('||a==')'||a==';'||a=='='||a=='<'||a=='{'||a=='}'||a=='['||a==']'||a=='\r'){
backup();
end=pos;
currentToken=inPut.substring(begin,end);
errorA=true;
output.append("\n"+"错误: 非法符号"+currentToken);
state=done;
if(a=='\r'||a=='\n')
pos++ ;
}else if(finish){
end=pos;
currentToken=inPut.substring(begin,end);
errorA=true;
output.append("\n"+"错误: 非法符号"+currentToken);
state=done;
}
break;
case 5:
if(Character.isDigit(a)&&finish==false){
}else{
if (a==' '||a=='\n'||a=='+'||a=='-'||a=='*'||a=='/'||a=='('||a==')'||a==';'||a=='='||a=='<'||a=='{'||a=='}'||a=='['||a==']'||a=='\r'){
backup();
end=pos;
currentToken=inPut.substring(begin,end);
if(currentToken.endsWith(".")){
currentToken=inPut.substring(begin,end-1);
backup();
}
tokens.offer(currentToken);
ID.offer("DIG");
output.append("\n"+"num: "+currentToken);
state=done;
}else if (Character.isDigit(a)&&finish==true){
end=pos;
currentToken=inPut.substring(begin,end);
tokens.offer(currentToken);
ID.offer("DIG");
output.append("\n"+"num: "+currentToken);
state=done;
}
else if(a=='.'){
if(!decimal&&!finish){
decimal=true;
}
else if(decimal){
end=pos-1;
currentToken=inPut.substring(begin,end);
tokens.offer(currentToken);
ID.offer("DIG");
output.append("\n"+"num: "+currentToken);
backup();
state = done;
} if(!decimal&&finish){
end=pos;
currentToken=inPut.substring(begin,end-1);
backup();
tokens.offer(currentToken);
ID.offer("DIG");
output.append("\n"+"num: "+currentToken);
state = done;
finish=false;
}
}else{
if(finish){
end=pos;
currentToken=inPut.substring(begin,end);
errorA =true;
output.append("\n"+"错误: 非法符号"+currentToken);
state=done;
}else{
state=error;
}
}
}
break;
case 6:
if(Character.isLetter(a)||a=='_'||Character.isDigit(a)){
if(finish){
end=pos;
currentToken=inPut.substring(begin,end);
if(currentToken.endsWith("_")){
errorA = true;
output.append("\n"+"非法标示符(不能以_结尾): "+currentToken);
state = done;
}else{
isKW(currentToken);
state = done;
}
}
} else if (a==' '||a=='\n'||a=='+'||a=='-'||a=='*'||a=='/'||a=='('||a==')'||a==';'||a=='='||a=='<'||a=='{'||a=='}'||a=='['||a==']'||a=='\r'){
backup();
end=pos;
currentToken=inPut.substring(begin,end);
if(currentToken.endsWith("_")){
errorA = true;
output.append("\n"+"非法标示符(不能以_结尾): "+currentToken);
state = done;
}else{
isKW(currentToken);
state = done;
}
}else{
state=error;
}
break;
case 7:
break;
}
}
}
public static void main(String[] args) {
new Cmm();
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="open"){
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
BufferedReader bufferedreader = new BufferedReader(
new FileReader(file));
String s1= bufferedreader.readLine();
for (String s2 = bufferedreader.readLine(); s2 != null; s2 = bufferedreader
.readLine()) {
s1=s1+"\n"+s2;
}
input.setText(s1);
output.setText("");
} catch (FileNotFoundException abc) {
output.setText("文件不存在");
} catch (IOException io) {
output.setText("文件格式错误");
}
} else {
}
}else if (e.getActionCommand()=="clean"){
input.setText("");
output.setText("");
}else if (e.getActionCommand()=="analyse"){
errorA = false;
read();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -