This question is difficult to answer from a practical point of view because most of the C compilers (even other language compilers) do not follow the classical ordering of compilation phases. Since this is a one-mark question ignoring the practical implementations and going by just theory answer will be syntax error. Because there are no lexical errors and “Integer” and “x” get read as identifiers as shown in the following output.
arjun@Armi:~$ cat p1.c
int main()
{
Integer x;
return 0;
}
arjun@Armi:~$ clang p1.c -c -Xclang -dump-tokens
int 'int' [StartOfLine] Loc=<p1.c:1:1>
identifier 'main' [LeadingSpace] Loc=<p1.c:1:5>
l_paren '(' Loc=<p1.c:1:9>
r_paren ')' Loc=<p1.c:1:10>
l_brace '{' [StartOfLine] Loc=<p1.c:2:1>
identifier 'Integer' [StartOfLine] [LeadingSpace] Loc=<p1.c:3:2>
identifier 'x' [LeadingSpace] Loc=<p1.c:3:10>
semi ';' Loc=<p1.c:3:11>
return 'return' [StartOfLine] [LeadingSpace] Loc=<p1.c:4:2>
numeric_constant '0' [LeadingSpace] Loc=<p1.c:4:9>
semi ';' Loc=<p1.c:4:10>
r_brace '}' [StartOfLine] Loc=<p1.c:5:1>
eof '' Loc=<p1.c:5:2>
Now, when this stream of tokens get passed to the syntax analyser – we have an identifier followed by another identifier which is not valid in C syntax – so syntax error. And this must be the answer here though we can argue for semantic error as well as follows.
Now consider a typedef usage like “typedef int Integer”. Now, this can be implemented by the compiler in multiple ways. One option is to immediately change the token type of “Integer” from identifier to the given “type”. Otherwise the syntax check can go with the AST generation. But if we go by the classical meaning of the compilation phases here we are matching a string which means it is a semantic phase.
Correct Answer: Syntax Analysis/Semantic analysis
More read: https://stackoverflow.com/questions/66290247/integer-x-is-syntactic-error-or-semantic-error
Official answer given in GATE key is “Semantic analysis” – but even the best compiler professors won’t conclude on that.
Though this was a bad question and even worse answer key, lets use it to learn something useful.
The following three flags will force cc
(C compiler) to check that your code complies to the relevant international standard, often referred to as the ANSI standard, though strictly speaking it is an ISO standard.
-Wall
Enable all the warnings which the authors of cc
believe are worthwhile. Despite the name, it will not enable all the warnings cc
is capable of.
-ansi
Turn off most, but not all, of the non-ANSI C features provided by cc
. Despite the name, it does not guarantee strictly that your code will comply to the standard.
-pedantic
Turn off all cc
's non-ANSI C features.
Without these flags, cc
will allow you to use some of its non-standard extensions to the standard. Some of these are very useful, but will not work with other compilers—in fact, one of the main aims of the standard is to allow people to write code that will work with any compiler on any system. This is known as portable code.
https://docs.freebsd.org/en_US.ISO8859-1/books/developers-handbook/tools-compiling.html