From the perspective of Compiler Theory, why would the javac compiler not accept a statement of the form x = x+++++y
but accept x = x+++ ++y
?
The javac command reads source files that contain module, package and type declarations written in the Java programming language, and compiles them into class files that run on the Java Virtual Machine. The javac command can also process annotations in Java source files and classes.
-Xlint. Enable all recommended warnings. In this release, enabling all available warnings is recommended. -Xlint:all. Enable all recommended warnings.
The javac.exe file is located in the bin folder of the JDK.
In fact, the Java compiler is often called the JVM compiler (for Java Virtual Machine). Consequently, you can write a Java program (on any platform) and use the JVM compiler (called javac) to generate a bytecode file (bytecode files use the extension . class).
Because ++
is a valid token for the java lexer, the statement x+++ ++y
will be parsed into tokens as :
(x)(++)(+)( )(++)(y)
Whereas x+++++y
will be tokenized into the invalid:
(x)(++)(++)(+)(y)
The above is invalid java because the ++
operator can only be applied to things that are numeric variables, which the result of (x++)
is not. This type of tokenizing is a great example of the concept known as maximal munch.
From the JLS section 3.2
The longest possible translation is used at each step, even if the result does not ultimately make a correct program while another lexical translation would.
Thus, the input characters a--b are tokenized (§3.5) as a, --, b, which is not part of any grammatically correct program, even though the tokenization a, -, -, b could be part of a grammatically correct program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With