in Compiler Design edited by
21,507 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.5k 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

32 Comments

sir this code. plz have a look at it . on geeks ide it says 

Compile Errors and Warnings:

2:11: error: expected ')' before '=' token
 void fro(i=0;i<2;i++)

#include <stdio.h>
void fro(i=0;i<2;i++)
{
    printf("this is a function");
    
}
int main() {
    
    return 0;
}

what u think where i m wrong can u plz correct this and provide a calling mechanism , 

0
0
you have used ";" instead of ",". Moreover you are doing function declaration and not function call.
2
2
got it . thanks .
1
1
Even if i used

fro ( i=0;i<=n;i++) ; ----this cant be function declaration

Function prototyping is

#include <stdio.h>

int area (int , int ) ;

ryt ?
0
0

@Arjun sir plz reply

when i ran this exact code on Dev C++ it gave an error of undefined reference to `fro'.

0
0
As a CS student you should not use an IDE- because then you can't answer these questions. In IDE when we click compile, it does compilation to object module and then linking to produce executable. The error you got is from linking part. Question asks only till compilation to object module.
29
29
ok sir,thanks.
0
0
Sir here in loop.. they are not seperated by ;

So isn't it a syntactic error....?
0
0
@Arjun Sir. Can you please tell from where I can get the standard C compiler to solve such questions. Mention url if possible.
0
0
int main() { 	        /*Line 1 */
int I, N;	                /*Line 2 */
fro (I=0; I<N; I++);	/*Line 3 */
}

this will cause compilation error right and not lexical error?

0
0
edited by

YOUR ANSWER is wrong as it is not compilation error. here , there  must be LEXICAL and Syntactic error. I tell you WHY...

When lexical analyzer makes the token , it also updates the Symbol table. Symbol table contains the information of all declared variables and function names with their datatype. Now , at declaration time no variable named as fro is declared , so there will be no entry present in the symbol table now, when the lexical analyzer comes to line 3 of the program , it gets fro ,now YOU are saying that it considered it as token but it is false , when it gets to fro ,it first check in Symbol Table. Whether actually Is there a variable named as fro is present but it wouldn't find then it generates an error.i.e LEXICAL ERROR. In reality, Lexical analyzer works like that if it finds the variable on the way it then check in its presence in Symbol Table.....

Becoz  It is a common sense that if you are using a variable then definitely above you have declared it.

THAT IS THE REASON WHY, before writing main we have to do funcn declaration becoz to make a entry in symbol table , so, that while execution if it found the funcn call in main , then it will be aware of that it exists down there unless it will show lexical error  as "NO FUNCTION DECLARATION"

Now after Lexical Analysis , if in some case the error gets undetected then definitely reach to syntactic error where it don't match to for loop CFG.

ANSWER is (D)

–1
–1

Now , at declaration time no variable named as fro is declared , so there will be no entry present in the symbol table now, when the lexical analyzer comes to line 3 of the program , it gets fro ,now YOU are saying that it considered it as token but it is false , when it gets to fro ,it first check in Symbol Table. Whether actually Is there a variable named as fro is present but it wouldn't find then it generates an error.i.e LEXICAL ERROR

For lexical analyzer fro is also a valid token , so i dont think it is a lexical error in any case. It must be syntactic error.

0
0
@varun good. Your common sense tells that you do not know of the compilation phases. Well that is common and so you have common sense. To check if a variable is declared before one needs the power of a linear bounded automata -- this makes C grammar context sensitive and also can only be done in the semantic phase.
21
21
@sushmita Syntax error -- is caught by syntax analysis which works on C grammar and also must be done by a PDA. If you add a lexical token at invalid position syntax error happens -- like doing a + / b; But if we do a + *b, this is a valid syntax as * gets treated as a pointer dereference.  Coming to this question, "fro" is treated as a function call, though from C99 standard onwards every function must be declared before being called. So, in the current standard, this code can cause a semantic error -- but definitely not lexical/syntax errors. I have added the outputs in the answer now.
9
9

@ arjun sir, i have replaced , with ; in the original code, in case of ; how can fro be treated as a function call?

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

please check it sir.

3
3
@sushmita No syntax of C language now matches that -- so gives a syntax error.
5
5

the string fro (I=0; I<N; I++); cannot be generated by any valid grammer rule of C  programming language hence the parser cant parse it (reduce it) so it is a syntax error 

11
11
Sir, constant on left hand side of assignment will give syntax error or semantic error ?
0
0
const data type can be initialised once  and then when we reinitialise it the previous syntax rule applied so it comes down to the problem of identifying WcW which cannot be done by parser hence semantic error
0
0

For more information on lexical,syntax and semantic errors https://gateoverflow.in/30530/syntax-and-semantic-errors

1
1
nice catch arjun sir
0
0
Thanks a lot sir
1
1

@Arjun Sir, C is context sensitive and since we have not declared fucntion fro, can C not check that fro is absent in symbol table? Ideally, why isn't there a semantic error Sir? 

0
0

Correct me if I am wrong. I think we will get syntax error. Compiler will treat line 3 as a function call but what will it think of the 1st parameter? Compiler knows that 2nd parameter has a boolean value and third parameter has integer value (exact values may be determined at runtime but type of value is determined at compile time). But I = 0, is an assignment, not an expression. Since parameter lists can only have expressions or identifiers but not assignments, won’t we get a syntax error? Please guide me in the right direction

ping @Arjun , @sushmita

0
0

As already mentioned by Arjun Sir, line 3 would give warning: implicit declaration of function. It is generated when the program has a function for which the compiler has not seen a declaration ("prototype") yet. 

See example here: https://stackoverflow.com/questions/8440816/warning-implicit-declaration-of-function

 

1
1
Great Answer sir!!!
0
0

if that’s the case then why is the answer for the below gate question is compiler error??

double foo (double);		/* Line 1 */
int main() {
    double da, db;
    //input da
    db = foo(da);
}
double foo (double a) {
    return a;
}

The above code compiled without any error or warning. If Line 11 is deleted, the above code will show:

  1. no compile warning or error

  2. some compiler-warnings not leading to unintended results

  3. some compiler-warnings due to type-mismatch eventually leading to unintended results

  4. compiler errors

0
0

@biswajitbora that’s because default return type of a function is int, at line db = foo(da) since it is called without being defined the compiler will take default return type as “int” but foo is returning “double” so there will be compiler error

0
0

@Arjun , @gatecse  @Abhrajyoti00 sir

In the answer of @Arjun sir 

arjun@linux:~$ gcc -c -ansi chk.c    

is not showing any error(why).

0
0

@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