GATE CSE
First time here? Checkout the FAQ!
x
+2 votes
333 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 (48.4k points)   | 333 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.4k 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 (54.9k 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 Jun 2017
  1. Bikram

    3704 Points

  2. Hemant Parihar

    1502 Points

  3. junaid ahmad

    1432 Points

  4. Arnab Bhadra

    1416 Points

  5. Niraj Singh 2

    1391 Points

  6. Debashish Deka

    1246 Points

  7. Rupendra Choudhary

    1194 Points

  8. rahul sharma 5

    1158 Points

  9. Arjun

    956 Points

  10. srestha

    950 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 Jun 19 - 25
  1. Bikram

    1960 Points

  2. Niraj Singh 2

    1386 Points

  3. junaid ahmad

    502 Points

  4. Debashish Deka

    414 Points

  5. sudsho

    410 Points


23,373 questions
30,079 answers
67,406 comments
28,396 users