📄 token.java
字号:
package parser;
import exceptions.*;
/**
* main program of the Lexical analysis
* @author Yuanhang Yang
*
*/
public class Token {
private int ip = -1;
private char ch = 0;
private Terminal lastToken = null;
private StringBuffer result = new StringBuffer();
private String input;
private int state;
private boolean read = true;
public Token(String inputstr) throws ExpressionException {
input = inputstr;
if (input.length() == 0)
throw new EmptyExpressionException();
if (input.indexOf('$') != -1)
throw new IllegalSymbolException();
}
/**function: recognize a token according to the DFA
*
* @return Terminal
* @throws LexicalException
*/
public Terminal nexttoken() throws LexicalException {
state = 0;
while (ip < input.length() - 1 && input.charAt(ip + 1) == ' ')
++ip;
while (true) {
if (read)
nextchar();
switch (state) {
case 0:
state0();
break;
case 1:
state1();
break;
case 2:
state2();
break;
case 3:
state3();
break;
case 4:
state4();
break;
case 5:
state5();
break;
case 6:
state6();
break;
case 7:
state7();
break;
case 8:
state8();
break;
case 9:
state9();
break;
case 10:
state10();
break;
case 11:
state11();
break;
case 12:
state12();
break;
case 13:
state13();
break;
case 14:
state14();
break;
case 15:
state15();
break;
case 16:
state16();
break;
case 17:
state17();
break;
case 18:
state18();
break;
case 19:
state19();
break;
case 20:
state20();
break;
case 21:
state21();
break;
case 22:
state22();
break;
case 23:
read = false;
state23();
break;
case 24: {
read = true;
return state24();
}
default:
break;
}
}
}
/**function:the initial state of the DFA.Accord to next token ,it decides its next state.
*
* @throws LexicalException
*/
private void state0() throws LexicalException {
if (Character.isDigit(ch)) {
state = 1;
} else if (ch == '+' || ch == '-' || ch == '*' || ch == '/'
|| ch == '(' || ch == ')' || ch == '?' || ch == ':'
|| ch == '|' || ch == '&' || ch == '!' || ch == ','
|| ch == '^' || ch == '=' || ch == '$') {
state = 24;
read = false;
}
switch (ch) {
case 'm':
state = 11;
break;
case 'c':
state = 9;
break;
case 's':
state = 7;
break;
case 't':
state = 16;
break;
case 'f':
state = 19;
break;
case '<':
state = 14;
break;
case '>':
state = 15;
break;
default:
break;
}
if (state == 0) {
if (Character.isLetter(ch))
throw new IllegalIdentifierException();
else if(ch == '.')
throw new IllegalDecimalException();
else
throw new IllegalSymbolException();
}
}
private void state1() {
if (Character.isDigit(ch)) {
state = 1;
} else if (ch == '.') {
state = 2;
} else if (ch == 'e') {
state = 4;
} else {
retractchar();
state = 23;
}
}
private void state2() throws LexicalException {
if (Character.isDigit(ch)) {
state = 3;
} else
throw new IllegalDecimalException();
}
private void state3() {
if (Character.isDigit(ch)) {
state = 3;
} else if (ch == 'E' || ch == 'e') {
state = 4;
} else {
retractchar();
state = 23;
}
}
private void state4() throws LexicalException{
if (ch == '+' || ch == '-') {
state = 5;
} else if (Character.isDigit(ch)) {
state = 6;
} else
throw new IllegalDecimalException();
//state = 5;
}
private void state5() throws LexicalException {
if (Character.isDigit(ch)) {
state = 6;
} else
throw new IllegalDecimalException();
}
private void state6()throws LexicalException {
if (Character.isDigit(ch)) {
state = 6;
} else if(ch=='.')
throw new IllegalDecimalException();
else{
retractchar();
state = 23;
}
}
private void state7() throws LexicalException {
if (ch == 'i') {
state = 8;
} else
throw new IllegalIdentifierException();
}
private void state8() throws LexicalException {
if (ch == 'n') {
state = 24;
read = false;
} else
throw new IllegalIdentifierException();
}
private void state9() throws LexicalException {
if (ch == 'o') {
state = 10;
} else
throw new IllegalIdentifierException();
}
private void state10() throws LexicalException {
if (ch == 's') {
state = 24;
read = false;
} else
throw new IllegalIdentifierException();
}
private void state11() throws LexicalException {
if (ch == 'a') {
state = 12;
} else if (ch == 'i') {
state = 13;
} else
throw new IllegalIdentifierException();
}
private void state12() throws LexicalException {
if (ch == 'x') {
state = 24;
read = false;
} else
throw new IllegalIdentifierException();
}
private void state13() throws LexicalException {
if (ch == 'n') {
read = false;
state = 24;
} else
throw new IllegalIdentifierException();
}
private void state14() {
if (ch == '>' || ch == '=') {
state = 24;
read = false;
} else {
retractchar();
state = 23;
}
}
private void state15() {
if (ch == '=') {
read = false;
state = 24;
} else {
retractchar();
state = 23;
}
}
private void state16() throws LexicalException {
if (ch == 'r') {
state = 17;
} else
throw new IllegalIdentifierException();
}
private void state17() throws LexicalException {
if (ch == 'u') {
state = 18;
} else
throw new IllegalIdentifierException();
}
private void state18() throws LexicalException {
if (ch == 'e') {
read = false;
state = 24;
} else
throw new IllegalIdentifierException();
}
private void state19() throws LexicalException {
if (ch == 'a') {
state = 20;
} else
throw new IllegalIdentifierException();
}
private void state20() throws LexicalException {
if (ch == 'l') {
state = 21;
} else
throw new IllegalIdentifierException();
}
private void state21() throws LexicalException {
if (ch == 's') {
state = 24;
} else
throw new IllegalIdentifierException();
}
private void state22() throws LexicalException {
if (ch == 'e') {
read = false;
state = 24;
} else
throw new IllegalIdentifierException();
}
private void state23() {
retractchar();
state = 24;
}
/**function: the final state.
*
* @return Terminal
*/
private Terminal state24() {
String ok = result.toString();
result = new StringBuffer();
lastToken = new Terminal(gettype(ok), ok);
return lastToken;
}
/**function: read next char from the inpunt.
*
* @return next char
*/
public char nextchar() {
++ip;
if (ip >= input.length()) {
ch = '$';
} else {
ch = input.charAt(ip);
}
result.append(ch);
return ch;
}
/**function: ip was setted back 1
*
* @return last token
*/
public Character retractchar() {
--ip;
if (ip < 0)
return null;
ch = input.charAt(ip);
result.deleteCharAt(result.length() - 1);
return ch;
}
/**function:get the tokentype of the token
*
* @param token
* String of a token
* @return tokenType
*/
public int gettype(String token) {
char c = token.charAt(0);
if (token.equals("("))
return TokenType.leftbracket;
else if (token.equals(")"))
return TokenType.rightbracket;
else if (token.equals("sin"))
return TokenType.fun;
else if (token.equals("cos"))
return TokenType.fun;
else if (token.equals("max"))
return TokenType.fun;
else if (token.equals("min"))
return TokenType.fun;
else if(token.equals("true"))
return TokenType.bool;
else if(token.equals("false"))
return TokenType.bool;
else if (token.equals("^"))
return TokenType.power;
else if (token.equals("*"))
return TokenType.mult_div;
else if (token.equals("/"))
return TokenType.mult_div;
else if (token.equals("+"))
return TokenType.add_sub;
else if (token.equals("-")) {
if (lastToken == null ||
(lastToken.tokenType != TokenType.rightbracket
&& lastToken.tokenType != TokenType.number)) {
return TokenType.negative;
}
return TokenType.add_sub;
} else if (token.equals(">"))
return TokenType.relop;
else if (token.equals("<"))
return TokenType.relop;
else if (token.equals("="))
return TokenType.relop;
else if (token.equals(">="))
return TokenType.relop;
else if (token.equals("<="))
return TokenType.relop;
else if (token.equals("<>"))
return TokenType.relop;
else if (token.equals("!"))
return TokenType.not;
else if (token.equals("&"))
return TokenType.and;
else if (token.equals("|"))
return TokenType.or;
else if (token.equals("?"))
return TokenType.question;
else if (token.equals(":"))
return TokenType.colon;
else if (token.equals(","))
return TokenType.comma;
else if (Character.isDigit(c)) {
return TokenType.number;
} else if (token.equals("$"))
return TokenType.dollor;
else
return -1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -