-2

My friends, I am a c++, c, python and c# developer from china. All Javabook and all Internet sites, which I saw in my own said, that the precedense in java are like the table under my text. the precedence table says: +expr and -expr is over additive + and - and also over the multiplicative like *,/ and %.

for instance

b*-a would interpreted as (b)*(-a) and that is true okay and correct with the table.

But, when I follow the table

a+b should not work because, the precedence table, which is in all books which i saw like in the internetsite, says Java should interpreted it so:

a(+b) 

so we should get a error and not an addition, but java makes an addition between a and b, but normally that should not be a addition, when I follow the precedence table.

When i make a++b then java intepret at like the precedence table to a+(+b), but normally, when I follow the precedence table a addition like a+b should not work, because the precedence table says that is not a addition!

precedence table a little bit of them

  • You can't write `a++b`, that results in a syntax error. – Progman Dec 12 '21 at 17:06
  • 1
    Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then check the [help/on-topic] to see which questions are on-topic on this site. You have not asked any question or explained what the problem is. – Progman Dec 12 '21 at 17:09
  • Yes because (a++)b dthat is true and correct with the precedence table, so that will not be work, but a+b should also not work, because the precedence table said that we need to interpret it like a(+b) – isthatamisstake Dec 12 '21 at 17:09
  • but a+++b should not be a problem, but it is a problem, when I follow the precedence table! – isthatamisstake Dec 12 '21 at 17:10
  • The precedence table describe the evaluation order of an expression, not how the source code is parsed by the lexer. – Progman Dec 12 '21 at 17:14
  • 1
    You might want to check other questions like https://stackoverflow.com/questions/36061185/java-expression-interpretation-rules-of-decrement-increment-operators – Progman Dec 12 '21 at 17:15
  • The Problem is the precedence table says when I have a + and a expr. that this + is in connection with the expression so it should be a+b not a+b it should be a(+b) the + should not be a addition, it should be a parameter for the b! And the question https://stackoverflow.com/questions/36061185/java-expression-interpretation-rules-of-decrement-increment-operators is also a misstake! Like this articel a+b should be interpeted as a(+b) and not as a addition! – isthatamisstake Dec 12 '21 at 17:15
  • Please [edit] your post to include a question your want to ask. – Progman Dec 12 '21 at 17:34
  • Either all books and web sites in the world are wrong, or you are wrong. Which do you think? – user16632363 Dec 13 '21 at 01:59
  • A precedence table is used to disambiguate a parse for an ambiguous grammar or to help write a grammar that is not ambiguous. The precedence table is not a grammar for Java expressions. Starting with the [Language Spec grammar](https://docs.oracle.com/javase/specs/jls/se17/html/jls-19.html), or [the Antlr instantiation of the grammar from the Language Spec](https://github.com/antlr/grammars-v4/tree/master/java/java9), it is impossible to produce a parse tree remotely similar to "a (+b)" (ignoring most of the intermediate CST node for expressions) for input "a+b". – kaby76 Dec 13 '21 at 10:53

1 Answers1

1

The issue is that 'unary plus' and 'binary plus' are different operators that happen to use the same character in source code.

So, first of all, it is necessary to determine whether a given '+' is the unary plus operator or the binary plus operator. Only after that do you worry about precedence.

The presentation of syntax in the Java language spec should make it clear. Given that we are in a context that requires an 'expression' and that we have 'a+b', we can't start the syntactical analysis in the middle of the expression and decide that '+b' is a unary expression, ignoring the previous characters. Instead, we parse left-to-right, recognize 'a+b' as an additive expression (in which 'a' and 'b' are each multiplicative expressions, and in turn are each unary expressions, according to the written grammar).

user16632363
  • 1,050
  • 3
  • 6