Question
Problem Change this program ( fa.cpp ) with new DFA functions (follow my ** comments) to accept the following tokens: [2pts per DFA] Your functions
Problem
Change this program (fa.cpp) with new DFA functions (follow my ** comments)
to accept the following tokens: [2pts per DFA] Your functions must match your DFAs.
function ident_token l ( l | d | _ )^* Give the DFA here with state numbers **
function real_token d^* . d^+ Give the DFA here with state numbers **
function int_token d^+ Give the DFA here with state numbers **
Code:
include
#include
#include
using namespace std;
//------------------------------------------------
// CS421 File fa.cpp for HW2B DFA->Scanner Function
// Your name: **
//------------------------------------------------
// ** Change this to fit the HW2B specification - look for **
// ** Must have the same types of tracing couts as mytoken.
// ** Must complete the ** comments with RE
// ---------- DFAs follow -------------------------
// MYTOKEN DFA done by Rika Sensei has a sample
// 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] != '\0')
{
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);
}// end of mytoken
// IDENT DFA
// This FA is for RE: **
bool ident_token(string s)
{
// ** complete this based on mytoken
}//end of ident
// REAL DFA
// This FA is for RE: **
bool real_token(string s)
{
// ** complete this based on mytoken
}//end of real
//INT DFA
// This FA is for RE: **
bool integer_token(string s)
{
// ** complete this based on mytoken
}// end of int
// -----------------------------------------------------
enum tokentype {ERROR, MYTOKEN, IDENT, REAL, INT};
int scanner(tokentype& , string& ); // to be called by main
fstream fin; // file stream to use
// The test-driver -- NO NEED TO CHANGE THIS
int main()
{ string fname;
cout << "Enter the input file name:";
cin >> fname;
fin.open( fname.c_str(), fstream::in);
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;
}
cout << ">>>>>End of File encountered" << endl;
fin.close();
}// end of main
// Scanner sets the_type and w - TO BE COMPLETED **
int scanner(tokentype& the_type, string& w)
{
// This goes through all machines one by one on the input string w
cout << endl;
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_mytoken.txt
abbb
ab
aabb
bb
EOF
Desired Output:
Enter the input file name:fain_mytoken.txt
hap
.....Scanner was called...
Word is:abbb
Trying the mytoken machine...
current state: 0
character: a
current state: 1
character: b
current state: 2
character: b
current state: 2
character: b
Type is:MYTOKEN
.....Scanner was called...
Word is:ab
Trying the mytoken machine...
current state: 0
character: a
current state: 1
character: b
Type is:MYTOKEN
.....Scanner was called...
Word is:aabb
Trying the mytoken machine...
current state: 0
character: a
current state: 1
character: a
I am stuck in state 1
Lexical Error: The string is not in my language
Type is:ERROR
.....Scanner was called...
Word is:bb
Trying the mytoken machine...
current state: 0
character: b
I am stuck in state 0
Lexical Error: The string is not in my language
Type is:ERROR
.....Scanner was called...
Word is:EOF
Trying the mytoken machine...
current state: 0
character: E
I am stuck in state 0
Lexical Error: The string is not in my language
Using File fain.txt
ab_2a
a_b_2_a
.23
23.3
23
ab&c
23.6
2a3
22..2
23.
EOF
Desired OutPut:
Enter the input file name:fain.txt
hap
.....Scanner was called...
Word is:ab_2a
Trying the mytoken machine...
current state: 0
character: a
current state: 1
character: b
current state: 2
character: _
I am stuck in state 2
Lexical Error: The string is not in my language
Type is:ERROR
.....Scanner was called...
Word is:a_b_2_a
Trying the mytoken machine...
current state: 0
character: a
current state: 1
character: _
I am stuck in state 1
Lexical Error: The string is not in my language
Type is:ERROR
.....Scanner was called...
Word is:.23
Trying the mytoken machine...
current state: 0
character: .
I am stuck in state 0
Lexical Error: The string is not in my language
Type is:ERROR
.....Scanner was called...
Word is:23.3
Trying the mytoken machine...
current state: 0
character: 2
I am stuck in state 0
Lexical Error: The string is not in my language
Type is:ERROR
.....Scanner was called...
Word is:23
Trying the mytoken machine...
current state: 0
character: 2
I am stuck in state 0
Lexical Error: The string is not in my language
Type is:ERROR
.....Scanner was called...
Word is:ab&c
Trying the mytoken machine...
current state: 0
character: a
current state: 1
character: b
current state: 2
character: &
I am stuck in state 2
Lexical Error: The string is not in my language
Type is:ERROR
.....Scanner was called...
Word is:23.6
Trying the mytoken machine...
current state: 0
character: 2
I am stuck in state 0
Lexical Error: The string is not in my language
Type is:ERROR
.....Scanner was called...
Word is:2a3
Trying the mytoken machine...
current state: 0
character: 2
I am stuck in state 0
Lexical Error: The string is not in my language
Type is:ERROR
.....Scanner was called...
Word is:22..2
Trying the mytoken machine...
current state: 0
character: 2
I am stuck in state 0
Lexical Error: The string is not in my language
Type is:ERROR
.....Scanner was called...
Word is:23.
Trying the mytoken machine...
current state: 0
character: 2
I am stuck in state 0
Lexical Error: The string is not in my language
Type is:ERROR
.....Scanner was called...
Word is:EOF
Trying the mytoken machine...
current state: 0
character: E
I am stuck in state 0
Lexical Error: The string is not in my language
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started