retagged by
17,238 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

4 votes
4 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.
2 votes
2 votes

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

0 votes
0 votes
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.
Answer:

Related questions

10 votes
10 votes
4 answers
1
Arjun asked Feb 18, 2021
6,799 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,566 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...