📄 parsertable.java
字号:
package parser;
import exceptions.*;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import scanner.*;
public class ParserTable {
/**
* get the type of error when one type meets another
* @param stack token type in thestack
* @param input lookahead token type
* @return
*/
public ExpressionException getError(int stack,int input)
{
switch(stack)
{
case TokenType.NUMBER:
if(input==TokenType.NUMBER||input==TokenType.LBRACKET||input==TokenType.SIN||input==TokenType.COS
||input==TokenType.MAX||input==TokenType.MIN||input==TokenType.NOT||input==TokenType.BOOL){
return new exceptions.MissingOperatorException();
}
break;
case TokenType.COLON:
if(input==TokenType.COLON){
return new exceptions.TrinaryOperationException();
}
case TokenType.LBRACKET:
if(input==TokenType.END)
{
return new exceptions.MissingRightParenthesisException();
}
break;
case TokenType.RBRACKET:
if(input==TokenType.NUMBER||input==TokenType.SIN||input==TokenType.COS||input==TokenType.LBRACKET
||input==TokenType.MAX||input==TokenType.MIN||input==TokenType.NOT||input==TokenType.BOOL)
{
return new exceptions.MissingOperatorException();
}
break;
case TokenType.QMARK:
if(input==TokenType.END)
return new exceptions.TrinaryOperationException();
break;
case TokenType.BOOL:
if(input==TokenType.NUMBER||input==TokenType.SIN||input==TokenType.COS||input==TokenType.LBRACKET
||input==TokenType.MAX||input==TokenType.MIN||input==TokenType.NOT||input==TokenType.BOOL)
{
return new exceptions.MissingOperatorException();
}
break;
case TokenType.END:
if(input==TokenType.RBRACKET){
return new exceptions.MissingLeftParenthesisException();
}
else if(input==TokenType.COLON){
return new exceptions.TrinaryOperationException();
}
break;
}
return new exceptions.MissingOperandException();
}
/**
* judge whether one type meet another causes an error
* @param stack the token type in the stack
* @param input the lookahead type
* @return true if error
*/
public boolean isError(int stack,int input)
{
switch(stack)
{
case TokenType.NUMBER:
if(input==TokenType.NUMBER||input==TokenType.LBRACKET||input==TokenType.SIN||input==TokenType.COS
||input==TokenType.MAX||input==TokenType.MIN||input==TokenType.NOT||input==TokenType.BOOL){
return true;
}
break;
case TokenType.LBRACKET:
if(input==TokenType.END)
{
return true;
}
break;
case TokenType.COLON:
if(input==TokenType.COLON){
return true;
}
break;
case TokenType.RBRACKET:
if(input==TokenType.NUMBER||input==TokenType.SIN||input==TokenType.COS||input==TokenType.LBRACKET
||input==TokenType.MAX||input==TokenType.MIN||input==TokenType.NOT||input==TokenType.BOOL)
{
return true;
}
break;
case TokenType.QMARK:
if(input==TokenType.END)
return true;
break;
case TokenType.BOOL:
if(input==TokenType.NUMBER||input==TokenType.SIN||input==TokenType.COS||input==TokenType.LBRACKET
||input==TokenType.MAX||input==TokenType.MIN||input==TokenType.NOT||input==TokenType.BOOL)
{
return true;
}
break;
case TokenType.END:
if(input==TokenType.RBRACKET||input==TokenType.COLON){
return true;
}
break;
}
return false;
}
/**
* judge whether to shift or reduce
* @param input token type in the stack
* @param lookahead lookahead token type
* @return true if shift
*/
public static boolean isShift(int input,int lookahead)
{
if(lookahead==TokenType.LBRACKET||input==TokenType.LBRACKET)
return true;
if(input==TokenType.RBRACKET||lookahead==TokenType.RBRACKET)
return false;
if(input==TokenType.COLON&&lookahead==TokenType.QMARK){
return true;
}
if(input==TokenType.QMARK&&lookahead==TokenType.COLON){
return true;
}
if(input==TokenType.QMARK&&lookahead==TokenType.QMARK){
return true;
}
if(TokenType.getPriority(input)==TokenType.getPriority(lookahead)){
if(TokenType.isLeft(input))
return true;
else
return false;
}
if(TokenType.getPriority(input)<TokenType.getPriority(lookahead)){
return false;
}
else return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -