Redirected
in Compiler Design retagged by
2,548 views
4 votes
4 votes

in Compiler Design retagged by
2.5k views

2 Comments

comment is not considered as token./*......*/
0
0
17 ??
1
1

2 Answers

7 votes
7 votes
Best answer
main()
{
    int x, y;
    fl/*gate oat z;
    x =/*exam*/10;
    y = 20;
}

Total number of tokens = 17.

These are the tokens:

  1. main
  2. (
  3. )
  4. {
  5. int
  6. x
  7. ,
  8. y
  9. ;
  10. fl
  11. 10
  12. ;
  13. y
  14. =
  15. 20
  16. ;
  17. }

The comment starts with a /* and ends to the nearest */. Therefore

/*gate oat z;
x =/*exam*/

will be considered a single token.

And "fl" and "10" will be separated as two different tokens.

selected by

2 Comments

if

/*gate oat z;
x =/*exam*/

is considered as single token then why it is not counted to make it 18

and why fl is a token and not an error

0
0
1. fl is not considered as an error in lexical phase(as given in the question), it is treated just like a variable name, and the lexical analyzer generates an id for it and passes to the syntax analyzer. The error will be detected in that phase.

2. All the comments are detected by the lex in the same way other tokens are detected, i.e. using finite automata. The whitespace class of token includes whitespaces, tabs, newlines and comments. These all are grouped in a single token class, but these tokes are not passed to the syntax phase. This is well explained in Alex Aiken's Compiler videos.
1
1
4 votes
4 votes

main

(

)

{

int

x

,

y

;

fl

10

;

y

=

20

;

}

Therefore, total of 17 tokens.

Related questions