GATE CSE
First time here? Checkout the FAQ!
x
+1 vote
236 views

Consider the following C program:
 

int main (void)
{
    in/*this is an example*/z;
    
    double/*is it an error?*/y;
    
    print( “This is simple” );
    
    return 0;
}

- How many Different tokens are there in the above code.

asked in Compiler Design by Veteran (43.4k points)   | 236 views
please categorize the tokens.

2 Answers

+8 votes
Best answer
int main ( void )
{
    in z ;
    
    double y ;
    
    print( “This is simple” );
    
    return 0 ;
}

All Colored ones are different Tokens = 16

 

Types of tokens-  

keywords - int, void , double, return.

Identifiers - main , in, z, y, print.

Strings     - “This is simple”.

Constant  - 0.

Special- symbols / Delimiter / Punctuator /  - ( , ), ; , { , } , comma.

NOTE:

 ( 6 types of  c tokens )

answered by Veteran (44.9k points)  
edited by
0 votes
"int","main","(","void",")"-5 tokens

"{"-1 token

"inz",";"-2 tokens

"double","y",";"-3 tokens

"printf","(","String literal",")",";"-5 tokens

"return","0",";"- 3 tokens

"}"- 1 token

Total 20 tokens

int,void-keyword

"inz","y"-identifier

";"-delimeter
answered by Veteran (52.4k points)  
edited by
please mention the type of the tokens also--like delimiter,,identifier etc etc

@srestha why u counted inz as one single token??
after removing comments in will be one and z will be one token...

ok means it will give lexical error, rt?

no it will not given any lexical error...in will be treated as one token and z as another...lexical analyser follows longest match rule...it will absolutely work fine here..

@Srestha

It throws a syntax error.
but "in" which type of token?

tokens are alays be separated by white spaces. Is it contain white space?



Top Users Mar 2017
  1. rude

    4018 Points

  2. sh!va

    2994 Points

  3. Rahul Jain25

    2804 Points

  4. Kapil

    2606 Points

  5. Debashish Deka

    2092 Points

  6. 2018

    1414 Points

  7. Vignesh Sekar

    1318 Points

  8. Bikram

    1218 Points

  9. Akriti sood

    1166 Points

  10. Sanjay Sharma

    1004 Points

Monthly Topper: Rs. 500 gift card

21,439 questions
26,753 answers
60,919 comments
22,929 users