(expression)++

T

thibault.langlois

Hello,
I see from several sources(*) the following as part of the definition
of the Java syntax:

numeric_expression
::=
( ( "-" | "++" | "--" ) expression )
| ( expression ( "++" | "--" ) )
| <more here>

This means that I could write something like (a + b)++ which sounds
strange to me (and to the compiler too :).
I wonder why the syntax was defined this way. Was it in order to
simplify it for didactical purpose ?
Is it impossible to define the java syntax such that (a + b)++ is not
allowed ?
One solution could be to define two kinds of expressions, for the
right-side and for the left-side of the attribuition.

Thibault Langlois

(*)
http://cui.unige.ch/db-research/Enseignement/analyseinfo/JAVA/numeric_expression.html
http://duke.csc.villanova.edu/jss1/
 
M

Matt Humphrey

Hello,
I see from several sources(*) the following as part of the definition
of the Java syntax:

numeric_expression
::=
( ( "-" | "++" | "--" ) expression )
| ( expression ( "++" | "--" ) )
| <more here>

This means that I could write something like (a + b)++ which sounds
strange to me (and to the compiler too :).
I wonder why the syntax was defined this way. Was it in order to
simplify it for didactical purpose ?
Is it impossible to define the java syntax such that (a + b)++ is not
allowed ?
One solution could be to define two kinds of expressions, for the
right-side and for the left-side of the attribuition.

My compiler (Eclipse 3.0) rejects the expression (a + b)++. The expression
grammer given in JLS is more complex than your example above, but still
relies on prose to indicate that the prefix operators should apply only to
variables:

15.15.1 ... A unary expression preceded by a ++ operator is a prefix
increment expression. The result of the unary expression must be a variable
of a type that is convertible (§5.1.8) to a numeric type, or a compile-time
error occurs.

A context-free grammer is not sufficiently powerful to describe all the
constraints of a typical programming language such as Java. The constraint
for prefix operators is not only that it be a variable, but that it be a
numeric type. I think that using left- and right-side expressions would
complicate the grammar without solving the problem of fully describing the
operations.

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
R

Robert Klemme

Hello,
I see from several sources(*) the following as part of the definition
of the Java syntax:

numeric_expression
::=
( ( "-" | "++" | "--" ) expression )
| ( expression ( "++" | "--" ) )
| <more here>

This means that I could write something like (a + b)++ which sounds
strange to me (and to the compiler too :).
I wonder why the syntax was defined this way. Was it in order to
simplify it for didactical purpose ?
Is it impossible to define the java syntax such that (a + b)++ is not
allowed ?
One solution could be to define two kinds of expressions, for the
right-side and for the left-side of the attribuition.

Thibault Langlois

(*)
http://cui.unige.ch/db-research/Enseignement/analyseinfo/JAVA/numeric_expression.html
http://duke.csc.villanova.edu/jss1/

You'll have to ask the authors of that paper. For the Java language
only the JLS is authoritative.

http://java.sun.com/docs/books/jls/

Especially
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.14

Kind regards

robert
 
P

Patricia Shanahan

Most programming languages, Java included, have really intractable
syntax if you try to represent it all in the grammar. Whether a
statement is valid or not may depend on a declaration that could be an
unbounded distance away in the source code, or even in a different file.
The grammar would be context sensitive or unrestricted, and parsing from
it would be intractable, for both computers and humans.

On the other hand, languages like Java can be represented by the
combination of a very tractable grammar and a set of additional rules
that may depend on remembering things like identifiers and types for
variables that are accessible in the current scope.

Once it is recognized that the grammar is not the whole story about what
is and is not a program, it is natural to use whichever expresses an
idea more simply and clearly out of grammar and rules. In this case, it
might be possible to put the entire rule in the grammar, but it would
mean maintaining, throughout the expression grammar, a distinction
between expressions that represent variables and expressions that don't.

The authoritative grammar in the JLS does not allow general expressions
for postfix ++, but it does allow "(a + b)++" because "(a + b)" is a
Primary, and therefore a PostfixExpression. It is excluded by the
additional rule "The result of the postfix expression must be a variable
of a type that is convertible (§5.1.8) to a numeric type, or a
compile-time error occurs.".

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.14.2

Patricia
 
T

thibault.langlois

I would like to thank all the previous posters for their answers they
confirm what I was suspecting regarding this aspect of the Java
language definition.

Thibault
 
C

Chris Uppal

Patricia said:
Once it is recognized that the grammar is not the whole story about what
is and is not a program, it is natural to use whichever expresses an
idea more simply and clearly out of grammar and rules. In this case, it
might be possible to put the entire rule in the grammar, but it would
mean maintaining, throughout the expression grammar, a distinction
between expressions that represent variables and expressions that don't.

Also, it can be easier to provide meaningful error messages if an "almost"
acceptable construction is detected by semantic analysis than if it were just
rejected by the parser. "I know what you are trying to say, but you can't do
it" is better than "WTF?!?"...

The authoritative grammar in the JLS

Which grammar is that then ? ;-)

(There are two, and at least up until JLS2 they didn't define the same
language -- I haven't checked JLS3)

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,813
Messages
2,569,696
Members
45,483
Latest member
TedDvb6626

Latest Threads

Top