in Compiler Design reopened by
5,286 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.3k views

31 Comments

i think it should be semantic error...lexical analyser and syntax analyser will parse it easily;;
0
0
i think it should be semantic error...lexical analyser and syntax analyser will parse it easily;;
0
0
none. since comment starts with /* and ends with */

it will be int gate;
1
1
But it shows error on IDE
0
0
prog.c: In function 'main':
The error on IDE-
prog.c:5:5: error: unknown type name 'in'
     in/*comment x; 
     ^
prog.c:6:22: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'gate'
     floa/*comment*/t gate;
0
0
lexical error for in/*
1
1
How so?
0
0
It will be a syntax error.
1
1
@Shubhanshu

what about in
1
1
but in general, lexical analyzer will remove the comments and blank spaces right? so the comment should be vanished when the code reaches syntax analyzer.
1
1
Oops mistyped it is a semantic error, not a syntax error.
1
1
Actually in class,key given by sir is C i.e. both.. but I am not able to get it
0
0

@Reshu $ingh what is the solution provided by them?

1
1

@Shubhanshu

When "in" is read and since comment in between so "in" and "t" two different leximes and "in" and "t" without comma-- So SYNTAX error

Now 'gate' variable has come but without data type being defined -- So SEMANTIC error

 

0
0
1st line not a comment line
1
1
b?
0
0
Yes Its B.
0
0

 in/*comment t x;

will give syntax error 

0
0

@Arjun please answer this question. 

0
0

/*comment t x; floa/*comment*/ this will be considered as 1 comment? 

@srestha

0
0

In lexical analysis any comment line just removed

So, 

/*comment t x;

      floa/*comment*/

will be removed totally.  lexical analyzer will take 

main()

{     int gate;

}

So, no error.

But syntax analyzer cannot recognize "in" keyword , will give error

0
0
It will be like this?

main()

{ in t gate;

}

so lexical analyser will not give any error. Error will come due to syntax analyser or semantic analyser? Because semantic analyser says the variable is undeclared.
0
0
please answer this question
0
0

@Satbir

if there is no compile time error(syntax error), then how there will be runtime error??

0
0

@Rhythm

main()

{ in t gate;

}
 

no, it removes blank spaces too.

0
0
"in" must be a semantic error, but why will it not a syntax error??

Syntax analyzer will get in/*....*/t  gate;

So no pattern for in and then t and then gate

right??
0
0
Yeah if it were in,t,gate then syntax analyser would've shown no error. Basically these three identifiers should be separated by operators or separators like comma but not semicolon. Am i right?
0
0
@Arjun Sir .. please answer.
0
0
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