ternary expression validity

S

slashdotcommacolon

Hello, I've been asked to port some old code to a new platform. We have
a vendor-supplied compiler that is supposed to be ansi compliant, but
it refuses to compile the following code:

$ cat test.c
int main(int argc, char **argv)
{
static int a, b, c, d, e, f, g, h, i, j, k;

return k = a ? b++, c : d ? e++, f : g ? h++, i : j;
}

The error is 'ternary operation `?:` unterminated', however the GNU
compiler accepts it even in 'pedantic' mode. It's not a problem, as I
just rewrote the code, but I'm curious if this code (as i suspect)
looks acceptable to you language lawyers, if so I'll file a bug report
with the vendor.

Thanks guys.
 
R

Robert Gamble

Hello, I've been asked to port some old code to a new platform. We have
a vendor-supplied compiler that is supposed to be ansi compliant, but
it refuses to compile the following code:

$ cat test.c
int main(int argc, char **argv)
{
static int a, b, c, d, e, f, g, h, i, j, k;

return k = a ? b++, c : d ? e++, f : g ? h++, i : j;
}

The code above is valid and should compile properly. I happen to think
that the last statement is very poorly written though, I wouldn't want
to see the rest of the code written by the guy who came up with this
gem. This is the type of code that leads to bugs and maintenance
nightmares, especially if you forget that the tertiary operator is
right-to-left associative.

Robert Gamble
 
C

Chuck F.

Robert said:
The code above is valid and should compile properly. I happen to
think that the last statement is very poorly written though, I
wouldn't want to see the rest of the code written by the guy who
came up with this gem. This is the type of code that leads to
bugs and maintenance nightmares, especially if you forget that
the tertiary operator is right-to-left associative.

Yeah. What he said. In spades.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
S

Sjouke Burry

Hello, I've been asked to port some old code to a new platform. We have
a vendor-supplied compiler that is supposed to be ansi compliant, but
it refuses to compile the following code:

$ cat test.c
int main(int argc, char **argv)
{
static int a, b, c, d, e, f, g, h, i, j, k;

return k = a ? b++, c : d ? e++, f : g ? h++, i : j;
}

The error is 'ternary operation `?:` unterminated', however the GNU
compiler accepts it even in 'pedantic' mode. It's not a problem, as I
just rewrote the code, but I'm curious if this code (as i suspect)
looks acceptable to you language lawyers, if so I'll file a bug report
with the vendor.

Thanks guys.
You should send this in as obfuscated code, anybody
writing that kind of code, should not be allowed near
a computer,and waste his employers money.
 
P

Peter Nilsson

Hello, I've been asked to port some old code to a new platform. We have
a vendor-supplied compiler that is supposed to be ansi compliant, but
it refuses to compile the following code:

$ cat test.c
int main(int argc, char **argv)
{
static int a, b, c, d, e, f, g, h, i, j, k;

return k = a ? b++, c : d ? e++, f : g ? h++, i : j;
}

The error is 'ternary operation `?:` unterminated', however the GNU
compiler accepts it even in 'pedantic' mode. It's not a problem, as I
just rewrote the code, but I'm curious if this code (as i suspect)
looks acceptable to you language lawyers, if so I'll file a bug report
with the vendor.

I would hazzard a guess that the compiler writer is not aware that the
middle
term of the conditional operator is any other expression...

conditional-expression:
logical-OR-expression
logical-OR-expression ? expression : conditional-expression

In other words...

a ? b, c : d

....is equivalent to...

a ? (b, c) : d
 
P

pete

Robert said:
tertiary operator

There are unary, binary, and ternary operators,
according to the number of operands of the operator;
rather than primary, secondary, and tertiary.
 

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

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,478
Latest member
ReginaldVi

Latest Threads

Top