支持多位数的加减法需要在词法解析、语法解析、解释器同时修改。
- 词法解析:连续识别多位数字、增加减法
- 语法解析:语法图增加减法
- 解释器:语法节点增加减法功能
修改词法解析
// 词性枚举
public enum TokenType {
INTEGER // 数字
, PLUS // 加法运算符
, EOF // 程序结束
, MINUS // 减法运算符
}
// 词法解析器
public class Lexer {
private String text; // 输入的程序
private Integer position; // 记录扫描的位置
private Character currentChar; // 记录当前扫描的字符
public Token getNextToken(){ // 获取词法单元
while(this.currentChar != null){
if(Character.isDigit(this.currentChar)){
return this.integer();
}else if(Character.isWhitespace(currentChar)){
this.skipWhiteSpace();
}else if(this.currentChar == '+'){
Token token = new Token(TokenType.PLUS , "+");
this.advance();
return token;
}else if(this.currentChar == '-'){
Token token = new Token(TokenType.MINUS , "-");
this.advance();
return token;
}else {
this.error("未知的词法");
}
}
return new Token(TokenType.EOF);
}
public Token integer(){ // 识别多个数字
String result = "";
while(this.currentChar != null && Character.isDigit(this.currentChar)){
result += this.currentChar;
this.advance();
}
return new Token(TokenType.INTEGER ,Integer.valueOf(result));
}
private void skipWhiteSpace(){ // 空格跳过
while(currentChar != null && Character.isWhitespace(currentChar)){
this.advance();
}
}
public void advance(){ // 往后走一步
this.position += 1;
if(this.position <= this.text.length() - 1){ // 扫描的位置有效
this.currentChar = text.charAt(this.position);
}else{ // 扫描完了
this.currentChar = null;
}
}
public void error(String msg){ // 报错函数
throw new RuntimeException(msg);
}
public Lexer(String text) {// 构造器
this.text = text;
this.position = 0;
this.currentChar = text.charAt(this.position);
}
}
修改语法解析
@Data
public class ProgramAst extends Ast{
private Integer leftValue;
private TokenType op;
private Integer rightValue;
public ProgramAst(Integer leftValue,TokenType op, Integer rightValue) {
this.leftValue = leftValue;
this.op = op;
this.rightValue = rightValue;
}
}
// 语法解析器
public class Parser {
private Lexer lexer ; // 词法解析器
private Token currentToken; // 当前的词法单元
public Parser(Lexer lexer) {
this.lexer = lexer;
this.currentToken = this.lexer.getNextToken();
}
public ProgramAst programAst(){ // 程序节点
// programAst : INTEGER PLUS INTEGER
Token left = this.currentToken;
this.eat(TokenType.INTEGER);
Token op = this.currentToken;
if(this.currentToken.getType() == TokenType.PLUS){
this.eat(TokenType.PLUS);
}else if(this.currentToken.getType() == TokenType.MINUS){
this.eat(TokenType.MINUS);
}
Token right = this.currentToken;
this.eat(TokenType.INTEGER);
return new ProgramAst((Integer)left.getValue() ,op.getType(), (Integer)right.getValue());
}
public void eat(TokenType tokenType){ // 确认当前的词性是否正确
if(tokenType == this.currentToken.getType()){
this.currentToken = this.lexer.getNextToken();
}else{
this.error("语法错误");
}
}
public void error(String msg){ // 报错函数
throw new RuntimeException(msg);
}
public Ast parse(){ // 获取语法树
return this.programAst();
}
}
修改解释器
// 目标执行器
public class Interpreter {
private Parser parser; // 语法解析器
public Interpreter(Parser parser) {
this.parser = parser;
}
public Object visitProgramAst(Ast ast){ // 访问programAst节点
ProgramAst programAst = (ProgramAst) ast;
if(programAst.getOp() == TokenType.PLUS){
return programAst.getLeftValue() + programAst.getRightValue(); // 加法计算
}else if(programAst.getOp() == TokenType.MINUS){
return programAst.getLeftValue() + programAst.getRightValue(); // 减法计算
}
return null;
}
public Object visit(Ast ast){ // 使用反射通过类名调用对应的函数
String methodName = "visit" + ast.getClass().getSimpleName();
try {
Method method = this.getClass().getDeclaredMethod(methodName , Ast.class );
return method.invoke(this, ast);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Integer expr() {
Ast ast = parser.parse(); // 获取语法树
Integer result = (Integer)this.visit(ast); // 遍历获取结果
return result;
}
}
执行测试
private static void testInterpreter() {
Lexer lexer = new Lexer(" 11 + 14 ");
Parser parser = new Parser(lexer);
Interpreter interpreter = new Interpreter(parser);
Integer result = interpreter.expr();
System.out.println("计算结果:" + result);
}
执行打印 计算结果:25