retagged by
16,963 views
21 votes
21 votes

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
retagged by

7 Answers

Best answer
36 votes
36 votes

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

selected by
18 votes
18 votes
Syntax Analyzer throws error for above
14 votes
14 votes

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

5 votes
5 votes
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

 

Answer :- C
Answer:

Related questions

9 votes
9 votes
4 answers
1
Arjun asked Feb 18, 2021
6,652 views
In the context of compilers, which of the following is/are $\text{NOT}$ an intermediate representation of the source program?Three address codeAbstract Syntax Tree $\text...
8 votes
8 votes
1 answer
4
Arjun asked Feb 18, 2021
6,468 views
Consider the following augmented grammar with $\{ \#, @, <, >, a, b, c \}$ as the set of terminals. $$\begin{array}{l} S’ \rightarrow S \\ S \rightarrow S \# cS \\ S \r...