Name: ____________________________________________________ Alpha: _____________________
Describe help received: _________________________________________________________________
To submit written work: take a clear picture of your work with your phone, and email it to me at wcbrown@usna.edu with the title: si413hw 08. The space between "si413hw" and "08" is really important!
Theorem: Assuming we are operating under the principle of "maximal munch" ... If the underlying automaton has no loops that consist solely of non-accepting states, then there is some constant K such that no program requires more than K putback.
> 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[;]
binary2int defined below:
// 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 as: submit-external -c=SI413 -p=hw08 hwtok.cpp