reopened by
5,181 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
reopened by

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

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

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;

1 votes
1 votes

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

6 votes
6 votes
1 answer
1
junaid ahmad asked Jan 12, 2018
1,234 views
Is this line a/*b successfully generating the token's or it give lexical error ? if it is given the lexical error then why so ?I think it will give lexical error because...
7 votes
7 votes
4 answers
2
itsvkp1 asked Nov 1, 2017
3,204 views
if there is miss spelling in some keyword in a program then this misspelled keyword will be treated as lexical errors or it will be treated as a new identifier and accept...
2 votes
2 votes
1 answer
3
Manu Thakur asked Sep 26, 2017
1,537 views
This screenshot is token from the book Ullman,How can following be a lexical error? because "elipseSize" should have a token recorded as an identifier.
1 votes
1 votes
2 answers
4