In this problem, we're going to modify the lexer to handle hexadecimal numbers.
So what it is hexadecimal number?
Well, more importantly, what is a decimal number?
Let's go over this very quickly.
If you have--let's say a number we write out--451--we consider this the ones place,
the tens place, and the hundreds place.
We do that because this is the 0th position, the 1th, and the 2nd,
and 10 to the 0, 10 to the 1, and 10 to the 2.
This is one, tens, hundreds.
We calculate the value of this by multiplying the position of the digit by the value of the place.
So this would yield 451.
In computer science, we often use base 2.
That's because the electronic circuits that we use in all of our fun little computers
are most easily designed with 2 states--on and off.
That's represented by a 1 or a 0.
So the numbers that we often use are binary.
Let's say we have this number--1101.
In this case, it's not the ones place, the tens place, the hundreds place,
and the thousands place.
We've abstracted this idea of a base so that we have 2 to the 0, 2 to the 1, 2 to the 2,
and 2 to the 3.
If we multiply all of that out like we did with the decimal number,
we get 13.
So I still haven't answered what a hexadecimal number is.
Hexadecimal is just base 16, and the reason we really like base 16--
it may seem odd to you at first--is that it's a very nice way of representing these base 2 numbers
and handling them in a more concise manner.
So 1, 1, 0, 1--I can write that with 1 hexadecimal number which happens to be d
and convention dictates that whenever we write a hexadecimal number,
we're going to put a 0 and an x in front of it.
0 and the x don't mean anything, except that it's a hexadecimal number that follows.
So that's a hexadecimal, and for short, I'll say hex.
So the goal of this problem is to modify the lexer to identify numbers written in hex
and store the value of the number as what it equals.
So we're going to identify the number using a regular expression,
and then we're going to calculate the value using a formula much like this.
So let's go straight to the IDE.
So here I have the given code for this problem.
Let's quickly run through it.
Right here we have importing the lexer.
Then we name the tokens that we want to have.
Num stands for the hexadecimal numbers that we're going to identify,
and ID is pretty much everything else, but the definition is described in the problem.
We have 2 types of NUMs.
One is a decimal number and the other is going to be the hexadecimal number,
which we have to write.
We ignore spaces, tabs, returns, etc., and we throw an error when there's something
we don't understand.
We have some code that runs our lexer for the sake of texting,
and then we have some test cases.
Let's fill in our code!
Because it's easier, let's do the ID first.
We have to come up with a regular expression.
The definition for the ID is pretty simple.
It's just a regular expression that matches sequences of characters with the letters a - z
regardless of their capitalization,
and then we just return it.
So now we want to do the definition for hexadecimal numbers.
The regular expression here is also not that bad.
Here we have our definition for a hexadecimal number.
We require that there's a 0 in x because that's what indicates that this is written in hex
and not decimal.
And then we have 1 or more numbers or letters a - f.
In this line, I'm going to strip off the 0 and the x because that doesn't really help me
calculate anything that has to do with the value of the number.
Now I'm going to go through each digit of our number.
For every digit that there is we're going to multiply our number by 16.
That gives us the power of 16 for every digit that we've done so far.
Here I want to check, if the digit we're at is between 0 and 9,
so we don't have to do anything special with the fact that it's a letter,
but we do have to do a bit of a conversion from the fact that it's a character
and not a number.
So one way to do this is take the numerical order value of our digit - data 0,
and that's going to yield a number between 0 and 9 because they're all together in that.
Also you could take--use INT--the INT function which will convert that string into an integer.
Either way works.
Here, I'm going to handle the case where the current digit is a letter a- f.
I'm going to find out how many places away from a it is,
and then I'm going to add 10, so a is going to equal 10.
b is going to be some number - the value of a, which is going to give 1,
so 1 + 10 + our number--we're going to add 11 to the number
because b represents 11 and so on.
I'm going to wrap up this function by setting the value of this token to the number
and setting the type to NUM and returning the token itself.
That's everything you have to do for this problem.