The Gateway to Computer Science Excellence
+51 votes
5.3k views

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 by Boss (13.5k points)
edited by | 5.3k views
+2
OPtion C syntactical error
0
what is lexical error
+5
syntax errors- due to not having proper syntax like paranthesis not closed properly or contan assignment

lexical error- define int  as '12wde'   these two errors are occured during compile time

symantic errors- comes due to like we find area of square  and we do A= a+a // here must be multiply      and this error leads to logical error bcz what we expect the area of square we did not get it.

run time errors- occurs due during execution our turbo c are not work properly.
0

@krish Your explanation for semantic errors is wrong. Logical error has nothing to do with compiler but programmer himself/herself. Semantic analysis cares for errors found during type-checking. And "type checking" is one example.

 

4 Answers

+75 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:

[email protected]:~$ 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 */
  ^
[email protected]:~$ gcc -c -ansi chk.c
[email protected]:~$ 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 */
  ^
[email protected]:~$ 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

by Veteran (424k points)
edited by
0

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 , 

+1
you have used ";" instead of ",". Moreover you are doing function declaration and not function call.
+1
got it . thanks .
0
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

@Arjun sir plz reply

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

+14
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.
0
ok sir,thanks.
0
Sir here in loop.. they are not seperated by ;

So isn't it a syntactic error....?
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
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?

–2

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)

0

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.

+13
@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.
+4
@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.
+2

@ 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
@sushmita No syntax of C language now matches that -- so gives a syntax error.
+8

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 

0
Sir, constant on left hand side of assignment will give syntax error or semantic error ?
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
+1

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

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

@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? 

+21 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 Veteran (50.6k points)
0
thanks bro, most appropriate
0
why int a#;  does not have a lexical error. As I know a# is malformed here.

https://stackoverflow.com/questions/15570553/lexical-and-semantic-errors-in-c
+1

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

@amitqy 

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

also check your 3rd and 4th point

+6 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

by Boss (45.3k points)
+2 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.

by Active (1.9k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,647 questions
56,461 answers
195,358 comments
100,247 users