📄 tttest.java
字号:
import java.io.*;import java.util.*;public class TTTest{ public static void main(String[] args) { System.out.println("符号表文件会输出在一个叫table.dat的文件中"); System.out.println("二元式文件会输出在一个叫TableTwoNUM.dat的文件中"); System.out.println("错误文件会输出在一个叫wrongFile.dat的文件中"); System.out.println("请输入要解析文件的名字"); try{ BufferedReader reader = new BufferedReader( new InputStreamReader(System.in) ); String name = reader.readLine(); Table myTable = new Table(); TokenGet tokenGet= new TokenGet(name,myTable); tokenGet.putBuffer(); String s; while(!TokenGet.endOfFenX) { s= tokenGet.nextToken(); System.out.println( s ); System.out.println("识别出一个表示符"); } myTable.printTable(); }catch(Exception e){e.printStackTrace();} }} class TokenGet{ public TokenGet( String name ,Table aTable) { endOfFenX= false; name = name.trim(); table = aTable; try { wrongFile = new PrintWriter( new FileWriter("WrongFile.dat")); in = new BufferedReader( new FileReader( name ) ); } catch(IOException e) { e.printStackTrace();} } String nextToken() { String theid; isDouble=false; begining=ending; System.out.println("beginning="+begining);// System.out.println("ending="+ending);// char c; state=0; //at the begining while(true) { System.out.println("beginning="+begining); System.out.println("ending="+ending); switch( state ) { //识别空白t case -1: return "WWWWWW"; case 0: c=nextChar(); if( c==' '||c=='\t'||c=='\n' ) { state = 1; System.out.println("black1"); } else state=fail(); break; case 1: c=nextChar(); if( c==' '||c=='\t' ) { System.out.println("black2"); state = 1; } else // text begining { state=2; } break; case 2: state = 3; ungetChar(); break; //识别标识符 id case 3: c= nextChar(); if( isLetter(c) ) { state=4; } else state = fail(); System.out.println("State 3"); break; case 4: c= nextChar(); if( isLetter(c) || isDigit(c) ) { state=4; } else state=5; System.out.println("State 4"); break; case 5: ungetChar(); System.out.println("beginning="+begining); System.out.println("ending="+ending); theid=buffer.substring(begining,ending); table.insert( theid , theid.length() ,"","",""); System.out.println("State 5"); return "the id"; //识别括号和; case 6: c=nextChar(); if(c=='(') { state = 7; System.out.println("ok"); } else if( (c==')') ) state=8; else if( (c=='{') ) state=9; else if( (c=='}') ) state=10; else if( (c==';') ) state=11; else state=fail(); System.out.println("State 6"); break; case 7: System.out.println("括号1"); theid = buffer.substring(begining,ending); table.insert(theid,1000,"","",""); return "("; case 8: System.out.println("括号2"); theid = buffer.substring(begining,ending); table.insert(theid,1000,"","",""); return ")"; case 9: System.out.println("括号3"); theid = buffer.substring(begining,ending); table.insert(theid,1000,"","",""); return "{"; case 10: System.out.println("括号4"); theid = buffer.substring(begining,ending); table.insert(theid,1000,"","",""); return "}"; case 11: System.out.println("括号5"); theid = buffer.substring(begining,ending); table.insert(theid,1000,"","",""); return ";"; //识别NUMBER case 12: c=nextChar(); if( isDigit(c) ) state=13; else state=fail(); System.out.println("state 12"); break; case 13: c=nextChar(); if( isDigit(c) ) state=13; else if( (c=='.') ) { isDouble=true; state=14; } else if(isLetter(c)) {getWrong(); state=16;} else state=16; System.out.println("state 13"); break; case 14: c=nextChar(); if( isDigit(c) ) state=15; else if( isLetter(c) ) getWrong(); System.out.println("state 14"); break; case 15: c=nextChar(); if( isDigit(c) ) state=15; else if(isLetter(c)) { getWrong();state=16; } else state= 16; System.out.println("state 15"); break; case 16: ungetChar(); theid=buffer.substring(begining,ending); if(isDouble()) table.insert( "" , 1000 ,"实型","常数",theid); else table.insert( "" , 1000 ,"整型","常数",theid); System.out.println("State 5"); return "number"; //识别STRING case 17: c=nextChar(); if((c=='\"')) state=18; else state=fail(); System.out.println("state 17"); break; case 18: c=nextChar(); if(isLetter(c)) state=19; System.out.println("state 18"); break; case 19: c=nextChar(); if(isLetter(c)) state=19; else if((c=='\"')) state=20; System.out.println("state 19"); break; case 20: theid=buffer.substring(begining,ending); table.insert( theid , theid.length()-2 ,"字符串型","常数",""); System.out.println("state 20"); return "String"; //识别CHAR case 21: c=nextChar(); if((c=='\'')) state=22; else state=fail(); System.out.println("state 21"); break; case 22: c=nextChar(); if(isLetter(c)) state=23; break; case 23: c=nextChar(); if((c=='\'')) state=24; break; case 24: theid=buffer.substring(begining,ending); table.insert( theid , theid.length()-2 ,"字符型","常数",""); return "Char"; //识别不等式 case 25: c=nextChar(); if((c=='<')) state=26; else if((c=='=')) state=30; else if((c=='>')) state=31; else state=fail(); System.out.println("state 25"); break; case 26: c=nextChar(); if( (c=='=') ) state=27; else if((c=='>')) state=28; else state=29; break; case 27: theid = buffer.substring(begining,ending); table.insert(theid,1000,"","",""); return "<="; case 28: theid = buffer.substring(begining,ending); table.insert(theid,1000,"","",""); return "<>"; case 29: ungetChar(); theid = buffer.substring(begining,ending); table.insert(theid,1000,"","",""); return "<"; case 30: System.out.println("state 30"); theid = buffer.substring(begining,ending); table.insert(theid,1000,"","",""); return "="; case 31: c=nextChar(); if((c=='=')) state=32; else state=33; break; case 32: theid = buffer.substring(begining,ending); table.insert(theid,1000,"","",""); return ">="; case 33: System.out.println("state 33"); ungetChar(); theid = buffer.substring(begining,ending); table.insert(theid,1000,"","",""); return ">"; //识别算术符号 case 34: c=nextChar(); if((c=='+')) state=35; else if((c=='-')) state=36; else if((c=='*')) state=37; else if((c=='/')) state=38; else state=fail(); System.out.println("state 34"); break; case 35: System.out.println("state 35"); theid = buffer.substring(begining,ending); table.insert(theid,1000,"","",""); return "+"; case 36: System.out.println("state 36"); theid = buffer.substring(begining,ending); table.insert(theid,1000,"","",""); return "-"; case 37: System.out.println("state 37"); theid = buffer.substring(begining,ending); table.insert(theid,1000,"","",""); return "*"; case 38: System.out.println("state 38"); theid = buffer.substring(begining,ending); table.insert(theid,1000,"","",""); return "/"; }//end of switch }//end of while }//end nextToken int fail() { int start=-1; ending--; switch(state) { case 0: start=3; break; case 3: start=6; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -