GATE CSE
First time here? Checkout the FAQ!
x
+2 votes
299 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 (46.9k points)   | 299 views
please categorize the tokens.

2 Answers

+9 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 (47.2k points)  
edited by
Why remaining three semicolons are not counted?
Why remaining three semicolons are not get counted?

Question says number of distinct tokens and not just (count the total number of tokens) .

main is a function type na why did you take it a identifier "main(" is considered as fuction i think
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 (53.7k 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 May 2017
  1. akash.dinkar12

    3154 Points

  2. pawan kumarln

    1636 Points

  3. sh!va

    1590 Points

  4. Arjun

    1350 Points

  5. Bikram

    1298 Points

  6. Devshree Dubey

    1246 Points

  7. Angkit

    1044 Points

  8. Debashish Deka

    1042 Points

  9. LeenSharma

    880 Points

  10. srestha

    706 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 May 22 - 28
  1. Bikram

    384 Points

  2. pawan kumarln

    262 Points

  3. Ahwan

    236 Points

  4. Arnab Bhadra

    136 Points

  5. LeenSharma

    118 Points


22,770 questions
29,090 answers
65,119 comments
27,635 users