in Compiler Design retagged by
17,103 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
in Compiler Design retagged by
by
17.1k views

4 Comments

According to GATE final answer key, answer is C: Semantic analyzer

2
2
Is there any deterministic way to know for any given program which phase will detect an error?
0
0
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.
0
0

7 Answers

36 votes
36 votes
Best answer

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

14 Comments

But unless we make sure that it is not a primitive data type, how can an error be detected? A syntax analyzer cannot differentiate between keyword and identifier right? only Semantic analyzer can do that once it realizes and reduces the statement into starting symbol, which is when we try to make sense of the program i.e. in semantic analysis phase?
2
2
A syntax analyzer cannot decide between a keyword and an identifier? Well this is wrong and that's why number of keywords is finite in most languages. They are recognized by the lexical analyzer.

Actually one can try lex and yacc on C grammar to give a formal proof of this. They are available online, but may not run straight away. Worth a try though.
1
1
I was right :)
2
2
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?
0
0

syntax analyzer will only focus on the syntaxes right? and here in the given sample code all the syntaxes are correct but as the Integer is not declared before use  so that’s why there is semantic error.

correct me if there is anything wrong in my reasoning!!

0
0

My reasoning for the answer being semantic phase is like:

In syntax analysis we get error if we can not generate the syntax tree,right? So here it is from the grammar created by, lets say  developers of C.

Then as per the ”Integer x”  this means identifier-identifier side by side. which can generate syntax tree and does not give error.

[ Why?  If we have defined Integer as alias to int  (like typedef int Integer) then as per the program it should be correct and should not give error rather it will check this at the time of semantic phase ]

Now identifier-identifer generates the syntax tree – then in the next phase (i.e Semantic phase) it will check if the type.identifier ←  type.datatype

Hence at this phase this error will be detected !

1
1
It is syntax analyzer in my opinion.
0
0

will you pls elaborate more ?

0
0

 Quoted from answer… Due to this.

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.

whenever input is passed from syntax analyser it checks from C language CFG and if error is generated no parse tree is formed.

1
1

but then why the answer here is semantic ?? pls refer my comment once. 

https://gateoverflow.in/357537/Gate-cse-2021-set-2-question-3?show=365144#c365144

is anything wrong with my reasoning?

 

1
1

syntax analyzer will only focus on the syntaxes right? and here in the given sample code all the syntaxes are correct but as the Integer is not declared before use  so that’s why there is semantic error.

 How can you say that… even if it is declared it is generally there.

Suppose a program,

main(){

int b=5,c=7;

a=b+c;          // Here you can say integer a is not declared before use

}

Here you are getting the very first occurrence of integer (Look below) and you decided it is not declared before use… :(

  1. int main () {
  2. Integer x;
  3. return 0;
  4. }
1
1

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😥.

 

1
1
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.
2
2

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.

1
1
18 votes
18 votes
Syntax Analyzer throws error for above

4 Comments

@kshitij86 can you link to that stack overflow thread?
1
1
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?
0
0
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

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.

4
4
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