Redirected
edited by
5,435 views
4 votes
4 votes

There are 40 tokens in this question.

But I have doubt that what should be the wording of the question 

"The number of LEXEMES generated by the Lexical Analyzer in the above program is _______ "

OR

"The number of TOKENS generated by the Lexical Analyzer in the above program is _______ "

In most of the question, it is asked about calculating numbers of tokens. Is the answer remains same if they asked about the number of lexemes instead of tokens?

I am confused between these two terms, and I know there are 6 different types of tokens we have namely:-

Keyword, Operators, Identifiers, Constant, Punctuation Symbol and String Constants.

How token and lexeme are different from each other?

edited by

4 Answers

31 votes
31 votes

It should be 40..Key things to note here :

a) Lexical analyser is a DFA .So it has capability to identify compound operators ; so they will be considered as 1 lexeme only under the token class "operator"..

b) Numbers are treated as  constants irrespective of the base used whether it is octal or decimal..

c) compiler follows greedy approach and will try to make longest pattern available. like x++ will be 2 token instead of x , + ,+ (3)

So here "01" , "!=" and ">>=" will be treated as 1 token only..

Hence the correct answer should be 40..

Ref : https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Compound_assignment_operators

edited by
0 votes
0 votes
unsigned int counted once as Compiler generates one token for that, 01 counted once as it is constant. As >> and =, they are different operators so counted twice.
0 votes
0 votes
List of tokens final answer is 40

1. int

2. foo

3. (

4. unsigned

5. int

6. n

7. )

8.{

9. int

10. c

11. ,

12. x

13. =

14. 0

15. ;

16 while

17 (

18 n

19 != (Greedy method mentioned by Habib Sir compiler combines operators and considers it as one token)

20 0

21 )

22 {

23 if

24  (

25 n

26 &

27 01 Same greedy approach

28 )

29 x

30 ++

31 ;

32 n

33 >>= Greedy Approach

34 1

35 ;

36 }

37 return

38 c

39 ;

40 }

Hope this rests all the confusion

Related questions

6 votes
6 votes
1 answer
1
Tech Geek asked Apr 3, 2018
19,784 views
What is the difference between lexeme and tokens?
0 votes
0 votes
0 answers
2
pC asked Jan 11, 2016
1,443 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
3
Na462 asked Nov 7, 2018
3,678 views
0 votes
0 votes
2 answers
4
saumya mishra asked Jun 12, 2018
2,267 views
In this question we will take n-=n1 as n=n-n1 as 5 tokens or n-=n1 as 4 tokens ????