First time here? Checkout the FAQ!
+4 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 (56.9k points) 36 189 499 | 491 views
please categorize the tokens.

2 Answers

+13 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 (50.4k points) 22 90 410
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 (65k points) 35 222 625
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?

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
Top Users Oct 2017
  1. Arjun

    23338 Points

  2. Bikram

    17048 Points

  3. Habibkhan

    7912 Points

  4. srestha

    6228 Points

  5. Debashish Deka

    5438 Points

  6. jothee

    4968 Points

  7. Sachin Mittal 1

    4772 Points

  8. joshi_nitish

    4286 Points

  9. sushmita

    3964 Points

  10. Rishi yadav

    3794 Points

Recent Badges

Popular Question makhdoom ghaya
Popular Question junaid ahmad
Notable Question learner_geek
Notable Question jothee
Popular Question jothee
Notable Question Jeffrey Jose
Notable Question air1ankit
Nice Question jothee
Verified Human shaleen25
Popular Question jothee
27,290 questions
35,142 answers
33,231 users