3
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ Programming is offtopic here, and I think implementation details of particular compilers are as well. Community votes, please: offtopic? Migrate to Stack Overflow? $\endgroup$ – Raphael Mar 27 '18 at 14:13
  • 1
    $\begingroup$ @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. $\endgroup$ – xskxzr Mar 27 '18 at 15:38
  • $\begingroup$ @xskxzr In that case, I'd appreciate an edit that highlights the conceptual issue here, as opposed to "what does gcc do?" $\endgroup$ – Raphael Mar 27 '18 at 21:56
  • $\begingroup$ @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. $\endgroup$ – rici Mar 28 '18 at 15:56
4
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – John P Mar 27 '18 at 13:57
  • $\begingroup$ @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. $\endgroup$ – rici Mar 28 '18 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.