The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+49 votes
5k 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.4k points)
edited by | 5k 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

+66 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 (420k points)
edited by
+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.

+2
@sushmita No syntax of C language now matches that -- so gives a syntax error.
+7

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? 

+19 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.4k 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.1k 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 Junior (733 points)
Answer:

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,309 questions
55,747 answers
192,248 comments
90,537 users