3

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

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

$\text{What will be the type of error?}$

$\text{a) Lexical}$,

$\text{b) Syntax}$,

$\text{c) Semantic}$,

$\text{d) None}$ $\tag {GATE-CS 2021}$

According to me, Syntax Analysis phase should detect the error as there is no grammar in C which produces Integer x or in tokenized form id id. Any help is appreciated. :)

John L.
  • 39,205
  • 4
  • 34
  • 93
Curiosity
  • 61
  • 2

3 Answers3

4

Your statement Integer x; is syntactically identifier identifier; which is a perfectly fine variable declaration. Semantically, the first identifier must be defined as a type, and the second identifier must not be defined as a variable in the same scope. The first condition fails, therefore ...

And you might want to download a copy of the C Standard to check if int main() is acceptable, or whether it must be int main(void), which is preferable and might be required.

gnasher729
  • 32,238
  • 36
  • 56
1

In ANSI C syntax, as you say, there is no such construct as Integer, but only int. So your compiler should highlight a Syntax Error.

The grammar would be something as:

keyword = int | void | return | ...
expression = keyword id

Since expression won't match any keyword, the compiler stops at syntactic level.

purple_lolakos
  • 231
  • 1
  • 5
0

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?

The answer to this is most likely SEMANTIC ANALYSIS.