Name: ____________________________________________________ Alpha: _____________________
Describe help received: _________________________________________________________________
> g++ -o hwtok hwtok.cpp > ./tok 3*(-4 + 12/3); NUM[3] OPM[*] LP[(] OPA[-] NUM[4] OPA[+] NUM[12] OPM[/] NUM[3] RP[)] STOP[;]Some changes to a language can be handled completely by the scanner. Your job is to modify hwtok.cpp so it can accept integers input in either decimal or binary. Specifically, a string of 0's and 1's followed immediately by a "b", no whitespace or anything, is an integer literal in binary. So, for example:
> ./hwtok 10 + 3; NUM[10] OPA[+] NUM[3] STOP[;] 101b + 3; NUM[5] OPA[+] NUM[3] STOP[;]
1030b + ..., i.e. where the "b" is
there, but the digits in front of it are not all 0's and
1's. Correct behavior would be to tokenize this as
NUM[1030] Bad char b ... . I'm not asking
you for that (though it would be cool!)
// Input: val - a string consisting of the form (0|1)+b , e.g. 1011b
// Output: the integer value equal to val (note: no error checking is done!)
int binary2int(const string &val)
{
int x = 0;
for(int i = 0; val[i] != 'b'; ++i)
x = 2*x + (val[i]-'0');
return x;
}
Submit your code with: submit -c=SI413 -p=hw08 hwtok.cpp