为什么这个Java程序在Eclipse中的控制台输入输入后终止?

时间:2015-07-06 14:48:55

标签: java eclipse parsing

我从教科书(编译原则,技术和工具)复制了这个程序,并改变了它中的一些东西来做我想要的。这是一个将中缀表达式转换为pretfix形式的程序。 这是代码:

package prefixTrans;
import java.io.*;
public class Parser {
    static int lookahead;

    public Parser() throws IOException{
        lookahead = System.in.read();
    }

    void exp() throws IOException{
        while(true) {
            if (lookahead == '*'){
                match('*'); System.out.write('*'); exp(); term();
            }
            else if (lookahead == '/'){
                match('/'); System.out.write('/'); exp(); term();
            }
            else return;
        }
    }

    void term() throws IOException{
        if (lookahead == '+'){
            match('+'); System.out.write('+'); factor(); term();}
        else if (lookahead == '-'){
            match('-'); System.out.write('-'); factor(); term();}
        else return;

    }

    void factor() throws IOException{
        if ( Character.isDigit((char)lookahead))  { 
            int v = 0;
            while(Character.isDigit((char)lookahead)){
                v = v * 10 + lookahead; 
            }
        }
        else if(Character.isAlphabetic(lookahead)){
            String lexeme = "";
            while(Character.isLetter(lookahead)){
                lexeme = lexeme + lookahead;
            }
        }
        System.out.write((char)lookahead); match(lookahead);
    }

    void match(int t) throws IOException{
        if(lookahead == t) lookahead = System.in.read();
        else throw new Error("syntax error");
    }

    public static void main(String [] args) throws IOException{
        Parser parse = new Parser();
        parse.exp(); System.out.write('\n');

    }
}

每当我在Eclipse内的控制台中输入一个输入时,程序就会终止。

我已编辑了我的代码,它现在没有终止,但我没有输出。这是编辑过的:

package prefixTrans;

import java.io.*;
import java.util.Scanner;


public class Parser {
    static int lookahead;
    Scanner input;

    public Parser() throws IOException{
        //lookahead = System.in.read();
         input = new Scanner(System.in);
            lookahead = input.next().charAt(0);
        }

    void exp() throws IOException{

        if (lookahead == '*'){
                match('*'); System.out.write('*');exp();term();  
            }
            else if (lookahead == '/'){
                match('/'); System.out.write('/');exp();term(); 
            }
            else term();

    }

    void term() throws IOException{

        if (lookahead == '+'){
            match('+'); System.out.write('+'); factor(); term(); }
        else if (lookahead == '-'){
            match('-'); System.out.write('-'); factor(); term(); }
        else factor();

    }

    void factor() throws IOException{
        if ( Character.isDigit((char)lookahead))  { 
            int v = 0;
            while(Character.isDigit((char)lookahead)){
                v = v * 10 + lookahead; 
                }
            }
            else if(Character.isLetter(lookahead)){
                String lexeme = "";
                while(Character.isLetter(lookahead)){
                    lexeme = lexeme + lookahead;
                }
            }
            System.out.write((char)lookahead); match(lookahead);
        }

     void match(int t) throws IOException{
        if(lookahead == t) /*lookahead = System.in.read();*/ lookahead = input.next().charAt(0);
        else throw new Error("syntax error");
    }


         public static void main(String [] args) throws IOException{
             Parser parse = new Parser();
             parse.exp(); System.out.write('\n');

     }
}

2 个答案:

答案 0 :(得分:1)

首先调用exp。如果第一个字符既不是*也不是/,则调用return,因此存在函数和程序。这可能不是你想要的。 此外,只有在按下回车键时才会读取(在我的机器上)字符,因此前瞻符号值"\n",您无法满足该值。基本上,既然您在Eclipse中运行,请调试程序以逐步查看正在发生的事情。

答案 1 :(得分:0)

正如“JP Moresmau”在另一个答案中所建议的,这里的问题是System.in.read()只读取一个字节(在构造函数Parser中)和回车符(输入*或/后输入的字符)是在方法match()中读取下一个System.in.read()。由于回车符不匹配*或/,您的程序终止。

使用回车或以下列方式使用Scanner API来获得您的预期。

public class Parser {
    private char lookahead;
    Scanner input;

    public Parser() throws IOException{
        input = new Scanner(System.in);
        lookahead = input.next().charAt(0);
    }

    void exp() throws IOException{
        while(true) {
            if (lookahead == '*'){
                match('*'); System.out.write('*'); exp(); term();
            }
            else if (lookahead == '/'){
                match('/'); System.out.write('/'); exp(); term();
            }
            else return;
        }
    }

    void term() throws IOException{
        if (lookahead == '+'){
            match('+'); System.out.write('+'); factor(); term();}
        else if (lookahead == '-'){
            match('-'); System.out.write('-'); factor(); term();}
        else return;

    }

    void factor() throws IOException{
        if ( Character.isDigit((char)lookahead))  { 
            int v = 0;
            while(Character.isDigit((char)lookahead)){
                v = v * 10 + lookahead; 
            }
        } else if(Character.isAlphabetic(lookahead)){
                String lexeme = "";
            while(Character.isLetter(lookahead)){
                lexeme = lexeme + lookahead;
            }
        }
        System.out.write((char)lookahead); match(lookahead);
    }

    void match(char t) throws IOException{
        if(lookahead == t) lookahead = input.next().charAt(0);
        else throw new Error("syntax error");
    }

    public static void main(String [] args) throws IOException{
         Parser parse = new Parser();
         parse.exp(); System.out.write('\n');
    }
}