edited by
12,285 views
16 votes
16 votes
// comment
printf("string %d ",++i++&&&i***a);
return(x?y:z)

Q1]  Find Number of Tokens and Lexems in the above code snippet
Q2 ]  ***  -> counted as 1 token or 3 tokens ?  why it is so
Q3 ] ' printf ' is lexeme whose token is id , ' i ' is lexeme and token is id are they both countes as single token or separate token
Q4 ] If the Question would have been count the number of unique lexemes and tokens what would have been the answer ?

edited by

2 Answers

Best answer
29 votes
29 votes
// comment
printf("string %d ",++i++&&&i***a);
return(x?y:z)

I am applying whitespaces in the code to seperate tokens.

// comment
printf   (  "string %d "  ,  ++  i  ++  &&  &   i  *   *   *   a  )  ;
return   (  x  ?  y  :  z  )

Total number of tokens will be = 24 

Distinct number of tokens will be = 18 

How && and & are seperated ?

Compiler's Lexical Analysis phase uses greedy tokenization approach. It always matches with the biggest pattern available .

Like + is there, but ++ is also used in C. Hence, whenever i have ++, i count it as single token.

Now, i think its easy to count tokens in &&&. Like, & is used in C as Bitwise AND while && is used in C as Logical AND, so we use biggest possible pattern which is matched . Hence, &&& has 3 tokens like && and &. 

We have learnt * in C. Is there ** defined in C ? No, so *** is 3 tokens .

Difference between Lexeme and a token ?

When a source program is fed into the lexical analyzer, suppose 

return(x?y:z)

which yields the lexemes : return       (        x          ?            y      :       z      )

With corresponding tokens : <id , 0>   <(>   <id,1>   <?>   <id,3>   <:>   <id,z>    <)>

selected by
5 votes
5 votes
total 24 tokens. tokens are as follows: *,*,*  and &&,& are considered separately.

1. printf 
2. ( 
3. "string %d " 
4. ,
5. ++
6. i
7. ++
8.  &&
9.  &

10. i
11. *
12. *
13. *
14. a
15. )
16. ;
17. return
18. (
19. x
20. ?
21. y
22. :
23. z
24. )

Related questions

0 votes
0 votes
2 answers
2
shikharV asked Nov 19, 2015
1,862 views
The number of lexemes in the statement in FORTRANDO 10 I = 100is __________ .
0 votes
0 votes
0 answers
3
pC asked Jan 11, 2016
1,410 views
Find Number Of Lexeme And Tokens in the Below FORTRAN Code. [ ignore the white spaces only ]( pls also explain me what do the code mean )DO 10 I = 100DO 10 I=10,1DO 10 I ...
2 votes
2 votes
2 answers
4
Na462 asked Nov 7, 2018
3,600 views