Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define NMAX 1005
- #define pb push_back
- #define ll long long
- #define INF 0x3f3f3f3f
- #define EPS 1e-3
- using namespace std;
- map<string, int> tabela_codificare = {
- {"int", 2},
- {"double", 3},
- {"char", 4},
- {"if", 5},
- {"else", 6},
- {"while", 7},
- {"for", 8},
- {"input", 9},
- {"print", 11},
- {"+", 12},
- {"-", 13},
- {"*", 14},
- {"/", 15},
- {"=", 16},
- {"<", 17},
- {">", 18},
- {"<=", 19},
- {"==", 20},
- {">=", 21},
- {"[]", 22},
- {"(", 23},
- {")", 24},
- {"[", 25},
- {"]", 26},
- {"{", 27},
- {"}", 28},
- {",", 29}
- // {"", 30}
- };
- map<string, int> TS;
- class Automata {
- private:
- int nr_states, start_state;
- vector<int> state_type;
- map<pair<int,int>, int> edge;
- public:
- Automata(string rules_path) {
- ifstream fin(rules_path);
- fin >> nr_states;
- state_type.resize(nr_states);
- for(int i=0;i<nr_states;++i)
- fin >> state_type[i];
- fin >> start_state;
- int x,y;
- char ch;
- while(fin >> x >> y >> ch) {
- edge[{x,ch}] = y;
- }
- }
- bool check_sequence(string token) {
- int current_state = start_state;
- for(auto it:token) {
- if(edge.find({current_state, it}) == edge.end())
- return false;
- current_state = edge[{current_state, it}];
- }
- return state_type[current_state];
- }
- };
- int check_type(string token, Automata constants_automata, Automata identifiers_automata) {
- if(tabela_codificare.find(token) != tabela_codificare.end())
- return tabela_codificare[token];
- if(identifiers_automata.check_sequence(token))
- return 0;
- if(constants_automata.check_sequence(token))
- return 1;
- return -1;
- }
- int main() {
- char separatori[] = " \n,(){};[]\t";
- string line;
- int nr_lines = 1;
- vector<pair<int, int> > FIP;
- ifstream fin("fisier.in");
- ofstream fout("fisier.out");
- string current_token = "";
- Automata constants_automata = Automata("automat_const.in");
- Automata identifiers_automata = Automata("automat_identifier.in");
- assert(constants_automata.check_sequence("1245439") == true);
- assert(constants_automata.check_sequence("100004") == true);
- assert(constants_automata.check_sequence("+0") == true);
- assert(constants_automata.check_sequence("-41656") == true);
- assert(constants_automata.check_sequence("01312") == false);
- assert(constants_automata.check_sequence("+012") == false);
- assert(identifiers_automata.check_sequence("abfes") == true);
- assert(identifiers_automata.check_sequence("ab__1s") == true);
- assert(identifiers_automata.check_sequence("a1235") == true);
- assert(identifiers_automata.check_sequence("1fesv54") == false);
- assert(identifiers_automata.check_sequence("fes+bgdf") == false);
- while(getline(fin, line)) {
- for(auto it:line) {
- if(strchr(separatori, it)) {
- if(current_token == "") continue;
- int type = check_type(current_token, constants_automata, identifiers_automata);
- if(type == -1) {
- cout << "Eroare la linia " << nr_lines << '\n';
- return 0;
- }
- else if(type == 0 || type == 1){
- if(TS.find(current_token) == TS.end())
- TS[current_token] = TS.size();
- }
- if(type<2) FIP.push_back({type, TS[current_token]});
- else FIP.push_back({type, -1});
- current_token = "";
- }
- else current_token.push_back(it);
- }
- ++nr_lines;
- }
- fout << "TABELA DE SIMBOLURI:\n";
- for(auto it:TS)
- fout << setw(14) << it.first << ' ' << setw(14) << it.second << '\n';
- fout << "\n\nFIP:\n";
- for(auto it:FIP)
- fout << setw(4) << it.first << ' ' << setw(4) << it.second << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement