Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. vector<string> tokenizer(string line)
  7. {
  8. vector <string> tokens;
  9. stringstream check1(line);
  10. string intermediate;
  11. // Tokenizing w.r.t. space ' '
  12. while(getline(check1, intermediate, ' '))
  13. {
  14. tokens.push_back(intermediate);
  15. }
  16. return tokens;
  17. }
  18.  
  19. bool check_duplicate (string str , vector<string> temp)
  20. {
  21. for(int i=0; i < temp.size(); i++)
  22. {
  23. if(temp[i] == str)
  24. {
  25. return true;
  26. }
  27. }
  28. return false;
  29. }
  30.  
  31. vector<int> find_match(string str,vector<string> tokens)
  32. {
  33. vector<int> indexes;
  34. for(int i = 0; i < tokens.size(); i++)
  35. {
  36. if(str == tokens[i])
  37. {
  38. indexes.push_back(i);
  39. }
  40. }
  41. return indexes;
  42. }
  43.  
  44. int get_index(string poped,vector<vector <string> > input)
  45. {
  46. for(int i = 0; i < input.size();i++)
  47. {
  48. if(poped == input[i][0])
  49. return i ;
  50. }
  51. return -1;
  52. }
  53.  
  54. string get_first_token(string str)
  55. {
  56. stringstream check1(str);
  57. string intermediate;
  58. if(getline(check1, intermediate, ' ')) // Tokenizing w.r.t. space ' '
  59. return intermediate;
  60. }
  61.  
  62. bool is_non_terminal(string str)
  63. {
  64. string first_token = get_first_token(str);
  65. if(first_token[0] == '\'')
  66. return false;
  67. return true;
  68. }
  69.  
  70. string remove_quotes(string str)
  71. {
  72. return str.substr(1, str.size()-2);
  73. }
  74.  
  75. vector<vector <string> > find_first(vector<vector<string> > input)
  76. {
  77. stack<string> st;
  78. vector<vector <string> > first;
  79. vector<string> temp;
  80. for(int i = input.size() - 1; i >= 0; i--)
  81. {
  82. temp.push_back(input[i][0]);
  83. for(int j = 1; j < input[i].size(); j++)
  84. {
  85. if(is_non_terminal(input[i][j]))
  86. {
  87. string first_token = get_first_token(input[i][j]);
  88. int first_index = get_index(first_token,first);
  89. if(first_index == -1)
  90. st.push(get_first_token(input[i][j])); //split on space and push non terminal on the stack .
  91. else
  92. {
  93. for(int k = 1; k < first[first_index].size(); k++)
  94. {
  95. temp.push_back(first[first_index][k]);
  96. }
  97. }
  98. }
  99. else
  100. {
  101. string terminal = get_first_token(input[i][j]);
  102. if(!check_duplicate(terminal,temp))
  103. temp.push_back(remove_quotes(terminal)); //push terminal
  104. //before pushing in the vector check duplicates
  105. }
  106. }
  107.  
  108. while(!st.empty())
  109. {
  110. string poped;
  111. poped = st.top();
  112. st.pop();
  113. int input_index = get_index(poped,input);
  114. for(int j = 1; j < input[input_index].size(); j++)
  115. {
  116. if(is_non_terminal(input[input_index][j]))
  117. {
  118. string first_token = get_first_token(input[input_index][j]);
  119. int first_index = get_index(first_token,first);
  120. if(first_index == -1)
  121. st.push(get_first_token(input[input_index][j])); //split on space and push non terminal on the stack .
  122. else
  123. {
  124. for(int k = 1; k < first[first_index].size(); k++)
  125. {
  126. temp.push_back(first[first_index][k]);
  127. }
  128. }
  129.  
  130. // st.push(get_first_token(input[index][j])); //split on space and push non terminal on the stack .
  131. }
  132. else
  133. {
  134. string terminal = get_first_token(input[input_index][j]);
  135. if(!check_duplicate(terminal,temp))
  136. temp.push_back(remove_quotes(terminal)); //push terminal
  137. //before pushing in the vector check duplicates
  138. }
  139. }
  140. }
  141.  
  142. first.push_back(temp);
  143. temp.clear();
  144. }
  145. reverse(first.begin(),first.end());
  146.  
  147. return first;
  148. }
  149.  
  150. vector <vector<string> > find_follow(vector <vector<string> > input)
  151. {
  152. vector <vector<string> > first = find_first(input);
  153. cout <<first.size()<<endl;
  154. vector <vector<string> > follow;
  155. vector <string> temp;
  156. vector <string> tokens;
  157. for(int i =0; i < 4; i++)
  158. {
  159. //cout <<"hello" <<endl;
  160. string search_follow = input[i][0];
  161. temp.push_back(search_follow);
  162. if(i == 0)
  163. temp.push_back("$");
  164. for(int j =0; j < input.size(); j++)
  165. {
  166. for(int k = 1; k < input[j].size(); k++)
  167. {
  168. tokens = tokenizer(input[j][k]);
  169. vector<int> indexes = find_match(search_follow,tokens);
  170. for(int l =0; l < indexes.size(); l++)
  171. {
  172. if(tokens.size() > indexes[l] + 1 && !is_non_terminal(tokens[indexes[l]+ 1])) //after the matched string check if terminal
  173. {
  174. temp.push_back(remove_quotes(tokens[indexes[l]+ 1]));
  175. }
  176. else if(indexes[l] == tokens.size() - 1 && input[j][0] != tokens[indexes[l]])
  177. {
  178.  
  179. int follow_index = get_index(input[j][0],follow);
  180. for(int m = 1; m < follow[follow_index].size();m++)
  181. {
  182. temp.push_back(follow[follow_index][m]);
  183. cout << follow[follow_index][m]<<endl;
  184. }
  185.  
  186. }else if(tokens.size() > indexes[l] + 1 && input[j][0] != tokens[indexes[l]+1])
  187. {
  188. int first_index = get_index(tokens[indexes[l]+1],first);
  189. for(int m = 1; m < first[first_index].size();m++)
  190. temp.push_back(first[first_index][m]);
  191. }
  192. }
  193. }
  194. }
  195. /* for(int j=0; j <temp.size(); j++)
  196. {
  197. cout << temp[j] << '\t';
  198. }
  199. cout <<endl;*/
  200. follow.push_back(temp);
  201. temp.clear();
  202. }
  203. for(int i = 0; i < follow.size(); i++)
  204. {
  205. for(int j=0; j <follow[i].size(); j++)
  206. {
  207. cout << follow[i][j] << '\t';
  208. }
  209. cout <<endl;
  210. }
  211. return follow;
  212. }
  213.  
  214. int main()
  215. {
  216. vector <vector <string> > input;
  217. vector<string> temp;
  218. temp.push_back("METHOD_BODY");
  219. temp.push_back("STATEMENT_LIST");
  220. input.push_back(temp);
  221. temp.clear();
  222. temp.push_back("STATEMENT_LIST");
  223. temp.push_back("STATEMENT STATEMENT_LIST'");
  224. input.push_back(temp);
  225. temp.clear();
  226. temp.push_back("STATEMENT_LIST'");
  227. temp.push_back("STATEMENT STATEMENT_LIST'");
  228. temp.push_back("'\\L'");
  229. input.push_back(temp);
  230. temp.clear();
  231. temp.push_back("STATEMENT");
  232. temp.push_back("DECLARATION");
  233. temp.push_back("IF");
  234. temp.push_back("WHILE");
  235. temp.push_back("ASSIGNMENT");
  236. input.push_back(temp);
  237. temp.clear();
  238. temp.push_back("DECLARATION");
  239. temp.push_back("PRIMITIVE_TYPE 'id';'");
  240. input.push_back(temp);
  241. temp.clear();
  242. temp.push_back("PRIMITIVE_TYPE");
  243. temp.push_back("'int'");
  244. temp.push_back("'float'");
  245. input.push_back(temp);
  246. temp.clear();
  247. temp.push_back("IF");
  248. temp.push_back("'if' '(' EXPRESSION ')' '{' STATEMENT '}' 'else' '{' STATEMENT '}'");
  249. input.push_back(temp);
  250. temp.clear();
  251. temp.push_back("WHILE");
  252. temp.push_back("'while' '(' EXPRESSION ')' '{' STATEMENT '}'");
  253. input.push_back(temp);
  254. temp.clear();
  255. temp.push_back("ASSIGNMENT");
  256. temp.push_back("'id' '=' EXPRESSION ';'");
  257. input.push_back(temp);
  258. temp.clear();
  259. temp.push_back("EXPRESSION");
  260. temp.push_back("SIMPLE_EXPRESSION ACTION'");
  261. input.push_back(temp);
  262. temp.clear();
  263. temp.push_back("ACTION'");
  264. temp.push_back("'relop' SIMPLE_EXPRESSION");
  265. temp.push_back("'\\L'");
  266. input.push_back(temp);
  267. temp.clear();
  268. temp.push_back("SIMPLE_EXPRESSION");
  269. temp.push_back("TERM SIMPLE_EXPRESSION'");
  270. temp.push_back("SIGN TERM SIMPLE_EXPRESSION'");
  271. input.push_back(temp);
  272. temp.clear();
  273. temp.push_back("SIMPLE_EXPRESSION'");
  274. temp.push_back("'addop' TERM SIMPLE_EXPRESSION'");
  275. temp.push_back("'\\L'");
  276. input.push_back(temp);
  277. temp.clear();
  278. temp.push_back("TERM");
  279. temp.push_back("FACTOR TERM'");
  280. input.push_back(temp);
  281. temp.clear();
  282. temp.push_back("TERM'");
  283. temp.push_back("'mulop' FACTOR TERM'");
  284. temp.push_back("'\\L'");
  285. input.push_back(temp);
  286. temp.clear();
  287. temp.push_back("FACTOR");
  288. temp.push_back("'id'");
  289. temp.push_back("'num'");
  290. temp.push_back("'(' EXPRESSION ')'");
  291. input.push_back(temp);
  292. temp.clear();
  293. temp.push_back("SIGN");
  294. temp.push_back("'+'");
  295. temp.push_back("'-'");
  296. input.push_back(temp);
  297. find_follow(input);
  298. return 0;
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement