Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Theory of Computing Compile it and run it with fain.txt that has: abbb a bb abb

ID: 3590374 • Letter: T

Question

Theory of Computing

Compile it and run it with fain.txt that has:

    abbb  

    a

    bb        

    abb    to see what it does.

This program accepts     a b^+

Change this program with new FA functions (follow my ** comments) to accept the following:

function ident_token   l ( l | d | _ )^*

function real_token    d^* . d^+   

function int_token     d^+               

Note: l can be either a or b

           d can be either 2 or 3

You should not handle other digits and characters.

Main calls Scanner repeatedly.

Scanner grabs another word from the input file.

Scanner calls ident_token, real_token and int_token in this order until one of them returns TRUE.

Scanner gives back the token type and the word to Main.

Main has to display the word and the token type.

Must test the program again with the input file fain.txt.  

ab_2a     - ident

.23       - real

23.3      - real

23        - int

ab&c      - bad

23.6      - bad

2a3       - bad

22..2     - bad

23.       – bad


________________________

fa.cpp

#include<iostream>

#include<fstream>

#include<string>

using namespace std;

// File fa.cpp for HW3

// ** Change this to fit the HW3C specification - look for **

// ** Must have the same types of tracing couts as mytoken.

// MYTOKEN DFA

// This FA is for a b^+

bool mytoken(string s)

{

int state = 0;

int charpos = 0;

cout << "Trying the mytoken machine..." << endl;

while (s[charpos] != '')

{

cout << "current state: " << state << endl;

cout << "character: " << s[charpos] << endl;

if (state == 0 && s[charpos] == 'a')

state = 1;

else

if (state == 1 && s[charpos] == 'b')

state = 2;

else

if (state == 2 && s[charpos] == 'b')

state = 2;

else

   {

      cout << "I am stuck in state " << state << endl;

      return(false);

   }

charpos++;

}//end of while

// where did I end up????

if (state == 2) return(true); // end in a final state

   else return(false);

}

// IDENT DFA

// This FA is for **

bool ident_token(string s)

{

// ** complete this based on mytoken

}

// REAL DFA

// This FA is for **

bool real_token(string s)

{

// ** complete this based on mytoken

}

//INT DFA

// This FA is for **

bool integer_token(string s)

{

// ** complete this based on mytoken

}

enum tokentype {ERROR, MYTOKEN, IDENT, REAL, INT};

int scanner(tokentype& , string& ); // to be called byy main

ifstream fin ("fain.txt", ios::in); // input file stream

// The test-driver: to be completed **

int main()

{

string tokens[5] = {"ERROR", "MYTOKEN", "IDENT", "REAL", "INT"};

tokentype thetype;

string theword;

while (true) // keep on going

{

   scanner(thetype, theword); // the paramers will be set by Scanner

   if (theword == "EOF") break;

   cout << "Type is:" << tokens[thetype] << endl;

}

fin.close();

}

int scanner(tokentype& the_type, string& w)

{

  

// This goes through all machines one by one on the input string

cout << ".....Scanner was called..." << endl;

fin >> w; // grab next word from fain.txt

cout << "Word is:" << w << endl;

if (mytoken(w))

{ the_type = MYTOKEN; }

  

// ** add other if-then's here in the right order to go through

// all FAs one by one and set the_type to be IDENT, REAL or INT.

else //none of the FAs returned TRUE

   { cout << "Lexical Error: The string is not in my language" << endl;

the_type = ERROR; }

}//the very end of scanner


________________________

fain.txt

abbb

a

bb  

abb

ab_2a

.23

23.3

23

ab&c

23.6

2a3

22..2

23.


Word from fain.txt Main calls Scanner calls FA-functions

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

// File fa.cpp for HW3
// ** Change this to fit the HW3C specification - look for **
// ** Must have the same types of tracing couts as mytoken.
// MYTOKEN DFA
// This FA is for a b^+
bool mytoken(string s)
{
int state = 0;
int charpos = 0;

cout << "Trying the mytoken machine..." << endl;  

while (s[charpos] != '')
{
cout << "current state: " << state << endl;
cout << "character: " << s[charpos] << endl;

if (state == 0 && s[charpos] == 'a')
state = 1;
else
if (state == 1 && s[charpos] == 'b')
state = 2;
else
if (state == 2 && s[charpos] == 'b')
state = 2;
else
{
cout << "I am stuck in state " << state << endl;
return(false);
}
charpos++;
}//end of while

// where did I end up????
if (state == 2) return(true); // end in a final state
else return(false);
}


// IDENT DFA
// This FA is for **
bool ident_token(string s)
{
int state = 0;
int charpos = 0;

cout << "Trying the ident_token machine..." << endl;  

while (s[charpos] != '')
{
cout << "current state: " << state << endl;
cout << "character: " << s[charpos] << endl;

if (state == 0 &&( s[charpos] == 'a' || s[charpos] == 'b'))
state = 1;
else
if (state == 1 &&( s[charpos] == 'a' || s[charpos] == 'b' || s[charpos] == '2' || s[charpos] == '3'))
state = 1;
else
if (state == 1 && s[charpos] == '_')
state = 2;
else
if (state == 2 &&( s[charpos] == 'a' || s[charpos] == 'b' || s[charpos] == '2' || s[charpos] == '3' ) )
state = 3;
else
if (state == 3 &&( s[charpos] == 'a' || s[charpos] == 'b' || s[charpos] == '2' || s[charpos] == '3' ) )
state = 3;
else
{
cout << "I am stuck in state " << state << endl;
return(false);
}
charpos++;
}//end of while

// where did I end up????
if (state == 3) return(true); // end in a final state
else return(false);

}


// REAL DFA
// This FA is for **
bool real_token(string s)
{
int state = 0;
int charpos = 0;

cout << "Trying the real_token machine..." << endl;  

while (s[charpos] != '')
{
cout << "current state: " << state << endl;
cout << "character: " << s[charpos] << endl;

if (state == 0 && (s[charpos] == '2' || s[charpos] == '3'))
state = 0;
else
if (state == 0 && s[charpos] == '.')
state = 1;
else
if (state == 1 && (s[charpos] == '2' || s[charpos] == '3'))
state = 2;
else
if (state == 2 && (s[charpos] == '2' || s[charpos] == '3'))
state = 2;
else
{
cout << "I am stuck in state " << state << endl;
return(false);
}
charpos++;
}//end of while

// where did I end up????
if (state == 2) return(true); // end in a final state
else return(false);

}


//INT DFA
// This FA is for **
bool integer_token(string s)
{
int state = 0;
int charpos = 0;

cout << "Trying the integer_token machine..." << endl;  

while (s[charpos] != '')
{
cout << "current state: " << state << endl;
cout << "character: " << s[charpos] << endl;

if (state == 0 && (s[charpos] == '2' || s[charpos] == '3'))
state = 1;
else
if (state == 1 && (s[charpos] == '2' || s[charpos] == '3'))
state = 1;
else
{
cout << "I am stuck in state " << state << endl;
return(false);
}
charpos++;
}//end of while

// where did I end up????
if (state == 1) return(true); // end in a final state
else return(false);

}

enum tokentype {ERROR, MYTOKEN, IDENT, REAL, INT};
int scanner(tokentype& , string& ); // to be called byy main
ifstream fin ("fain.txt", ios::in); // input file stream


// The test-driver: to be completed **
int main()
{
string tokens[5] = {"ERROR", "MYTOKEN", "IDENT", "REAL", "INT"};
tokentype thetype;
string theword;

while (!fin.eof()) // keep on going
{
fin >> theword; // grab next word from fain.txt
cout << "Word is:" << theword << endl;
scanner(thetype, theword); // the paramers will be set by Scanner
if (theword == "EOF") break;

cout << "Type is:" << tokens[thetype] << endl;
}

fin.close();
}

int scanner(tokentype& the_type, string& w)
{
  
// This goes through all machines one by one on the input string

cout << ".....Scanner was called..." << endl;

//fin >> w; // grab next word from fain.txt
//cout << "Word is:" << w << endl;

if (mytoken(w))
{ the_type = MYTOKEN; }
else
if (ident_token(w))
{ the_type = IDENT; }
else
if (real_token(w))
{ the_type = REAL; }
else
if (integer_token(w))
{ the_type = INT; }
else //none of the FAs returned TRUE
{ cout << "Lexical Error: The string is not in my language" << endl;
the_type = ERROR; }

}//the very end of scanner