tags:

views:

91

answers:

2

In the kaleidoscope parser / AST example at LLVM, the enum is given all negative values. Why the minus sign ?

enum Token {
  tok_eof = -1,
  // commands
  tok_def = -2, tok_extern = -3,
  // primary
  tok_identifier = -4, tok_number = -5
};

+1  A: 

I believe the usage of these negative values was just a way to denote special tokens in the code.

In the example code, valid tokens are from 0 to 255 so any value out of this range can be used for special tokens like tok_eof. Therefore since 0 to 255 cannot be used for the enum, they've then chosen to use negative values although they could have used, 256, 257, 258 etc. Negative values seem more inituitive than 256, 257, 258 IMO.

Mr Roys
+2  A: 

A common C idiom with enums is to use negative values to mean one set of conditions and positive values to mean another set. For example, error conditions from the parser might be all positive values, while normal conditions have all negative values, and maybe zero is the "undefined" case. So in your code testing for any error is as simple as tok >= 0.

John