11,001 views

Consider the following $\text{ANSI C}$ program:

int main () {
Integer x;
return 0;
}

Which one of the following phases in a seven-phase $C$ compiler will throw an error?

1. Lexical analyzer
2. Syntax analyzer
3. Semantic analyzer
4. Machine dependent optimizer

Is there any deterministic way to know for any given program which phase will detect an error?
It is a statement similar to a type declaration. Int a, char x etc. But with a unknown type integer which is not defined. So unknown reference to a data type is used in the above statement so it's semantic error. I didn't see it at once but it's visible after watching some references.

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.

[email protected]:~$cat p1.c int main() { Integer x; return 0; } [email protected]:~$ clang p1.c -c -Xclang -dump-tokens
int 'int'	 [StartOfLine]	Loc=<p1.c:1:1>
l_paren '('		Loc=<p1.c:1:9>
r_paren ')'		Loc=<p1.c:1:10>
l_brace '{'	 [StartOfLine]	Loc=<p1.c:2:1>
semi ';'		Loc=<p1.c:3:11>
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.

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

by

ASNR1010

i got ur point now..now i also believe that this must be a syntax error and not semantic. but what about the official answer key😥.

In above explanation given by @gatecse sir it can be either one of them. So, it is an ambiguous question but I feel syntax more because it is coming earlier after all.

ASNR1010

Yes it is an ambiguous  question. and so many things are not properly defined in the question so one can assume anything . and on the basis of different assumptions the answers will definitely vary.

Syntax Analyzer throws error for above

Can U just tell that, Is gateoverflow Rank predictor is now perfectly set according to gate Answer Key(except Challeged ques) ,ie, will expect challenge ques will there be no more variation in marks,Am I correct?

Answer will be C  i.e. Semantic Analyzer because Integer is not defined so compiler will treat it as unknown reference.

See example no 4

https://www.inf.unibz.it/~calvanese/teaching/04-05-ip/lecture-notes/uni09/node4.html

### 1 comment

edited by

I think this is a syntactic error, which compiler can indeed detect and report.

A semantic error is something that compiler is fine with; but does not do what you want. Semantic errors are part of your algorithm more than your actual syntax.

As per the given source – https://www.inf.unibz.it/~calvanese/teaching/04-05-ip/lecture-notes/uni09/node4.html ; It seems to be semantic error also;

But again if this error is caught initially at syntax analysis phase ( at compile time) no need to check next compiler phase.

So By Compiling everyone noticed that there is an error saying undeclared identifier. This error comes under Semantic Error. So the answer Should be Semantic Error