Skip to content

Instantly share code, notes, and snippets.

@lvwvm
Last active August 29, 2015 14:20
Show Gist options
  • Save lvwvm/6118189409bd0906c6d1 to your computer and use it in GitHub Desktop.
Save lvwvm/6118189409bd0906c6d1 to your computer and use it in GitHub Desktop.
Numeric Literal DFA
#include <iostream>
#include <fstream>
#include <utility>
#include <stdexcept>
#include <map>
using namespace std;
/******************
* STATE types act as identification for each state in the DFA, GARBAGE is set automatically.
******************/
enum STATE{
S0 =0,
S1 =1,
S2 =2,
S3 =3,
S4 =4,
S5 =5,
S6 =6,
S7 =7,
GARBAGE
};
/******************
* The State structure used to contain model data for any particular state.
******************/
struct State {
STATE state;
const char c;
struct State operator=(State rhs){
state = rhs.state;
return *this;
}
};
/******************
* Transition Map
*
* sigma - A list of transitions defined in a map.
******************/
map < pair<STATE,char>, STATE> sigma = {
{{S0,'i'}, S2},
{{S0,'s'}, S1},
{{S0,'d'}, S3},
{{S1,'i'}, S2},
{{S1,'d'}, S3},
{{S2,'e'}, S4},
{{S2,'d'}, S3},
{{S2,'i'}, S2},
{{S3,'e'}, S4},
{{S4,'i'}, S6},
{{S4,'s'}, S5},
{{S5,'i'}, S6},
{{S6,'i'}, S6},
{{S3,'i'}, S7},
{{S7,'e'}, S4},
{{S7,'i'}, S7}
};
/******************
* Validation functions
* isInt(), isDec(), isSign(), isExp()
*
* @param i - the integer used for ordinality checking.
******************/
bool isInt(int i){
return i >= 48 && i <= 57;
}
bool isSign(int i){
return i == 43 || i == 45;
}
bool isDec(int i){
return i == 46;
}
bool isExp(int i){
return i == 69 || i == 101;
}
bool isValid(const char &c){
int i = int(c);
return isInt(i) || isSign(i) || isDec(i) || isExp(i);
}
char to_symbol(const char &c){
int n = int(c);
if (isInt(n)){
return 'i';
}
else if (isExp(n)){
return 'e';
}
else if (isSign(n)){
return 's';
}
else if (isDec(n)){
return 'd';
}
else{
return '#';
}
}
/******************
* nextState(State &s, const char &c)
*
* @params
*
* &s - current State
* &c - input to process
*
* return new
******************/
State nextState(State &s, const char &c){
try{
s.state = sigma.at({s.state, to_symbol(c)});
}
catch (std::out_of_range){
s.state =GARBAGE;
}
return s;
}
/******************
* isNumericLiteral(string c)
*
******************/
bool isNumericLiteral(string c){
unsigned int i = 0;
State s = { S0 , c[i] };
while(i < c.length()){
if (isValid(c[i]) && s.state != GARBAGE){
s = nextState(s, c[i]);
}
else{
return false;
}
i++;
}
return s.state == S2 || s.state == S6 || s.state == S7 || s.state == S3;
}
int main() {
string str;
ifstream inf("test_cases.dat"); // Make Input file
ofstream outf("out.txt"); // Make Output file
while(!inf.eof()){ // Iterate through the File until the end
inf >> str; // Read line of File
if (isNumericLiteral(str)) { // if
outf << str << " is a valid numeric literal.\n";
cout << str << " is a valid numeric literal.\n";
}
else{
outf << str << " is NOT a valid numeric literal\n";
cout << str << " is NOT a valid numeric literal.\n";
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment