The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+18 votes
6.4k views

The number of tokens in the following C statement is

printf("i=%d, &i=%x", i, &i);
  1. $3$
  2. $26$
  3. $10$
  4. $21$
asked in Compiler Design by Veteran (59.6k points)
edited by | 6.4k views
0
Is white space considered as a token ? and if yes doesn't the above programs have 2 white spaces :

1) after string constant

2) after variable i

?
0
In C , whitespace is not considered as a token class...
0
0
WhiteSpaces will be removed by the lexical analyser from the source code, this is one of the jobs of LA.

6 Answers

+37 votes
Best answer

answer - C

Tokens are:

  1. printf 
  2. (
  3. "i=%d, &i=%x"
  4. ,
  5. i
  6. ,
  7. &
  8. i
  9. )
  10. ;
answered by Loyal (9.1k points)
edited by
0

@ankit in case of distinct token count the two i identifiers are considered same or different?

0
What would be the answer if they would have asked to find the number of lexemes?
0
I think they will consider same
0
here the identifier 'i' will be counted as many times as it appears in the source code, but will be given only one token number in the symbol table?
please correct me
+9 votes

printf("i=%d,&i=%x",i&i);  it has 9 token but option are not matching 

the same question came in gate 2001  
printf("i=%d,&i=%x",i,&i); with this statement it has 10 token

1.printf 
2.(
3."i=%d,&i=%x"
4.,
5.i
6.,
7.&
8.i
9.)
10.;

answered by Boss (16k points)
0
Is the string inside the print statement considered as a single token (token no. 3)...?
0
yes!
0
Thanks for the reply...

Actually my doubt is about the quotes (" " ) shouldn't we count that also...?
+11

C tokens are of six types. They are,

  1. Keywords               (int, while,etc),
  2. Identifiers               ( main, total,etc),
  3. Constants              ( 10, 20),
  4. Strings                    ( “total”, “hello”),
  5. Special symbols  ( (,), {,} etc),
  6. Operators              ( +, /,-,*,etc)
+4 votes

1.printf
2.(
3."i=%d, &i=%x"
4.,
5.i
6.,
7.&
8.i
9.)
10.;
option C

 

answered by Boss (16k points)
0
Why is 3."i=%d, &i=%x" taken as one token ? I thought " , i, = , %d , & , i , = ,%x were all separate tokens Why is , and i duplicated twice in the count ?
+4

Types of token in c

  1. Keywords               (eg: int, while),
  2. Identifiers               (eg: main, total),
  3. Constants              (eg: 10, 20),
  4. Strings                    (eg: “total”, “hello”) 
  5. Special symbols  (eg: (, {),
  6. Operators              (eg: +, /,-,*)
    when compiler meets " it generate one token for whole string as it means nothing specifically in C just a string.
    i is not duplicated i means value of i and &i means address of i.
+1
yes, the format specifiers are handled inside "printf". Not a C token.
0
What is the token category of & here? Is it special symbol?
+1
it is an operator- returns the address of operand.
0
Okey
0
Arjun Sir, In the complete reference it has discussed three types of reference-VARIABLE,NUMBER and DELIMITER.Is it correct or the above types of tokens is right?
+1 vote
if it is  i++   in place of &i   then answer will be 10 ??
answered by Loyal (5.3k points)
+3
yes bcoz both(i++ and &i ) have 2 tokens.
+1 vote
Number of tokens(i.e valid lexemes that matches with their pattern ) are

 1)printf   

2). (

3)"i=%d, &i=%x"

4.) ,

5) i

6) ,

7) &

8) i

9) )

10) ;

Hence ,total no of tokens =10  ,Answer C is correct.
answered by (233 points)
0 votes
printf("i=%d, &i=%x", i, &i);

has 10 tokens

 
answered by Boss (13.1k points)
edited by
0
it is not appearing as it should, why??
Answer:

Related questions



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

42,518 questions
48,528 answers
155,037 comments
63,382 users