java编译器一次不给出所有错误 [英] java compiler not give all error at a time

查看:124
本文介绍了java编译器一次不给出所有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

        System.out.println("First eror ::  without semicolon ") // first Error
        System.Out.println("This second error :: OUT object not used proper :: "); //second error


class TestCompilation
{
    public static void main(String []args)
    {   
        System.out.println("Hello no semicolon::")//First Error 
        System.Out.println("Hello out spell not correctely::"); //Second Error 

        }

}

每当我在cmd中通过JAVAC命令编译上面的代码时,它只给出First Error意味着不给出第二个错误。在java语言中,部分编译器和部分解释器在java中首先进行编译,因此编译器应列出所有错误,但它只给出了单个错误。为什么会这样?我不明白所以请帮我解决这个问题..

whenever i compile above code by JAVAC command in cmd it gives only First Error means not give second Error. In java language partially compiler and partially interpreter and In java first compilation happen so compiler should list out all errors but it gives me only single error. why its happening ? i dont understand so please help me out from this problem..

我想现在我很清楚我的问题意味着编译器完全有效...

I think now i am clear about my question means compiler works totally ...

为此我创建了一个简单的例子,它将帮助你理解java编译器的工作原理。

For that i create simple example it will help you to understand the java compiler working .

class TestCompilation
{
    public static void main(String []args)
    {   
        Syste.out.rintln("Hello");//First Error 
        Syste.Out.rintln("Hello");//Second Error (Not printed at compiler time because it syntatically correct as in compiler first phase)  
        Hitesh542.add(); //there is no such class called Hitesh542.- but compiler thinks that it is right way to call the method. So it passes the first time.
        Hitesh542.add()();//Look out here.. There is a problem, that we can't call a method like this..So it will show the error on first lookup.
        System.otu.println("Hello")//second  Errorasdasdas

        if(); //look this is also an error.- A BASIC syntax error shown at the first lookup.

        try{
            int i = 10 / 0;
        }
        //Third error

        //So bottom line is the JAVA syntatical errors are checked first i.e The SYNTAX of JAVA not the classes or objects.
        //But obv is the first thing is Java Compiler should get the Java class first for compilation on proper path. :)
    }
}


推荐答案

我会说这与编译器经常工作有关:

I'd say this has to do with how compilers often work:


  1. 执行词法分析,其中源代码转换为令牌序列。

  2. 代码解析,编译器检查令牌是否符合语言语法。这是您的第一行失败的地方:Java中的每个语句必须以分号结束。

  3. 语义分析,执行编译器尝试解析变量,根据已知符号列表的方法等 - 在Java中,这将粗略地转换为您的类路径。

  4. 代码生成其中源语句是翻译成本地字节码或一些中间字节码(后者是Java的情况)。

  1. Lexical analysis is performed, where the source code is converted into a sequence of "tokens".
  2. The code is parsed, where the compiler checks whether the tokens meet the language syntax. This is where your first line would fail: every statement in Java must be ended with a semicolon.
  3. Semantic analysis is performed, where the compiler tries to resolve variables, methods, etc. according to the list of known symbols - in Java, this would roughly translate to your classpath.
  4. Code is generated where the source statements are translated either into native bytecode or some intermediate bytecode (the latter is the case for Java).

如果其中一个步骤失败,进程必须停止,因为当代码不符合语法时,编译器无法执行语义分析。

If one of the steps fails, the process must stop, because a compiler cannot perform semantic analysis when the code doesn't meet the syntax.

这篇关于java编译器一次不给出所有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆