Max Osad
Max Osad

Reputation: 27

Patter for byte when I got pattern for numbers in lexer

I have pattern for numbers in lexer

$digit=0-9
   $digit+                       { \s -> TNum  (readRational s) }

I want to add another pattern for bytes. Bytes contain 2 symbols from diaposon 0 - f. Which pattern I should write for byte? Will it distinguish 11 :: byte and 11 :: num correctly?

Upvotes: 0

Views: 69

Answers (1)

Noughtmare
Noughtmare

Reputation: 10695

If you add two overlapping patterns in your lexer then it will either fail or simply select one of them (I don't know the exact behavior of alex), so that is probably not what you want.

If I understand it correctly there are actually two issues at play here:

  1. decimal and hexadecimal numbers overlap
  2. fixed-size numeric types overlap, e.g. 11 could be 8 bits or 32 bits.

The common approach to solve problem 1 is to add a prefix to hexadecimal numbers. C uses the prefix 0x for that. So then 17 would be written as 0x11 in hexadecimal.

I think you should not try to solve problem 2 in the lexer, just keep the numbers as a general number type. Later during type checking you can determine the exact type that is required by the context or default to a type if there are no other constraints.

Upvotes: 0

Related Questions