Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

(expression)++

Thread view: 
thibault.langlois@gmail.com - 10 Oct 2006 14:30 GMT
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/
Matt Humphrey - 10 Oct 2006 15:27 GMT
> Hello,
> I see from several sources(*) the following as part of the definition
[quoted text clipped - 14 lines]
> 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 matth@ivizNOSPAM.com http://www.iviz.com/
Robert Klemme - 10 Oct 2006 15:46 GMT
> Hello,
> I see from several sources(*) the following as part of the definition
[quoted text clipped - 20 lines]
> 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
Patricia Shanahan - 10 Oct 2006 16:56 GMT
>> Hello,
>> I see from several sources(*) the following as part of the definition
[quoted text clipped - 12 lines]
>> Is it impossible to define the java syntax such that (a + b)++ is not
>> allowed ?

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
thibault.langlois@gmail.com - 10 Oct 2006 19:17 GMT
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
Chris Uppal - 11 Oct 2006 10:21 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.