⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 postfix.java

📁 中山大学编译原理实验三 后缀表达式Postfix
💻 JAVA
字号:

import java.io.*;

/**
 * 语法分析器,将中缀表达式转换为后缀表达式
 * @author Zeng
 */
class Parser {

    /**
     * 下一个字符
     */
    static int lookahead;
    /**
     * 错误个数
     */
    private int errornum;
    /**
     * 错误位置
     */
    private int errorps;
    /**
     * (未匹配)左括号个数
     */
    private int bracket;
    /**
     * 表达式是否结束
     */
    private boolean next;
    /**
     * 结果缓冲区
     */
    private PrintWriter rs;
    /**
     * 错误提示缓冲区
     */
    private PrintWriter er;

    /**
     * 初始化变量
     * @throws java.io.IOException
     */
    public Parser() throws IOException {
        errornum = 0;
        errorps = 0;
        bracket = 0;
        next = true;
        rs = new PrintWriter(System.out);
        er = new PrintWriter(System.out);
    }

    /**
     * 将输入的中缀表达式转换成后缀表达式
     * @throws java.io.IOException
     */
    void run() throws IOException {
        readnext();
        expr();
        System.out.println("/************************/");
        if (errornum > 0) {
            System.out.println("Total:" + errornum + " errors.");
            er.flush();
        } else {
            System.out.println("Result:");
            rs.flush();
            System.out.println();
        }
        System.out.println("/************************/");
    }

    /**
     * 对表达式进行操作
     * @throws java.io.IOException
     */
    void expr() throws IOException {
        term();
        rest();
    }

    /**
     * 对加减进行操作
     * @throws java.io.IOException
     */
    void rest() throws IOException {
        while (next) {
            if (lookahead == '+') {
                match('+');
                term();
                rs.write('+');
            } else if (lookahead == '-') {
                match('-');
                term();
                rs.write('-');
            } else if (lookahead == ')') {
                if (bracket > 0) {
                    bracket--;
                    return;
                } else {
                    errornum++;
                    er.write("Unit " + errorps + ":语法错误,无法找到匹配的左括号.\n");
                    readnext();
                }
            }
        }
    }

    /**
     * 对乘除进行操作
     * @throws java.io.IOException
     */
    void term() throws IOException {
        factor();
        while (next) {
            if (lookahead == '*') {
                match(lookahead);
                factor();
                rs.write('*');
            } else if (lookahead == '/') {
                match(lookahead);
                factor();
                rs.write('/');
            } else if (lookahead == '+' || lookahead == '-') {
                return;
            } else if (lookahead == ')') {
                if (bracket > 0) {
                    return;
                } else {
                    errornum++;
                    er.write("Unit " + errorps + ":语法错误,无法找到匹配的左括号.\n");
                    readnext();
                }
            } else if (Character.isDigit((char) lookahead)) {
                errornum++;
                er.write("Unit " + errorps + ":语法错误,左边缺少运算符.\n");
                readnext();
            } else if (lookahead == '(') {
                errornum++;
                er.write("Unit " + errorps + ":语法错误,左边缺少运算符.\n");
                factor();
            } else {
                errornum++;
                er.write("Unit " + errorps + ":词法错误,非法字符.\n");
                readnext();
            }
        }
    }

    /**
     * 对数字和括号进行操作
     * @throws java.io.IOException
     */
    void factor() throws IOException {
        if (!next) {
            errornum++;
            er.write("Unit " + errorps + ":语法错误,缺少右运算量.\n");
        }
        if (Character.isDigit((char) lookahead)) {
            rs.write(lookahead);
            match(lookahead);
        } else if (lookahead == '(') {
            bracket++;
            rs.write('(');
            match('(');
            expr();
            match(')');
            rs.write(')');
        } else if (lookahead == '+' || lookahead == '-' || lookahead == '*' || lookahead == '/' || lookahead == ')') {
            errornum++;
            er.write("Unit " + errorps + ":语法错误,缺少左运算量.\n");
        } else if (lookahead != 13) {
            errornum++;
            er.write("Unit " + errorps + ":词法错误,非法字符.\n");
            readnext();
        }
    }

    /**
     * 检查是否与t匹配
     * @param t 用来与下一个字符进行比较的数字
     * @throws java.io.IOException
     */
    void match(int t) throws IOException {
        if (lookahead != t) {
            errornum++;
            er.write("Unit " + errorps + ":语法错误,缺少\'" + (char) t + "\'匹配.\n");
        }
        if (lookahead != 13) {
            readnext();
        }
    }

    /**
     * 读取下一个字符
     * @throws java.io.IOException
     */
    void readnext() throws IOException {
        do {
            lookahead = System.in.read();
            errorps++;
        } while (lookahead == ' ');
        if (lookahead == 13) {
            next = false;
        }
    }
}

/**
 * 将中缀表达式转换为后缀表达式
 * @author Zeng
 */
public class Postfix {

    /**
     * 主程序入口
     * @param args
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        System.out.println(
                "Input an infix expression and output its postfix notation:");
        new Parser().run();
        System.out.println("End of program.");
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -