Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.27 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.util.Arrays;
  6. import java.util.Scanner;
  7. import java.util.Stack;
  8.  
  9. public class Closure1 {
  10. static Scanner input;
  11. static Stack<Production> grammar;
  12. static Stack<Configuration> question;
  13. static FirstSet firstSet;
  14. static FileWriter outputFile;
  15.  
  16. public static void main(String[] args) {
  17. grammar = new Stack<Production>();
  18. question = new Stack<Configuration>();
  19. input("input.txt");
  20.  
  21. try {
  22. outputFile = new FileWriter("output.txt");
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26.  
  27. // showGrammar();
  28.  
  29. firstSet = new FirstSet(grammar);
  30. // firstSet.showFirstSet();
  31.  
  32. // System.out.println("Question:");
  33. // showConfiguration(question);
  34. // System.out.println();
  35.  
  36. solveQuestion();
  37. try {
  38. outputFile.close();
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43.  
  44. private static void solveQuestion() {
  45. Stack<Configuration> answer = new Stack<Configuration>();
  46. for (int i = 0; i < question.size(); i++) {
  47. answer.push(question.get(i).clone());
  48. doClosure1(answer);
  49. //showConfiguration(answer);
  50. mergeAnswer(answer);
  51. writeAnswer(answer);
  52. showConfiguration(answer);
  53. System.out.println("#");
  54. answer.clear();
  55. }
  56. }
  57.  
  58. private static void writeAnswer(Stack<Configuration> answer) {
  59. String content = "";
  60. for (int i = 0; i < answer.size(); i++) {
  61. content += answer.get(i).from + "->";
  62. // show String to with *
  63. for (int j = 0; j < answer.get(i).to.length(); j++) {
  64. if (answer.get(i).point == j) {
  65. content += '*';
  66. }
  67. content += answer.get(i).to.charAt(j);
  68. }
  69.  
  70. if (answer.get(i).point == answer.get(i).to.length()) {
  71. content += '*';
  72. }
  73.  
  74. // show lookahead
  75. content += " {" + answer.get(i).lookahead.charAt(0);
  76. for (int j = 1; j < answer.get(i).lookahead.length(); j++) {
  77. content += "," + answer.get(i).lookahead.charAt(j);
  78. }
  79. content += "}\r\n";
  80. }
  81. content += "#\r\n";
  82. try {
  83. outputFile.write(content);
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88.  
  89. private static void mergeAnswer(Stack<Configuration> answer) {
  90. for (int i = 0; i < answer.size(); i++) {
  91. for (int j = 0; j < i; j++) {
  92. if (hasSameProductionWithStar(answer, i, j)) {
  93. mergeLookahead(answer, i, j);
  94. answer.remove(i);
  95. i--;
  96. break;
  97. }
  98. }
  99. }
  100. }
  101.  
  102. private static void mergeLookahead(Stack<Configuration> answer, int i, int j) {
  103. // union i's lookahead into j's lookahead
  104. String ilookahead = answer.get(i).lookahead;
  105. String jlookahead = answer.get(j).lookahead;
  106. jlookahead = union(jlookahead, ilookahead);
  107. answer.get(j).lookahead = jlookahead;
  108. }
  109.  
  110. private static String union(String alk, String blk) {
  111. // union into alk
  112. for (int i = 0; i < blk.length(); i++) {
  113. char c = blk.charAt(i);
  114. if (alk.indexOf(c) == -1) {
  115. alk = alk + c;
  116. }
  117. }
  118. char[] array = alk.toCharArray();
  119. Arrays.sort(array);
  120. alk = String.valueOf(array);
  121. return alk;
  122. }
  123.  
  124. private static boolean hasSameProductionWithStar(
  125. Stack<Configuration> answer, int i, int j) {
  126. char ifrom = answer.get(i).from;
  127. String ito = answer.get(i).to;
  128. int ipoint = answer.get(i).point;
  129.  
  130. char jfrom = answer.get(j).from;
  131. String jto = answer.get(j).to;
  132. int jpoint = answer.get(j).point;
  133.  
  134. if (ifrom == jfrom && ito.equals(jto) && ipoint == jpoint) {
  135. return true;
  136. } else {
  137. return false;
  138. }
  139. }
  140.  
  141. private static void doClosure1(Stack<Configuration> answer) {
  142. for (int i = 0; i < answer.size(); i++) {
  143. Configuration conf = answer.get(i);
  144.  
  145. if (conf.point < conf.to.length() - 1) {// before S->A*$
  146. char symbol = conf.to.charAt(conf.point);
  147. if (!isTerminal(symbol)) {
  148.  
  149. // make lookahead
  150. String afterpoint = conf.to.substring(conf.point + 1);
  151. String lookahead = firstSet.getFirstSet(afterpoint);
  152. //System.out.println(i+","+afterpoint + "," + lookahead);
  153. if (lookahead.indexOf('l') != -1) {
  154. // remove lemda union conf.lookahead;
  155. int index = lookahead.indexOf('l');
  156. lookahead = lookahead.substring(0, index)
  157. + lookahead.substring(index + 1);
  158. lookahead = union(lookahead, conf.lookahead);
  159.  
  160. }
  161. char[] array = lookahead.toCharArray();
  162. Arrays.sort(array);
  163. lookahead = String.valueOf(array);
  164. //System.out.println(lookahead);
  165.  
  166. // find productions start with the symbol
  167. for (int j = 0; j < grammar.size(); j++) {
  168. Production p = grammar.get(j);
  169. if (p.from == symbol) {
  170. Configuration c = new Configuration(p.from, p.to, 0);
  171. c.lookahead = lookahead;
  172. // test if the configuration is exist?
  173. if (!answerExist(answer, c)) {
  174. answer.push(c);
  175. }
  176. }
  177. }
  178. }
  179.  
  180. } else if (conf.point == conf.to.length() - 1) {
  181. // the point is at the end. eg.S->A*$ {l}
  182. char symbol = conf.to.charAt(conf.point);
  183. if (!isTerminal(symbol)) {
  184. // make lookahead
  185. String lookahead = conf.lookahead;
  186.  
  187. // find productions start with the symbol
  188. for (int j = 0; j < grammar.size(); j++) {
  189. Production p = grammar.get(j);
  190. if (p.from == symbol) {
  191. Configuration c = new Configuration(p.from, p.to, 0);
  192. c.lookahead = lookahead;
  193. // test if the configuration is exist?
  194. if (!answerExist(answer, c)) {
  195. answer.push(c);
  196. }
  197. }
  198. }
  199.  
  200. }
  201. }
  202.  
  203. }
  204. }
  205.  
  206. private static boolean answerExist(Stack<Configuration> answer,
  207. Configuration c) {
  208. for (int i = 0; i < answer.size(); i++) {
  209. Configuration ac = answer.get(i);
  210. if (ac.from == c.from && ac.to.equals(c.to) && ac.point == c.point
  211. && ac.lookahead.equals(c.lookahead)) {
  212. return true;
  213. }
  214. }
  215. return false;
  216. }
  217.  
  218. private static boolean isTerminal(char symbol) {
  219. return !Character.isUpperCase(symbol);
  220. }
  221.  
  222. private static void showConfiguration(Stack<Configuration> cfs) {
  223. for (int i = 0; i < cfs.size(); i++) {
  224. System.out.print(cfs.get(i).from + "->");
  225. // show String to with *
  226. for (int j = 0; j < cfs.get(i).to.length(); j++) {
  227. if (cfs.get(i).point == j) {
  228. System.out.print('*');
  229. }
  230. System.out.print(cfs.get(i).to.charAt(j));
  231. }
  232.  
  233. if (cfs.get(i).point == cfs.get(i).to.length()) {
  234. System.out.print('*');
  235. }
  236.  
  237. // show lookahead
  238. System.out.print(" {" + cfs.get(i).lookahead.charAt(0));
  239. for (int j = 1; j < cfs.get(i).lookahead.length(); j++) {
  240. System.out.print("," + cfs.get(i).lookahead.charAt(j));
  241. }
  242. System.out.print("}\n");
  243. }
  244. }
  245.  
  246. private static void showGrammar() {
  247. System.out.println("Grammar:");
  248. for (int i = 0; i < grammar.size(); i++) {
  249. System.out.println(grammar.get(i).from + "->" + grammar.get(i).to);
  250. }
  251. System.out.println();
  252. }
  253.  
  254. private static void input(String path) {
  255. try {
  256. input = new Scanner(new File(path));
  257. int p_size = input.nextInt();
  258. input.nextLine();
  259. for (int i = 0; i < p_size; i++) {
  260. String line = input.nextLine();
  261. Production p = new Production(line.charAt(0), line.substring(3));
  262. grammar.push(p);
  263. }
  264. int q_size = input.nextInt();
  265. input.nextLine();
  266. for (int i = 0; i < q_size; i++) {
  267. String line = input.nextLine();
  268. char from = line.charAt(0);
  269. int point = line.indexOf('*');
  270. line = line.substring(0, point) + line.substring(point + 1);
  271. point = point - 3;
  272.  
  273. int p_end = line.indexOf(' ');
  274. String to = line.substring(3, p_end);
  275.  
  276. Configuration c = new Configuration(from, to, point);
  277. int lk = p_end + 2;
  278. while (lk < line.length()) {
  279. c.unionLookahead(line.charAt(lk));
  280. lk = lk + 2;
  281. }
  282. c.sortLookahead();
  283. question.push(c);
  284. }
  285.  
  286. } catch (FileNotFoundException e) {
  287. e.printStackTrace();
  288. }
  289. }
  290.  
  291. }
  292.  
  293. class Production {
  294. char from;
  295. String to;
  296.  
  297. public Production(char from, String to) {
  298. this.from = from;
  299. this.to = to;
  300. }
  301. }
  302.  
  303. class Configuration extends Production {
  304. int point;
  305. String lookahead;
  306.  
  307. public Configuration(char from, String to, int point) {
  308. super(from, to);
  309. this.point = point;
  310. lookahead = "";
  311. }
  312.  
  313. public void sortLookahead() {
  314. char[] s = lookahead.toCharArray();
  315. Arrays.sort(s);
  316. lookahead = String.valueOf(s);
  317. }
  318.  
  319. public void unionLookahead(char c) {
  320. if (lookahead.indexOf(c) == -1) {
  321. lookahead = lookahead + c;
  322. sortLookahead();
  323. }
  324. }
  325.  
  326. public Configuration clone() {
  327. Configuration output = new Configuration(from, to, point);
  328. output.lookahead = lookahead;
  329. return output;
  330. }
  331. }
  332.  
  333. class FirstSet {
  334. Stack<Production> grammar;
  335. char[] symbol;
  336. String sets[];
  337.  
  338. public FirstSet(Stack<Production> grammar) {
  339. this.grammar = grammar;
  340.  
  341. // initial storage
  342. getAllSymbol();
  343. sets = new String[symbol.length];
  344. for (int i = 0; i < sets.length; i++) {
  345. sets[i] = "";
  346. }
  347.  
  348. // set terminal
  349. for (int i = 0; i < symbol.length; i++) {
  350. if (isTerminal(symbol[i])) {
  351. union(symbol[i], symbol[i]);
  352. }
  353. }
  354.  
  355. // reach stable state
  356. String last_sets[] = new String[sets.length];
  357. while (!stable(last_sets)) {
  358. last_sets = sets.clone();
  359. // scan all rules
  360. for (int i = 0; i < grammar.size(); i++) {
  361. Production p = grammar.get(i);
  362. scan(p.from, p.to, 0);
  363. }
  364. // showFirstSet();
  365. }
  366.  
  367. }
  368.  
  369. public String getFirstSet(String s) {
  370. String output = "";
  371. for (int i = 0; i < s.length(); i++) {
  372. char c = s.charAt(i);
  373. output = output + getFirstSet(c);
  374. int index = output.indexOf('l');
  375. if (index == -1) {
  376. return output;
  377. } else {
  378. // remove lemda
  379. output = output.substring(0, index)
  380. + output.substring(index + 1);
  381. }
  382. }
  383. output += 'l';
  384. char[] array = output.toCharArray();
  385. Arrays.sort(array);
  386. output = String.valueOf(array);
  387. return output;
  388. }
  389.  
  390. private void showGrammar() {
  391. for (int i = 0; i < grammar.size(); i++) {
  392. Production p = grammar.get(i);
  393. System.out.println(p.from + "->" + p.to);
  394. }
  395. System.out.println();
  396. }
  397.  
  398. public void showFirstSet() {
  399. for (int i = 0; i < symbol.length; i++) {
  400. System.out.println(symbol[i] + ":{" + sets[i] + "}");
  401. }
  402. System.out.println();
  403. }
  404.  
  405. private void scan(char from, String to, int i) {
  406. if (i < to.length()) {
  407. char cur_symbol = to.charAt(i);
  408. // System.out.println(cur_symbol);
  409. if (isTerminal(cur_symbol)) {
  410. union(from, cur_symbol);
  411. } else {// is non-terminal
  412. if (setHasLemda(cur_symbol)) { // lemda is in first(cur_symbol)
  413. unionWithoutLemda(from, cur_symbol);
  414. scan(from, to, i + 1);
  415. } else {
  416. unionString(from, getFirstSet(cur_symbol));
  417. }
  418. }
  419. } else {
  420. union(from, 'l');
  421. }
  422.  
  423. }
  424.  
  425. private void unionWithoutLemda(char from, char cur_symbol) {
  426. String set = getFirstSet(cur_symbol);
  427. int index = set.indexOf('l');
  428. set = set.substring(0, index) + set.substring(index + 1);
  429. unionString(from, set);
  430. }
  431.  
  432. private void unionString(char from, String set) {
  433. String fromSet = getFirstSet(from);
  434. for (int i = 0; i < set.length(); i++) {
  435. char c = set.charAt(i);
  436. if (fromSet.indexOf(c) == -1) {
  437. fromSet = fromSet + c;
  438. }
  439. }
  440. char[] array = fromSet.toCharArray();
  441. Arrays.sort(array);
  442. fromSet = String.valueOf(array);
  443. setFirstSet(from, fromSet);
  444. }
  445.  
  446. private boolean setHasLemda(char cur_symbol) {
  447. String set = getFirstSet(cur_symbol);
  448. int index = set.indexOf('l');
  449. return index != -1;
  450. }
  451.  
  452. private boolean stable(String[] last_sets) {
  453. for (int i = 0; i < sets.length; i++) {
  454. if (!sets[i].equals(last_sets[i])) {
  455. return false;
  456. }
  457. }
  458. return true;
  459. }
  460.  
  461. private void union(char c, char d) {
  462. String set = getFirstSet(c);
  463. // System.out.println(set);
  464. int index = set.indexOf(d);
  465. if (index == -1) {// add element d
  466. set = set + d;
  467. char[] array = set.toCharArray();
  468. Arrays.sort(array);
  469. set = String.valueOf(array);
  470. setFirstSet(c, set);
  471. }
  472. }
  473.  
  474. private void setFirstSet(char c, String set) {
  475. int index = Arrays.binarySearch(symbol, c);
  476. sets[index] = set;
  477. }
  478.  
  479. private boolean isTerminal(char c) {
  480. return !Character.isUpperCase(c);
  481. }
  482.  
  483. public String getFirstSet(char c) {
  484. int index = Arrays.binarySearch(symbol, c);
  485. // System.out.println(c+" "+index);
  486. return sets[index];
  487. }
  488.  
  489. private void getAllSymbol() {
  490. Stack<Character> symbols = new Stack<Character>();
  491. for (int i = 0; i < grammar.size(); i++) {
  492. Production p = grammar.get(i);
  493. if (symbols.search(p.from) == -1) {
  494. symbols.push(p.from);
  495. }
  496. for (int j = 0; j < p.to.length(); j++) {
  497. if (symbols.search(p.to.charAt(j)) == -1) {
  498. symbols.push(p.to.charAt(j));
  499. }
  500. }
  501. }
  502. symbol = new char[symbols.size()];
  503. for (int i = 0; i < symbol.length; i++) {
  504. symbol[i] = symbols.get(i);
  505. }
  506. Arrays.sort(symbol);
  507. }
  508. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement