Sam Dennis wrote:
Jeremy Yallop wrote: Write a program that takes a C program in source form as input and
prints the source code for a program with equivalent behaviour, but
without semicolons, on standard output.
There is one really serious problem with that, other than string
literals and character constants (and being a stupid idea in the
first place): declarations are terminated with a semi-colon.
String literals and character constants are not a problem if you don't
mind restricting yourself to a particular character set. Declarations
are admittedly rather difficult to handle, but by no means impossible.
Here are a couple of clues:
int foo()
{
int x = 3;
float y = 2.0;
...
return something;
}
can be transformed to
void foo_helper(int x, float y, int *rv)
{
...
if (*rv = something) { }
}
void foo(int *rv)
{
if (foo_helper(3, 2.0, rv)) { }
}
It might appear that a (non-definition) declaration is required for at
least one of two mutually recursive functions, but that's not the
case:
void one()
{
if (((void (*)())two)(), 0) { }
}
void two()
{
if (one(), 0) { }
}
I'm pretty sure that it can be done, although I welcome
counterexamples . I'm sorry that you thought the problem was stupid; I
thought it was considerably more interesting than the usual "without
using a semicolon" sort of questions that come up fairly frequently,
and (if solved) it has the additional advantage of making it
unnecessary to ever have to think of answers to those questions again.
I think that a program I'm working on now will be able to solve the
problem (and many others) when finished, by the way, but it's not
written in C and it won't be ready for a couple of months at least.
Jeremy.