Redirected
in Compiler Design reopened by
5,247 views
6 votes
6 votes

Find the type of error produced by the following C code.

main()

{     in/*comment t x;

      floa/*comment*/t gate;

}
  1. Lexical error
  2. syntax error
  3. both a) and b) 
  4. None of these
in Compiler Design reopened by
5.2k views

4 Comments

why not syntax error?
0
0
@Arjun sir please this
0
0

7 Answers

9 votes
9 votes

There is no lexical issue as the tokens are identified here and all of them follow the naming convention of tokens here ..There is no such violation like :

a) There is some token which starts with the capital letter

b) There is some token which starts with some special character(except underscore) etc.

Hence no lexical error here..

And as far as the comment sign is concerned so it expects the closing comment sign also which is there in the given code..And after the token "gate" , we have the semicolon also.

So no syntax error also.

But there is a semantic error as we have "in" , so compiler treats it as an identifier which is undeclared..

Hence D) is correct answer..

4 Comments

1
1
See syntax error is something related to the correctness of format of statements which are sentences(strings) and which are hence which are verified by CFG of the parser..

Semantic error is that which is associated with the meaning of statement or token instead..Say I have a variable which is undeclared and we use it in code so it will lead to semantic error..This is done using SDD and corresponding SDT associated with each production of CFG of parser..

Hence both are separate things..
1
1

@ Habib

Yes i agree with you...You are saying the "correctness of format of format of statements"

CFG's  are written for each statement like for if else...E-->If id then St | If id then St else St

But what I know id id id is not correct format. There is no CFG written for it.

Identifiers CFG are like E->num , E->E*E , E-> E+E etc..

How parse tree can be made by Syntax analyser...?? and thus syntax error..

Correct me if i am wrong..!!

2
2
1 vote
1 vote

YES IT IS SYNTAX ERROR BECAUSE

FORM /* to last */ taken as comments 

so lexems are in,t, gate,; etc

so it is syntactically incorrect but lexically correct.

Some compilers do allow nesting of comments, but that also causes syntax error here as comments are not properly nested.


Output from gcc:

comment.c:4:2: error: unknown type name ‘in’
  in/*comment t x;
  ^
comment.c:5:19: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘gate’
  floa/*comment*/t gate;

3 Comments

What would be error if it is changed like :

main() {
    in/*comment t x;
    floa*/comment */t gate;
}
0
0
@mcjoshi in the given question it is syntax error because some compiler allow nesting of command line.

But in ur example even command line has no meaning.

So, compiler couldnot identify the lexeme.

causing lexical error.
1
1
/*comment t x;
    floa*/

from /* To */ it will take as comment. and for next */ it will generate Lexical error.

is it right @srestha.

0
0
1 vote
1 vote

Tokens generated in C are=>

main    (      )    {    in    t      gate     ;     }      total  9 tokens, so as tokens are generated successfully so there is no lexical error.

So acoording to my knowledge and CodeBlocks IDE output, there are semantic and syntactic errors.

in taken as identifier  which is undeclared so it comes into semantic error in C, then  it gives error of missing operator(like = before gate) or  mising semicolon before gate which comes into syntactic error in C.

So there are 2 errors: 1 semantic and 1 syntactic. Answer should be (d).

But when you will correct these errors, you will get new error like undeclared identifier gate(which will be a semantic error).

0 votes
0 votes

YES IT IS SYNTAX ERROR BECAUSE

FORM /* to last */ taken as comments 

so lexems are in,t, gate,; etc

so it is syntactically incorrect but lexically correct

Answer:

Related questions