📄 check.java~362~
字号:
package lexicalanalyzer2;
import java.io.*;
/**
* Title: Check
* Description: Check and return the four attributes of the token.java
* Copyright: Copyright (c) 2004 15th,Oct
* Company: B02251132
* @author: ZengBo Huang
* @version 1.0
*/
public class check {
//These are all the variable in this part
private int line,position;//Token's line and position
private int state;//All the case's state
private int tknln;//Length of one token
private int chrnum;//Length of the file
private int num_before_point_or_E;//For float number and exponent number,for example,a float number like XXX.XXXXX ,that is three
private int forward;//The point of character in the file
private int oneortwo=0;//Check the token's like //,==,>=,<= and etc.
private FileReader filereader;//The file input
private char[] chr=null;//This is the array that put the character of the input file
private boolean is_int=true,is_float,is_exponent;//Check if a number is int,float or exponent
private StringBuffer stringbuffer = new StringBuffer();//Create a buffer for token
public check()throws IOException {
chr=new char[10000];
filereader = new FileReader("text.txt");//Create a FileReader to put the file
chrnum = filereader.read(chr);//Put the file into the array,chrnum
}
public token nexttoken(){//This is a class from token
char c;
while (true) {//Create a roop that never down
switch (state) {//Begin the switch include 16 cases
case 0: //Check the if it is a letter or digit
c = chr[forward];
++forward;
position++;
if (Character.isDigit(c)) {//If the char is a digit,turn to case 16
state=16;
stringbuffer.append(c);//Put the char into the buffer
tknln++;
}
else
if (Character.isLetter(c)) {//If the char is a letter,turn to case 1
state=1;
stringbuffer.append(c);//Put the char into the buffer
tknln++;
}
else {//If not a letter or digit,turn to case 2 to check if it is other case
forward--;
position--;
state=2;
}
break;
case 1: //This case is fall from case 0,check if it isletter or digit
c=chr[forward];
++forward;
position++;
if (Character.isLetterOrDigit(c)){//If it is letter or digit
if(Character.isDigit(c)){//If it is a number
num_before_point_or_E++;//Check the number before point
}
state=1;//Back to the begin of case 1 to check the next char
stringbuffer.append(c);
tknln++;
}
else {
//Check if it is a situation like "asd12.8"
//It should be taken as a identify "asd" and a float number 12.8
if((c=='.'&&Character.isDigit(chr[forward+1]))||(c=='E'&&Character.isDigit(chr[forward+1]))){//If it is a float number
state=16;//Turn to the state 16 to check the number
forward=forward-num_before_point_or_E;//The forward point should be put before the number begin
}
else{//Not a float number
tknln--;
forward--;
position--;
state = 2;
//If it is a keywords
if (stringbuffer.toString().equals("int") ||
stringbuffer.toString().equals("real") ||
stringbuffer.toString().equals("if") ||
stringbuffer.toString().equals("then") ||
stringbuffer.toString().equals("else") ||
stringbuffer.toString().equals("while")) {
token tkn = new token(stringbuffer.toString(), "Keywords",
line + 1, position - tknln);//Output
for(int count=tknln;count>=0;count--){//Cleanout the buffer
stringbuffer.deleteCharAt(count);
}
tknln = 0;
state=0;
return tkn;
}
else {//Not the keywords,then it is a identifier
token tkn = new token(stringbuffer.toString(), "Identifier",
line + 1, position - tknln);
for(int count=tknln;count>=0;count--){
stringbuffer.deleteCharAt(count);
}
tknln = 0;
state=0;
return tkn;
}
}
}
break;
case 2: //Whitespace or change the line
c=chr[forward];
++forward;
position++;
if(c==' '){//If it is a whitespace
state=0;//Turn to state 0 and begin to check next token
tknln=0;
}
//else
else
if(chr[forward]=='\n'){//Change the line
forward++;//Forward should also change the line
state=0;
tknln=0;
line++;
position=0;
//token tkn=new token("Newline","Other"+"\t",line+1,position);
//return tkn;
}
else {//Not a whitespace or line change,next case
forward--;
position--;
state=3;
}
break;
case 3: //Operators(+)
c=chr[forward];
++forward;
position++;
if(c=='+'){
token tkn=new token("+","Operator",line+1,position);//Output
state=0;
return tkn;
}
else {
forward--;
position--;
state=4;
}
break;
case 4: //Operators(-)
c=chr[forward];
++forward;
position++;
if(c=='-'){
token tkn=new token("-","Operator",line+1,position+1);//Output
state=0;
return tkn;
}
else {
forward--;
position--;
state=5;
}
break;
case 5: //Operators(*)
c=chr[forward];
++forward;
position++;
if(c=='*'){
token tkn=new token("*","Operator",line+1,position+1);//Output
state=0;
return tkn;
}
else {
forward--;
position--;
state=6;
}
break;
case 6: //Operators(/) and cut the comment
c=chr[forward];
++forward;
position++;
if(c=='/'){//if it is a '/' turn back to case 6 to check if there is comment
state=6;
oneortwo++;
if(oneortwo>=2){ //There are two '/'s,then cut the comment
while(chr[forward-1]!='\n'){
forward++;
}
oneortwo=0;
state=0;
tknln=0;
line++;
position=0;
}else
if(oneortwo==1&&chr[forward]!='/'){//One '/' and then output the '/'
token tkn = new token("/", "Operator", line + 1, position + 1);
oneortwo=0;
state = 0;
return tkn;
}
}
else {
forward--;
position--;
state=7;
}
break;
case 7: //Operators( = or == or <= or >= or < or > )
c=chr[forward];
++forward;
position++;
if(c=='='){//Check the operator '=' and '=='
stringbuffer.append(c);
tknln++;
oneortwo++;
state=7;
}
else {
state=8;
forward--;
position--;
if(oneortwo==1){//There is one operator, output'<' or '>' or '=' or '!'
oneortwo=0;
if(chr[forward-1]=='<'){
token tkn=new token("<","Operator",line+1,position);//Output '<'
for(int count=tknln-1;count>=0;count--){
stringbuffer.deleteCharAt(count);
}
state = 0;
tknln = 0;
return tkn;
}
else
if(chr[forward-1]=='>'){
token tkn=new token(">","Operator",line+1,position);//Output '>'
for(int count=tknln-1;count>=0;count--){
stringbuffer.deleteCharAt(count);
}
state = 0;
tknln = 0;
return tkn;
}
else
if(chr[forward-1]=='='){
token tkn=new token("=","Operator",line+1,position);//Output '='
for(int count=tknln-1;count>=0;count--){
stringbuffer.deleteCharAt(count);
}
state = 0;
tknln = 0;
return tkn;
}
else
if(chr[forward-1]=='!'){
token tkn=new token("!","Operator",line+1,position);//Output '!'
for(int count=tknln-1;count>=0;count--){
stringbuffer.deleteCharAt(count);
}
state = 0;
tknln = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -