Which stage of the compiler detects the following errornous program?

86 views Asked by At

I'm having some trouble in understanding the differences between different errors in the complier phases, for instance this program:

int main()
{
      int x = 5;
      int x = 6;
}

My guess is that this would fail in the semantic analysis phase, but I'm not that certain (is it possible that it would fail in the lexical analysis phase as a duplicate token for (int x) would be generated?)

Appreciate your help.

1

There are 1 answers

0
AudioBubble On

You're right, this program is both lexically and syntactically correct but not semanticaly, assuming that the language you describe here is standard C or C++.

C/C++ does not allow redefinition of variables - that's semantics. If your language allowed variable redefinitions, it would be a semantically correct program.