680 views

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

edited | 680 views
+1
none. since comment starts with /* and ends with */

it will be int gate;
0
But it shows error on IDE
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;
+1
lexical error for in/*
0
How so?
+1
It will be a syntax error.
+1
@Shubhanshu

+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
Oops mistyped it is a semantic error, not a syntax error.
0
Actually in class,key given by sir is C i.e. both.. but I am not able to get it
+1

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

0

@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

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

in/*comment t x;

will give syntax error

0

0

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

@srestha

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

@Satbir

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

0

@Rhythm

main()

{ in t gate;

}

no, it removes blank spaces too.

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

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

by Veteran (101k points)
selected
0
1st line command line not completed
0
@srestha plz tell me no. of tokens here....
0
@srestha , no issue in that..Had there been no trailing comment symbol then there would have been problem..But here it is not an issue..
+1
^ i think there ll b no such issue bcoz of multiline comment
0
I verified it in compiler also..
+1

@ Habib

I am confused between syntax error and semantic error.

Lexical analyser removes the comment in first line and generates token for in, t, x so no issue for lexical error but id id id (identifier followed by identifiers) is not valid in C so why not error comes in syntax analysis phase.

Then after what you have said ' in' is undeclared comes into the picture.

I think syntax error will leads to this semantic error.

Option B or None of these?

Correct me if i am wrong..!!

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

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

here is only semantic error as comment between the words result in space after the compilation so there will be syntatical error i.e. error in meaning of the particular identifier is not define
by Active (3.9k points)
edited