• retagged by
33,525 views
46 46 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

12 Answers

Best answer
58 58 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
20 20 votes
Syntax Analyzer throws error for above
18 18 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

1 flag:
✌ Edit necessary (Sameer Bawane “we consider errors with respect to C lang., here u gave reference of Java”)
8 8 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
5 5 votes
Let’s dry run the lexical analysis on this –

Integer is a Lexeme of token type “identifier”, so it x. So, for this line of code, lexical analyzer will generate the token stream – identifier identifier EOS. Here EOS signifies End of Statement.

Now, ask yourself if CFG for C supports such a word? No it doesn’t – it will produce a Syntax Error. However, this doesn’t mean that there won’t be a Semantic Error – Integer is an identifier used before definition. As per Syntax Directed Translation scheme, both these error would be reported. An unambiguous question would have been – Which error is reported first? The answer to this is Syntax Error.
5 5 votes

The compiler will consider Integer as a variable so it will be detected in Semantic Analyzer(Since Undeclared Identifier).

Answer:
Position:
Show:

Related questions

19 19 votes
6 answers 6 answers
13.3k
13.3k views
Arjun asked Feb 18, 2021
13,287 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...
42 42 votes
3 answers 3 answers
20.5k
20.5k views
Arjun asked Feb 18, 2021
20,533 views
​​​​​​Consider the following $\text{ANSI C}$ code segment:z=x + 3 + y->f1 + y->f2; for (i = 0; i < 200; i = i + 2) { if (z i) { p = p + x + 3; q = q + y->f1; } else { p ...
26 26 votes
4 answers 4 answers
13.7k
13.7k views
Arjun asked Feb 18, 2021
13,732 views
For a statement $S$ in a program, in the context of liveness analysis, the following sets are defined:$\text{USE}(S)$ : the set of variables used in $S$$\text{IN}(S)$ : t...
22 22 votes
3 answers 3 answers
14.2k
14.2k views
Arjun asked Feb 18, 2021
14,197 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 \rig...