4 ms·
The constant expressions that #if understands are a very restricted subset of C. It's best thought of as a small grammar inlined into the preprocessor rather th
by barrkel 5y ago
The constant expressions that #if understands are a very restricted subset of C. It's best thought of as a small grammar inlined into the preprocessor rather than intermingled with the C parser, since the expressions can only evaluate preprocessor definitions and not any C declarations.
I've written several C-style preprocessors. They can be fiddly to write, but you shouldn't think of them as lexing to the same lexemes as a C parser needs. You can take a lot of shortcuts there.
Things like '<' and '>' being overloaded is quite common, even in lexers. You see it with nested templates / generics in C++, Java, C# etc. There are simple tricks for dealing with the ambiguity between '>>' vs '>' '>' in 'T<U<V>>'.
(FWIW, having written some of those preprocessors, I think they're a bad idea. It's notable that other languages haven't copied the idea, other than C++ which inherited it.)