-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
67 lines (59 loc) · 2.2 KB
/
Parser.cpp
File metadata and controls
67 lines (59 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
CSE355 optional project, Parser.h Parser.cpp
Purpose: To store all the input in memory so when the program is
making an NFA/DFA it knows all of the transitions, alphabets and states
before hand which makes it easier to generate the new machine.
@author Suhail Ghafoor
@version 0.1 03/08/18
*/
#include "Parser.h"
/**
* Default constructor since parser doesn't have any variables
*/
Parser::Parser() = default;
/**
* Adds alphabet to parser
* @param line - The input line that contains alphabet
*/
void Parser::addAlphabet(string line) {
line.erase(remove_if(line.begin(), line.end(), ::isspace), line.end());
alphabet.push_back(line);
}
/**
* Store transition from input file
* @param line - The input line that contains a transition. This line
* is turned into a struct of type transition.
*/
void Parser::addTransitions(string line) {
string initialState = line.substr(0,line.find_first_of(' '));
string alphabet = line.substr(line.find_first_of(' '), line.find_last_of(' '));
string nState = line.substr(line.find_last_of(' '), line.length()-1);
Transition * trans = new Transition(initialState, alphabet, nState);
cleanTransition(trans);
transitions.push_back(trans);
}
/**
* Sets the initial state
* @param line - Line containing the name of the initial state
*/
void Parser::setInitial(string line) {
line.erase(remove_if(line.begin(), line.end(), ::isspace), line.end());
initialState = line;
}
/**
* Add all the final states to the parser object
* @param line - Line that contains the name of final states
*/
void Parser::addFinalStates(string line) {
line.erase(remove_if(line.begin(), line.end(), ::isspace), line.end());
finalStates.push_back(line);
}
/**
* Makes sure the transition object values do not contain any spaces
* @param t - The transition object to clean
*/
void Parser::cleanTransition(Transition *t) {
t->initialState.erase(remove_if(t->initialState.begin(), t->initialState.end(), ::isspace), t->initialState.end());
t->alphabet.erase(remove_if(t->alphabet.begin(), t->alphabet.end(), ::isspace), t->alphabet.end());
t->nState.erase(remove_if(t->nState.begin(), t->nState.end(), ::isspace), t->nState.end());
}