Advertisement
warrior98

Analizor lexical

Nov 25th, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define NMAX 1005
  3. #define pb push_back
  4. #define ll long long
  5. #define INF 0x3f3f3f3f
  6. #define EPS 1e-3
  7.  
  8. using namespace std;
  9.  
  10. map<string, int> tabela_codificare = {
  11.     {"int", 2},
  12.     {"double", 3},
  13.     {"char", 4},
  14.     {"if", 5},
  15.     {"else", 6},
  16.     {"while", 7},
  17.     {"for", 8},
  18.     {"input", 9},
  19.     {"print", 11},
  20.     {"+", 12},
  21.     {"-", 13},
  22.     {"*", 14},
  23.     {"/", 15},
  24.     {"=", 16},
  25.     {"<", 17},
  26.     {">", 18},
  27.     {"<=", 19},
  28.     {"==", 20},
  29.     {">=", 21},
  30.     {"[]", 22},
  31.     {"(", 23},
  32.     {")", 24},
  33.     {"[", 25},
  34.     {"]", 26},
  35.     {"{", 27},
  36.     {"}", 28},
  37.     {",", 29}
  38. //  {"", 30}
  39. };
  40.  
  41. map<string, int> TS;
  42.  
  43. class Automata {
  44. private:
  45.     int nr_states, start_state;
  46.     vector<int> state_type;
  47.     map<pair<int,int>, int> edge;
  48.  
  49. public:
  50.     Automata(string rules_path) {
  51.         ifstream fin(rules_path);
  52.  
  53.         fin >> nr_states;
  54.         state_type.resize(nr_states);
  55.  
  56.         for(int i=0;i<nr_states;++i)
  57.             fin >> state_type[i];
  58.  
  59.         fin >> start_state;
  60.  
  61.         int x,y;
  62.         char ch;
  63.         while(fin >> x >> y >> ch) {
  64.             edge[{x,ch}] = y;
  65.         }
  66.     }
  67.  
  68.     bool check_sequence(string token) {
  69.         int current_state = start_state;
  70.  
  71.         for(auto it:token) {
  72.             if(edge.find({current_state, it}) == edge.end())
  73.                 return false;
  74.  
  75.             current_state = edge[{current_state, it}];
  76.         }
  77.  
  78.         return state_type[current_state];
  79.     }
  80. };
  81.  
  82. int check_type(string token, Automata constants_automata, Automata identifiers_automata) {
  83.     if(tabela_codificare.find(token) != tabela_codificare.end())
  84.         return tabela_codificare[token];
  85.  
  86.     if(identifiers_automata.check_sequence(token))
  87.         return 0;
  88.  
  89.     if(constants_automata.check_sequence(token))
  90.         return 1;
  91.  
  92.     return -1;
  93. }
  94.  
  95. int main() {
  96.     char separatori[] = " \n,(){};[]\t";
  97.     string line;
  98.     int nr_lines = 1;
  99.  
  100.     vector<pair<int, int> > FIP;
  101.  
  102.     ifstream fin("fisier.in");
  103.     ofstream fout("fisier.out");
  104.  
  105.     string current_token = "";
  106.  
  107.     Automata constants_automata = Automata("automat_const.in");
  108.     Automata identifiers_automata = Automata("automat_identifier.in");
  109.  
  110.     assert(constants_automata.check_sequence("1245439") == true);
  111.     assert(constants_automata.check_sequence("100004") == true);
  112.     assert(constants_automata.check_sequence("+0") == true);
  113.     assert(constants_automata.check_sequence("-41656") == true);
  114.     assert(constants_automata.check_sequence("01312") == false);
  115.     assert(constants_automata.check_sequence("+012") == false);
  116.  
  117.     assert(identifiers_automata.check_sequence("abfes") == true);
  118.     assert(identifiers_automata.check_sequence("ab__1s") == true);
  119.     assert(identifiers_automata.check_sequence("a1235") == true);
  120.     assert(identifiers_automata.check_sequence("1fesv54") == false);
  121.     assert(identifiers_automata.check_sequence("fes+bgdf") == false);
  122.  
  123.     while(getline(fin, line)) {
  124.         for(auto it:line) {
  125.             if(strchr(separatori, it)) {
  126.                 if(current_token == "") continue;
  127.  
  128.                 int type = check_type(current_token, constants_automata, identifiers_automata);
  129.  
  130.                 if(type == -1) {
  131.                     cout << "Eroare la linia " << nr_lines << '\n';
  132.                     return 0;
  133.                 }
  134.                 else if(type == 0 || type == 1){
  135.                     if(TS.find(current_token) == TS.end())
  136.                         TS[current_token] = TS.size();
  137.                 }
  138.  
  139.                 if(type<2) FIP.push_back({type, TS[current_token]});
  140.                 else FIP.push_back({type, -1});
  141.  
  142.                 current_token = "";
  143.             }
  144.             else current_token.push_back(it);
  145.         }
  146.  
  147.         ++nr_lines;
  148.     }
  149.  
  150.     fout << "TABELA DE SIMBOLURI:\n";
  151.     for(auto it:TS)
  152.         fout << setw(14) << it.first << ' ' << setw(14) << it.second << '\n';
  153.  
  154.     fout << "\n\nFIP:\n";
  155.     for(auto it:FIP)
  156.         fout << setw(4) << it.first << ' ' << setw(4) << it.second << '\n';
  157.  
  158.     return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement