in Compiler Design
5,241 views
1 vote
1 vote
Find no of tokens in below program.

#include<stdio.h>

main()

{

   int I;

   int *pi = &I; //parent pointer

   scanf("%d",pi);

   printf("%d\n", I+5);

}
in Compiler Design
by
5.2k views

11 Comments

L1:main()
L2:{
L3: int I;
L4: int *pi = &I; //parent pointer

L5 : scanf("%d",pi);
L6 : printf("%d\n", I+5);
L7: }

L1 : 3 ; L2 : 1 ; L3 : 3 ; L4 : 7; L5 : 7 ; L6 : 9 ; L7 : 1

So total number of tokens : 31
6
6
I am getting 31

What is answer ?
0
0
reshown by
what about header file?
0
0
Lexical analysis of header file is not done
0
0

@Sayan Bose

Lexical analysis of header file is not done

WHY? is there any standard reference?

0
0
0
0

@srestha mam

then how the printf and scanf or some other can resolve? ( i mean at what phase of compiler )

0
0
@Shaik

it is done before lexical analysis

In preprocessing phase (macro also done there)
1
1
i am getting total num of tokens as 32
0
0

References to printf and scanf are actually resolved by the linker by providing the object code for printf and scanf and other library functions.

In the preprocessing phase only function prototype are included in source code .

So the actual resolving done by linker .

That linking again of two types--

   Static linking 

   Dynamic linking

0
0
Total number of tokens are 31
0
0

1 Answer

1 vote
1 vote
statement # of tokens
#include<stdio.h> 0(zero)
main() 3
{ 1
   int I; 3
   int *pi = &I; //parent pointer 7
 scanf("%d",pi); 7
   printf("%d\n", I+5); 9
} 1
Total 31

1 comment

@sudharshan We can't say that 

#include<stdio.h>	

will have 0 tokens because in the preprocessor stage this header would be expanded to its actual content.

And the number of tokens would be huge !!!

0
0