Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating tokens in c statement

Tags:

c

token

The number of tokens in the following C statement.

printf("i = %d, &i = %x", i, &i);

I think there are 12 tokens here. But my answer is wrong.

Can anybody tell me how to find the tokens in the above C statement?

PS: I know that a token is source-program text that the compiler does not break down into component elements.

like image 760
Suraj Menon Avatar asked Oct 13 '12 13:10

Suraj Menon


People also ask

How are tokens calculated?

To count tokens, one can make use of NLTK's FreqDist class from the probability package. The N() method can then be used to count how many tokens a text or corpus contains.

How many tokens are there in C statement?

There are 6 types of C tokens : identifiers, keywords, constants, operators, string literals and other separators.

How do you count tokens in a program?

Count number of tokens : int main() { int a = 10, b = 20; printf("sum is :%d",a+b); return 0; } Answer: Total number of token: 27.

What is token in C with example?

One can define tokens in C as the smallest individual elements in a program that is meaningful to the functioning of a compiler. A token is the smallest unit used in a C program. Each and every punctuation and word that you come across in a C program is token.


2 Answers

As far as I understand C code parsing, the tokens are (10 in total):

printf
(
"i = %d, &i = %x"
,
i
,
&
i
)
;

I don't count white space, it's generally meaningless and only serves as a separator between other tokens, and I don't break down the string literal into pieces, because it's an integral entity of its own.

like image 62
Alexey Frunze Avatar answered Nov 05 '22 03:11

Alexey Frunze


This looks very much like a school assignment or something, but depending on whether or not whitespace counts: 10 or 12 (or 13, if whitespace counts and there is an ending newline)

'printf' '(' '"i = %d, &i = %x"' ',' 'i' ',' '&' 'i' ')' ';'
  1       2     3                4   5   6   7   8   9  10
like image 21
perh Avatar answered Nov 05 '22 03:11

perh