# Why doesn't the C/C++ compilers create different tokens for different types of numbers?

Based on GeeksforGeeks and many other sites, the C/C++ compilers will create the same token for float/int etc.

However if we have something like this:

int A[10.5];


then will there be a parser error or semantic error?

The book that I'm reading says the parser rather than the semantic routines will detect it, but after the lexical analysis wouldn't that be converted to this:

A[Constants];


meaning the 10.5 will be converted to a Constants token based on that website? Therefore the parser will not notice this is an error because both of 10 and 10.5 will be Constants!

So which one is right? Will the parser or one of the semantic routines detects this error? Do the C/C++ compilers create the same tokens for int numbers and floats?

• Programming is offtopic here, and I think implementation details of particular compilers are as well. Community votes, please: offtopic? Migrate to Stack Overflow? – Raphael Mar 27 '18 at 14:13
• @Raphael I think this question is not a programming question, and may be related to principle of compiling, which is indeed part of computer science. – xskxzr Mar 27 '18 at 15:38
• @xskxzr In that case, I'd appreciate an edit that highlights the conceptual issue here, as opposed to "what does gcc do?" – Raphael Mar 27 '18 at 21:56
• @xskxzr: I don't see any principles here. C is very idiosyncratic, but then all languages have their idiosyncrasies. In particular, the question "in what phase is an error detected?" and eve "what phases are there?" are wholly pragmatic. – rici Mar 28 '18 at 15:56

This is in fact an implementation detail of the compiler. The page you referenced only shows one way of assigning types to tokens, while there are also others. The compiler could have a lexer that distinguishes between integral and non-integral constants and the parser then cannot match the declaration

int A[10.5];


to any rule. But giving an error message stating exactly this to the programmer is not very helpful. Modern C++ compilers have quite complex parsing and compiling routines that give more information in case of errors. As an example, GCC will yield:

me@my-computer:/tmp\$ g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:9:15: error: size of array ‘A’ has non-integral type ‘double’
int A[10.5];
^


So you will get quite a good error message, and for obtaining it, some semantic interpretation was actually performed.

• So overall in the modern day compiler for C/C++ this type of errors will be detected by the semantic routines and not the parser(using grammar) correct? and the lexical analyzer will create different token for ints and floats right? – John P Mar 27 '18 at 13:57
• @JohnP: This particular error must be detected semantically because C restricts array dimensions to be "integer constant expressions", not integer constants. For example, N_TERMS * 2 + 1" is legal if N_TERMS` is an enum value. – rici Mar 28 '18 at 16:00