Advertisement
Guest User

parser

a guest
Mar 21st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <string>
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8. vector<string> inputs;          //input name
  9. vector<string> values;
  10. vector<string> keyword;         //vector of all keywords used in the language
  11. vector<string> regular_def;     //this vector is used in extracting inputs from regular expressions
  12.  
  13. string char_to_string(char c)
  14. {
  15.     stringstream ss;
  16.     string s;
  17.     ss << c;
  18.     ss >> s;
  19.     return s;
  20. }
  21.  
  22. void  extract_input_from_re(vector<string> tokens)
  23. {
  24.         //this loop removes * or + from the end of any token and push it back to tokens again
  25.     for(int i = 1; i < tokens.size(); i++)
  26.     {
  27.         string curr_token = tokens[i];
  28.         if(curr_token.size() > 2 && (curr_token[curr_token.size()-1] == '*' || curr_token[curr_token.size()-1] == '+' ) )
  29.         {
  30.             string s = char_to_string(curr_token[0]);
  31.             for(int k = 1; k < curr_token.size()-1; k++)
  32.             {
  33.             s += curr_token[k];
  34.             }
  35.             tokens.erase(tokens.begin() + i);
  36.             tokens.push_back(s);
  37.             i = 1;
  38.         }
  39.     }
  40.  
  41.     for(int i = 1; i < tokens.size(); i++)
  42.     {
  43.     string current_token = tokens[i];
  44.     if(current_token[0] == '(' && tokens[i].size() > 1)    //if the token starts with an open parenthesis
  45.     {
  46.         for(int k = 0; k < tokens[i].size() ; k++)
  47.         {
  48.             if(current_token[k] == '|')
  49.             {
  50.                 tokens.erase(tokens.begin() + i);
  51.                 stringstream check1(current_token);
  52.                 string intermediate;
  53.                 // Tokenizing w.r.t. space '|'
  54.                 while(getline(check1, intermediate, '|'))
  55.                 {
  56.                     tokens.push_back(intermediate);
  57.                     //cout << '\t' << intermediate << endl;
  58.                 }
  59.                 i = 1;
  60.                        /*to make the checker start again from the begin of the re because size
  61.                 is changed and new items is pushed into it*/
  62.             }
  63.         }
  64.     }
  65.  
  66.     /*in the previous step if the exist a parenthesis concatenated with the string and the string have more than
  67.     a regular_def inside it ,now the regular definitions are split*/
  68.     }
  69.  
  70.     for(int j = 1; j < tokens.size(); j++)
  71.     {
  72.         string curr_token = tokens[j];
  73.         if(curr_token[0] == '(' && curr_token.size() > 1)
  74.         {
  75.             string s = char_to_string(curr_token[1]);
  76.             for(int i = 2; i < curr_token.size(); i++)
  77.             {
  78.             s += curr_token[i];
  79.             }
  80.             tokens.erase(tokens.begin() + j);
  81.             tokens.push_back(s);
  82.             j = 1;
  83.         }else if(curr_token.size() > 1 && curr_token[curr_token.size() - 1] == ')')
  84.         {
  85.             string s = char_to_string(curr_token[0]);
  86.             for(int i = 1; i < curr_token.size() - 1; i++)
  87.             {
  88.             s += curr_token[i];
  89.             }
  90.             tokens.erase(tokens.begin() + j);
  91.             tokens.push_back(s);
  92.             j = 1;
  93.         }
  94.     }
  95.     for(int i = 1; i < tokens.size(); i++)
  96.     {
  97.         string current_token = tokens[i];
  98.         int matched = 0;
  99.         for(int j = 0; j < regular_def.size(); j++)
  100.         {
  101.             char temp1[64],temp2[64];
  102.             strcpy(temp1, tokens[i].c_str());
  103.             strcpy(temp2, regular_def[j].c_str());
  104.             if(!strcmp(temp1/*current token*/,temp2))    //if the token equal one of the regular definition then it's not input
  105.             {
  106.                 matched = 1;
  107.                 break;
  108.             }
  109.         }
  110.  
  111.         if(matched == 0)            //non of the regular definitions are matched
  112.         {
  113.             if(current_token.size() == 1 && current_token[0] != '|' && current_token[0] != '(' && current_token[0] != ')')
  114.             {
  115.                 int found = 0;
  116.                 for(int z = 0; z < values.size() ; z++)
  117.                 {
  118.                         if(values[z].size() == 1 && values[z] == current_token)
  119.                         {
  120.                             found = 1;
  121.                             break;
  122.                         }
  123.                 }
  124.                 if(!found)
  125.                 {
  126.                     inputs.push_back(current_token);
  127.                     values.push_back(current_token);
  128.                 }
  129.             }else
  130.             {
  131.                 for(int m = 0; m < current_token.size(); m++)
  132.                 {
  133.                     if(current_token[m] != '\\' &&current_token[m] != 'L' && current_token[m] != '|'&& current_token[m] != '(' && current_token[m] != ')')
  134.                     {
  135.                         int found = 0;
  136.                         for(int z = 0; z < values.size() ; z++)
  137.                             {
  138.                                 if(values[z].size() == 1 && values[z] == char_to_string(current_token[m]))
  139.                                     {
  140.                                     found = 1;
  141.                                     break;
  142.                                     }
  143.                             }
  144.                             if(!found)
  145.                             {
  146.                             inputs.push_back(char_to_string(current_token[m]));
  147.                             values.push_back(char_to_string(current_token[m]));
  148.                             }
  149.                     }
  150.                 }
  151.             }
  152.         }
  153.     }
  154. }
  155.  
  156. void table_of_inputs(vector<string> tokens)
  157. {
  158.     string first_token = tokens[0];
  159.     if(first_token[0] == '[')
  160.     {
  161.         inputs.push_back(char_to_string(first_token[1]));
  162.         values.push_back(char_to_string(first_token[1]));
  163.         for(int i = 1; i < tokens.size() ; i++)
  164.         {
  165.             string token = tokens[i];
  166.             if(tokens[i].size() == 1)
  167.             {
  168.             inputs.push_back(tokens[i]);
  169.             values.push_back(tokens[i]);
  170.             }
  171.             else if(token[1] != ']')
  172.             {
  173.             inputs.push_back(char_to_string(token[1]));
  174.             values.push_back(char_to_string(token[1]));
  175.             }else
  176.             {
  177.             inputs.push_back(char_to_string(token[0]));
  178.             values.push_back(char_to_string(token[0]));
  179.             }
  180.         }
  181.     }
  182.     else if(first_token[0] == '{')
  183.     {
  184.         if(first_token.size() != 1)         // case that the first keyword have a open parentheses with it
  185.         {
  186.             string s = char_to_string(first_token[1]);
  187.             for(int i = 2; i < first_token.size(); i++)
  188.             {
  189.             s += first_token[i];
  190.             }
  191.             keyword.push_back(s);
  192.  
  193.         }
  194.         for(int i = 1; i < tokens.size() - 1; i++)  // inserting the rest of keywords except the final one
  195.         {
  196.             keyword.push_back(tokens[i]);
  197.         }
  198.         string last_token = tokens[tokens.size() - 1];
  199.         if(last_token.size() != 1)
  200.         {
  201.             string s = char_to_string(last_token[0]);
  202.             for(int i = 1; i < last_token.size() - 1; i++)
  203.             {
  204.             s += last_token[i];
  205.             }
  206.             keyword.push_back(s);
  207.         }
  208.  
  209.     }else if(tokens[1] == "=" && tokens.size() > 2)      //regular definitions
  210.         {
  211.             if(tokens.size() > 4 && tokens[3] == "-")
  212.             {
  213.                 string temp = tokens[2] + "-"+ tokens[4];
  214.                 inputs.push_back(tokens[0]);
  215.                 values.push_back(temp);
  216.                 regular_def.push_back(tokens[0]);
  217.             }
  218.             else
  219.             {
  220.                 for(int i = 2; i < tokens.size() && tokens[i].size() <= 3 ; i = i + 2)
  221.                 {
  222.                 inputs.push_back(tokens[0]);
  223.                 values.push_back(tokens[i]);
  224.                 }
  225.                 regular_def.push_back(tokens[0]);
  226.             }
  227.     }else if(first_token[first_token.size() - 1] == ':')       //regular expressions
  228.     {
  229.         extract_input_from_re(tokens);
  230.     }
  231. }
  232.  
  233.  
  234. void read_file()
  235. {
  236.     ifstream infile("thefile.txt");
  237.     string line;
  238.     while (getline(infile, line))
  239.     {
  240.         vector <string> tokens;
  241.         stringstream check1(line);
  242.         string intermediate;
  243.         // Tokenizing w.r.t. space ' '
  244.         while(getline(check1, intermediate, ' '))
  245.         {
  246.             tokens.push_back(intermediate);
  247.         }
  248.         table_of_inputs(tokens);
  249.     }
  250. }
  251.  
  252.  
  253. int main()
  254. {
  255.     read_file();
  256.   /*  //printing the inputs vector
  257.     cout << "inputs vector:" << endl;
  258.     for(int i = 0;i<inputs.size();i++)
  259.     {
  260.         cout << inputs[i] << '\t' <<values[i]<< '\n';
  261.     }
  262.     cout << "keywords vector:" << endl;
  263.     //printing the keyword vector
  264.     for(int i = 0;i<keyword.size();i++)
  265.     {
  266.         cout <<keyword[i]<< '\n';
  267.     }
  268.     cout << "regular_def vector:" << endl;
  269.     //printing the regular_def vector
  270.     for(int i = 0;i<regular_def.size();i++)
  271.     {
  272.         cout <<regular_def[i]<< '\n';
  273.     }*/
  274.  
  275.      //printing the regular_def vector
  276.     for(int i = 0;i<inputs.size();i++)
  277.     {
  278.         cout <<inputs[i]<<'\t'<<  values[i]<<'\n';
  279.     }
  280.     return 0;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement