edited by
21,329 views
80 votes
80 votes

Consider line number $3$ of the following C-program.

int main() {                /*Line 1 */
    int I, N;               /*Line 2 */
    fro (I=0, I<N, I++);	/*Line 3 */
}

Identify the compiler’s response about this line while creating the object-module:

  1. No compilation error
  2. Only a lexical error
  3. Only syntactic errors
  4. Both lexical and syntactic errors
edited by

9 Answers

Best answer
117 votes
117 votes

C language allows only certain words in it- these are called tokens. If we input any invalid tokens it causes lexical error. 

eg:
$44a44$
causes lexical error as in C as an alphabet cannot come in between digits. 

Syntactic error is caused by bad combination of tokens. For example, we cannot have a constant on the left hand side of an assignment statement, a for loop must have two expressions inside $()$ separated by semi colon etc. 

In the given question, line $3$ won't cause a lexical error or syntactic error. The statement will be treated as a function call with three arguments. Function definition being absent will cause link time error, but the question asks only for compile-time errors. So, $(a)$ must be the answer. 

PS: Implicit function declaration was removed from $C99$ standard onwards. As per current standard, we should not use a function without declaration. Still, we cannot guarantee "compilation error"- just expect compiler warnings in C. In C++ this should produce a compilation (semantic) error. The output of compiling the above code using different standards are given below:

arjun@linux:~$ gcc -c chk.c
chk.c: In function ‘main’:
chk.c:3:2: warning: implicit declaration of function ‘fro’ [-Wimplicit-function-declaration]
  fro (I=0, I<N, I++); /*Line 3 */
  ^
arjun@linux:~$ gcc -c -ansi chk.c
arjun@linux:~$ gcc -c -std=c99 chk.c
chk.c: In function ‘main’:
chk.c:3:2: warning: implicit declaration of function ‘fro’ [-Wimplicit-function-declaration]
  fro (I=0, I<N, I++); /*Line 3 */
  ^
arjun@linux:~$ gcc -c -std=c11 chk.c
chk.c: In function ‘main’:
chk.c:3:2: warning: implicit declaration of function ‘fro’ [-Wimplicit-function-declaration]
  fro (I=0, I<N, I++); /*Line 3 */

http://stackoverflow.com/questions/15570553/lexical-and-semantic-errors-in-c

edited by
31 votes
31 votes

Answer is A. There is no error in the above code. Actually it is a link error ... Here compiler thinks fro is a function which is not declared , hence it will not produce any error .. It will only throw a warning in C .. 

Lexical analyser error comes when we declare a integer as 123zx . it is invalid to declare a integer like this.

Syntax analyser is related to syntax.

There is hence no error. Only link error because no function declaration has been found with 3 arguments.

11 votes
11 votes

Option A is correct.

Option B is wrong. Lexical Analyser would not throw any errors! Even if there were spelling errors, it'd simply correct them. 

Option C is wrong because fro is seen as a function. A function's arguments are separated by commas, which is the case here. There's no syntax error.

Consequently, Option D is wrong, too.

 

However, if Line 3 ran like this:

fro (I=0; I<N; I++);

Then, Option C would be correct, because a function's arguments aren't separated by semi-colons, as per the parsing rules — which gives a syntax error.

7 votes
7 votes

Answer : It is not a lexical error because lexical analyzer will treat fro as a token . I think it should be a syntactic error because few days ago i was confused between these 2 terms and i have posted this question here and i got answer in this way that syntactic errors can't be caught by c compiler because it does not violates all C -programming rules it is the responsibility of programmer to look at them and fix them and i believe if we write a program and by mistake we had written fro instead of for them we have to fix it .

Reference of the question I asked : https://gateoverflow.in/49019/compiler-phase-semantic-phase

Answer:

Related questions

31 votes
31 votes
2 answers
1
go_editor asked Nov 27, 2016
11,341 views
Consider the following expression grammar. The semantic rules for expression evaluation are stated next to each grammar production.$$\begin{array}{l|l} E\rightarrow numbe...
24 votes
24 votes
2 answers
2
34 votes
34 votes
5 answers
3
0 votes
0 votes
1 answer
4
go_editor asked Mar 26, 2020
1,005 views
Which of the statements related to Compilers is wrong ?Lexical analysis is breaking the input into tokensSyntax analysis is for parsing the phraseSyntax analysis is for a...