First time here? Checkout the FAQ!
+3 votes

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 (50.9k points)   | 400 views
please categorize the tokens.

2 Answers

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


 ( 6 types of  c tokens )

answered by Veteran (47.9k 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



answered by Veteran (56.5k 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 will be treated as one token and z as another...lexical analyser follows longest match will absolutely work fine here..


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

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

Related questions

Top Users Aug 2017

    4670 Points

  2. Bikram

    4556 Points

  3. akash.dinkar12

    3420 Points

  4. rahul sharma 5

    3124 Points

  5. manu00x

    2864 Points

  6. makhdoom ghaya

    2450 Points

  7. just_bhavana

    2136 Points

  8. Tesla!

    2042 Points

  9. stblue

    1930 Points

  10. joshi_nitish

    1686 Points

24,970 questions
32,072 answers
30,150 users