Reputation: 1293
Here's a formal grammar brain teaser (maybe :P)
I'm fairly certain there is no context where the character sequence =>
may appear in a valid C program (except obviously within a string). However, I'm unable to prove this to myself. Can you either:
To get your minds working, other combos I've been thinking about:
:-
(b ? 1:-1), !?
don't think so, ?!
(b ?!x:y), <<<
don't think so.
If anyone cares: I'm interested because I'm creating a little custom C pre-processor for personal use and was hoping to not have to parse any C for it. In the end I will probably just have my tokens start with $
or maybe a backquote but I still found this question interesting enough to post.
Edit: It was quickly pointed out that header names have almost no restrictions so let me amend that I'm particularly interested in non-pre-processor code, alternatively, we could consider characters within the <>
of #include <...>
as a string literal.
Re-edit: I guess macros/pre-processor directives beat this question any which way I ask it :P but if anyone can answer the question for pure (read: non-macro'd) C code, I think it's an interesting one.
Upvotes: 1
Views: 96
Reputation: 241731
In addition to all the other quibbles, there are a variety of cases involving macros.
The arguments to a macro expansion don't need to be syntactically correct, although of course they would need to be syntactically correct in the context of their expansion. But then, they might never be expanded:
#include <errno.h>
#define S_(a) #a
#define _(a,cosmetic,c) [a]=#a" - "S_(c)
const char* err_names[] = {
_(EAGAIN, =>,Resource temporarily unavailable),
_(EINTR, =>,Interrupted system call),
_(ENOENT, =>,No such file or directory),
_(ENOTDIR, =>,Not a directory),
_(EPERM, =>,Operation not permitted),
_(ESRCH, =>,No such process),
};
#undef _
const int nerr = sizeof(err_names)/sizeof(err_names[0]);
Or, they could be used but in stringified form:
#define _(a,b,c) [a]=#a" "S_(b)" "S_(c)
Note: Why #a
but S_(c)
? Because EAGAIN
and friends are macros, not constants, and in this case we don't want them to be expanded before stringification.
Upvotes: 1
Reputation: 110108
#include <abc=>
is valid in a C program. The text inside the <...>
can be any member of the source character set except a newline and >
.
This means that most character sequences, including !?
and <<<
, could theoretically appear.
Upvotes: 3