Question: //In C++ Finish the Program #include #include #include #include using namespace std; //-------------------------------------- // CS421 File td.cpp for HW2B Table-Driven Scanner // Your name: **
//In C++ Finish the Program
#include
#include
#include
#include
using namespace std;
//--------------------------------------
// CS421 File td.cpp for HW2B Table-Driven Scanner
// Your name: **
//--------------------------------------
// Complete this to fit the HW2B specification - look for **
// Must have the same types of tracing couts as my demo program.
// info on each DFA
struct info
{
string name; // token name
int startstate;
int finalstate;
};
info DFAs[4]; // store up to 4 dfas' start and final
int TRS[10][4]; // store all trs's - states 0-9 and chars a b c d -- all dfas transitions are in here
// ----- utility functions -----------------------
int readTables()
{
ifstream fin ("trs.txt", ios::in);
ifstream fin2 ("dfas.txt", ios::in);
// ** Read in the files into TRS and DFAs
// ** Return how many DFAs were read
}
void displayTables(int numDFAs)
{
// ** display DFAs nicely labeled
// ** display TRS nicely labeled
}
bool accept(info dfa, string word)
{
// ** Does the dfa accept the word?
// Start with the start state of the DFA and
// look up the next state in TRS for each char in word.
// At the end of the word, make sure you are in the
// final state of the DFA.
// Use a formula to convert chars to TRS col numbers.
}
int main()
{
cout << "This is a table driven scanner. Needs trs.txt and dfas.txt." << endl;
cout << "States are 0 to 9 and chars are a to d" << endl;
int numDFA = readTables(); // how many DFAs were read
displayTables(numDFA); // DISPLAY TABLES
cout << ".....done reading tables...." << endl;
string word;
while(true)
{ cout << "@@Enter a string: " ;
cin >> word;
// ** try the DFAs one by one and see
// if the word is accepted
// if so, display the word and the token name
// ** if no DFA does, generate a lexical error message.
cout << "do control-C to quit" << endl;
}
}//the end
TEST FILE: dfas.txt
token1 0 1
token2 2 3
TEST FILE: trs.txt
0 1 -1 -1
-1 -1 -1 -1
-1 -1 2 3
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
Strings that are Entered: ab
abb
aaab
cd
cdd
cccd
DESIRED OUTPUT:
This is a table driven scanner. Needs trs.txt and dfas.txt.
States are 0 to 9 and chars are a to d
a b c d
State 0: 0 1
State 1:
State 2: 2 3
State 3:
State 4:
State 5:
State 6:
State 7:
State 8:
State 9:
token1: 0 is start and ends in 1
token2: 2 is start and ends in 3
.....done reading tables....
@@Enter a string: ab
Trying dfa 0--------
state: 0 char: a
new state: 0
state: 0 char: b
new state: 1
Found token token1
do control-C to quit
@@Enter a string: abb
Trying dfa 0--------
state: 0 char: a
new state: 0
state: 0 char: b
new state: 1
state: 1 char: b
new state: -1
Trying dfa 1--------
state: 2 char: a
new state: -1
Lexical error!
do control-C to quit
@@Enter a string: aaab
Trying dfa 0--------
state: 0 char: a
new state: 0
state: 0 char: a
new state: 0
state: 0 char: a
new state: 0
state: 0 char: b
new state: 1
Found token token1
do control-C to quit
@@Enter a string: cd
Trying dfa 0--------
state: 0 char: c
new state: -1
Trying dfa 1--------
state: 2 char: c
new state: 2
state: 2 char: d
new state: 3
Found token token2
do control-C to quit
@@Enter a string: cdd
Trying dfa 0--------
state: 0 char: c
new state: -1
Trying dfa 1--------
state: 2 char: c
new state: 2
state: 2 char: d
new state: 3
state: 3 char: d
new state: -1
Lexical error!
do control-C to quit
@@Enter a string: cccd
Trying dfa 0--------
state: 0 char: c
new state: -1
Trying dfa 1--------
state: 2 char: c
new state: 2
state: 2 char: c
new state: 2
state: 2 char: c
new state: 2
state: 2 char: d
new state: 3
Found token token2
do control-C to quit
@@Enter a string: ^C
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
