Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <bits/stdc++.h>
- using namespace std;
- vector<string> inputs; //input name
- vector<string> values;
- vector<string> keyword; //vector of all keywords used in the language
- vector<string> regular_def; //this vector is used in extracting inputs from regular expressions
- string char_to_string(char c)
- {
- stringstream ss;
- string s;
- ss << c;
- ss >> s;
- return s;
- }
- void extract_input_from_re(vector<string> tokens)
- {
- //this loop removes * or + from the end of any token and push it back to tokens again
- for(int i = 1; i < tokens.size(); i++)
- {
- string curr_token = tokens[i];
- if(curr_token.size() > 2 && (curr_token[curr_token.size()-1] == '*' || curr_token[curr_token.size()-1] == '+' ) )
- {
- string s = char_to_string(curr_token[0]);
- for(int k = 1; k < curr_token.size()-1; k++)
- {
- s += curr_token[k];
- }
- tokens.erase(tokens.begin() + i);
- tokens.push_back(s);
- i = 1;
- }
- }
- for(int i = 1; i < tokens.size(); i++)
- {
- string current_token = tokens[i];
- if(current_token[0] == '(' && tokens[i].size() > 1) //if the token starts with an open parenthesis
- {
- for(int k = 0; k < tokens[i].size() ; k++)
- {
- if(current_token[k] == '|')
- {
- tokens.erase(tokens.begin() + i);
- stringstream check1(current_token);
- string intermediate;
- // Tokenizing w.r.t. space '|'
- while(getline(check1, intermediate, '|'))
- {
- tokens.push_back(intermediate);
- //cout << '\t' << intermediate << endl;
- }
- i = 1;
- /*to make the checker start again from the begin of the re because size
- is changed and new items is pushed into it*/
- }
- }
- }
- /*in the previous step if the exist a parenthesis concatenated with the string and the string have more than
- a regular_def inside it ,now the regular definitions are split*/
- }
- for(int j = 1; j < tokens.size(); j++)
- {
- string curr_token = tokens[j];
- if(curr_token[0] == '(' && curr_token.size() > 1)
- {
- string s = char_to_string(curr_token[1]);
- for(int i = 2; i < curr_token.size(); i++)
- {
- s += curr_token[i];
- }
- tokens.erase(tokens.begin() + j);
- tokens.push_back(s);
- j = 1;
- }else if(curr_token.size() > 1 && curr_token[curr_token.size() - 1] == ')')
- {
- string s = char_to_string(curr_token[0]);
- for(int i = 1; i < curr_token.size() - 1; i++)
- {
- s += curr_token[i];
- }
- tokens.erase(tokens.begin() + j);
- tokens.push_back(s);
- j = 1;
- }
- }
- for(int i = 1; i < tokens.size(); i++)
- {
- string current_token = tokens[i];
- int matched = 0;
- for(int j = 0; j < regular_def.size(); j++)
- {
- char temp1[64],temp2[64];
- strcpy(temp1, tokens[i].c_str());
- strcpy(temp2, regular_def[j].c_str());
- if(!strcmp(temp1/*current token*/,temp2)) //if the token equal one of the regular definition then it's not input
- {
- matched = 1;
- break;
- }
- }
- if(matched == 0) //non of the regular definitions are matched
- {
- if(current_token.size() == 1 && current_token[0] != '|' && current_token[0] != '(' && current_token[0] != ')')
- {
- int found = 0;
- for(int z = 0; z < values.size() ; z++)
- {
- if(values[z].size() == 1 && values[z] == current_token)
- {
- found = 1;
- break;
- }
- }
- if(!found)
- {
- inputs.push_back(current_token);
- values.push_back(current_token);
- }
- }else
- {
- for(int m = 0; m < current_token.size(); m++)
- {
- if(current_token[m] != '\\' &¤t_token[m] != 'L' && current_token[m] != '|'&& current_token[m] != '(' && current_token[m] != ')')
- {
- int found = 0;
- for(int z = 0; z < values.size() ; z++)
- {
- if(values[z].size() == 1 && values[z] == char_to_string(current_token[m]))
- {
- found = 1;
- break;
- }
- }
- if(!found)
- {
- inputs.push_back(char_to_string(current_token[m]));
- values.push_back(char_to_string(current_token[m]));
- }
- }
- }
- }
- }
- }
- }
- void table_of_inputs(vector<string> tokens)
- {
- string first_token = tokens[0];
- if(first_token[0] == '[')
- {
- inputs.push_back(char_to_string(first_token[1]));
- values.push_back(char_to_string(first_token[1]));
- for(int i = 1; i < tokens.size() ; i++)
- {
- string token = tokens[i];
- if(tokens[i].size() == 1)
- {
- inputs.push_back(tokens[i]);
- values.push_back(tokens[i]);
- }
- else if(token[1] != ']')
- {
- inputs.push_back(char_to_string(token[1]));
- values.push_back(char_to_string(token[1]));
- }else
- {
- inputs.push_back(char_to_string(token[0]));
- values.push_back(char_to_string(token[0]));
- }
- }
- }
- else if(first_token[0] == '{')
- {
- if(first_token.size() != 1) // case that the first keyword have a open parentheses with it
- {
- string s = char_to_string(first_token[1]);
- for(int i = 2; i < first_token.size(); i++)
- {
- s += first_token[i];
- }
- keyword.push_back(s);
- }
- for(int i = 1; i < tokens.size() - 1; i++) // inserting the rest of keywords except the final one
- {
- keyword.push_back(tokens[i]);
- }
- string last_token = tokens[tokens.size() - 1];
- if(last_token.size() != 1)
- {
- string s = char_to_string(last_token[0]);
- for(int i = 1; i < last_token.size() - 1; i++)
- {
- s += last_token[i];
- }
- keyword.push_back(s);
- }
- }else if(tokens[1] == "=" && tokens.size() > 2) //regular definitions
- {
- if(tokens.size() > 4 && tokens[3] == "-")
- {
- string temp = tokens[2] + "-"+ tokens[4];
- inputs.push_back(tokens[0]);
- values.push_back(temp);
- regular_def.push_back(tokens[0]);
- }
- else
- {
- for(int i = 2; i < tokens.size() && tokens[i].size() <= 3 ; i = i + 2)
- {
- inputs.push_back(tokens[0]);
- values.push_back(tokens[i]);
- }
- regular_def.push_back(tokens[0]);
- }
- }else if(first_token[first_token.size() - 1] == ':') //regular expressions
- {
- extract_input_from_re(tokens);
- }
- }
- void read_file()
- {
- ifstream infile("thefile.txt");
- string line;
- while (getline(infile, line))
- {
- vector <string> tokens;
- stringstream check1(line);
- string intermediate;
- // Tokenizing w.r.t. space ' '
- while(getline(check1, intermediate, ' '))
- {
- tokens.push_back(intermediate);
- }
- table_of_inputs(tokens);
- }
- }
- int main()
- {
- read_file();
- /* //printing the inputs vector
- cout << "inputs vector:" << endl;
- for(int i = 0;i<inputs.size();i++)
- {
- cout << inputs[i] << '\t' <<values[i]<< '\n';
- }
- cout << "keywords vector:" << endl;
- //printing the keyword vector
- for(int i = 0;i<keyword.size();i++)
- {
- cout <<keyword[i]<< '\n';
- }
- cout << "regular_def vector:" << endl;
- //printing the regular_def vector
- for(int i = 0;i<regular_def.size();i++)
- {
- cout <<regular_def[i]<< '\n';
- }*/
- //printing the regular_def vector
- for(int i = 0;i<inputs.size();i++)
- {
- cout <<inputs[i]<<'\t'<< values[i]<<'\n';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement