in Compiler Design edited by
21,308 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
in Compiler Design edited by
21.3k views

4 Comments

This is nothing but a Linking error

as because during compilation compiler will think that it is a Function

But during Linking there will be no module named as that so, Linker is failed.
6
6

loop statement is written as fro (I=0, I<N, I++);. This contains both lexical and syntactic errors:

  1. Lexical error: The keyword "for" is misspelled as "fro", which is not recognized by the C compiler. This is a lexical error because it involves incorrect spelling or misuse of language elements.

  2. Syntactic error: The loop statement has three expressions enclosed in parentheses, separated by commas. However, the correct syntax requires semicolons (;) instead of commas (,) to separate the expressions. So the correct form should be for (I=0; I<N; I++).

Therefore, the compiler will report both lexical and syntactic errors in line 3 of the code. The correct option would be D. Both lexical and syntactic errors.

0
0

@Subhasish012 its wrong bro.

0
0

9 Answers

117 votes
117 votes
Best answer

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
by

4 Comments

@Overflow04 That’s what Sir has explained. It shows warning but it is not compilation error. 

It is a linking error which doesn’t appear during compile time.

0
0

Question:Why always default is 0,0 printed. I tried it many times but default is only 0,0(what I know :it should print any value).

Why bool is creating error as i<n will return a bool value.

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

by

4 Comments

so invalid variable declaration will generate lexical errors always ?

like 

  1. 2foo (must not begin with a digit)
  2. my foo (spaces not allowed in names)
  3. $foo ($ not allowed -- only letters, and _)
  4. $foo ($ not allowed -- only letters, and _)
  5. 44a44(  alphabet cannot come in between digits )
1
1
Sir, please correct me if I'm wrong , but isn't linker used to link external files and/or libraries? So how can it be a Link error?
0
0

@amitqy 

"my foo" it will not give lexical error..

also check your 3rd and 4th point

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