Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static public LinkedList<Node> language = new LinkedList();
- static public LinkedList<TableNode> lexemaTable = new LinkedList();
- static public int getLexemaCode(String lexema, int lexemaClass) {
- for (TableNode tmp : lexemaTable) {
- if (!tmp.getLexemaText().equals(lexema) || (tmp.getLexemaCode() & -16777216) != lexemaClass) continue; //16777216 = 2^24
- return tmp.getLexemaCode();
- }
- return 0;
- }
- static public String getLexemaText(int code) {
- for (TableNode tmp : lexemaTable) {
- if (tmp.getLexemaCode() != code) continue;
- return tmp.getLexemaText();
- }
- return null;
- }
- static public void task1(String path) {
- BufferedReader s;
- char[] lexema = new char[256];
- int[] rule = new int[256];
- try {
- s = new BufferedReader(new FileReader(path));
- }
- catch (FileNotFoundException ee) {
- System.out.print("Файл не найден");
- return;
- }
- Arrays.fill(rule , 0);
- Arrays.fill(lexema , (char)0);
- int pos = 0;
- int type = 0;
- int rule_pos = 0;
- String line = null;
- int read_pos = 0;
- int len = 0;
- try {
- Node node;
- int newLexemaCode;
- TableNode tnode;
- while (s.ready()) {
- if (line == null || read_pos >= len) {
- line = s.readLine();
- if (line.length() > 0 && line.charAt(0) == 65279) {
- line = line.substring(1);
- }
- len = line.length();
- }
- start :
- for (read_pos = 0; read_pos < len; ++read_pos) {
- char cur = line.charAt(read_pos);
- String str;
- boolean flag;
- switch (type) {
- case 0: {
- String ck = " \t\n\n\b";
- if(ck.indexOf(cur) != -1)continue start;
- if (read_pos == 0 && rule_pos > 0 && (cur == '!' || cur == '#')) {
- node = new Node(rule, rule_pos);
- language.add(node);
- if (cur == '!') {
- rule_pos = 1;
- continue start;
- }
- rule_pos = 0;
- }
- lexema[0] = cur;
- pos = 1;
- if (cur == '#') {
- type = 1;
- continue start;
- }
- if (cur == '\\') {
- --pos;
- type = 3;
- continue start;
- }
- type = 2;
- continue start;
- }
- case 1: {
- lexema[pos] = cur;
- ++pos;
- if (cur != '#' && read_pos != 0) continue start;
- str = new String(lexema, 0, pos);
- tnode = new TableNode(str, 0x80000000);
- flag = true;
- for (TableNode tmp : lexemaTable) {
- if (!tnode.equals(tmp)) continue;
- flag = false;
- break;
- }
- if (flag) {
- lexemaTable.add(tnode);
- }
- newLexemaCode = getLexemaCode(str, 0x80000000);
- rule[rule_pos++] = newLexemaCode;
- pos = 0;
- type = 0;
- continue start;
- }
- case 2: {
- if (cur == '\\') {
- --pos;
- type = 3;
- continue start;
- }
- if (cur == ' ' || read_pos == 0 || cur == '#' || cur == '\r' || cur == '\t') {
- str = new String(lexema, 0, pos);
- tnode = new TableNode(str, 0x10000000);
- flag = true;
- for (TableNode tmp : lexemaTable) {
- if (!tnode.equals(tmp)) continue;
- flag = false;
- break;
- }
- if (flag) {
- lexemaTable.add(tnode);
- }
- newLexemaCode = getLexemaCode(str, 0x10000000);
- rule[rule_pos] = newLexemaCode;
- ++rule_pos;
- pos = 0;
- type = 0;
- --read_pos;
- continue start;
- }
- lexema[pos++] = cur;
- continue start;
- }
- case 3: {
- lexema[pos++] = cur;
- type = 2;
- }
- }
- }
- }
- if (pos != 0) {
- int code = (type == 1 ? 0x80000000 : 0x10000000); // Маска коду не термінала 0x80000000, маска коду термінала 0x10000000
- String str = new String(lexema, 0, pos);
- tnode = new TableNode(str, code);
- boolean flag = true;
- for (TableNode tmp : lexemaTable) {
- if (!tnode.equals(tmp)) continue;
- flag = false;
- break;
- }
- if (flag) {
- lexemaTable.add(tnode);
- }
- newLexemaCode = getLexemaCode(str, code);
- rule[rule_pos] = newLexemaCode;
- ++rule_pos;
- }
- if (rule_pos > 0) {
- node = new Node(rule, rule_pos);
- language.add(node);
- }
- }
- catch (IOException e) {
- System.out.println(e.toString());
- }
- }
- public static void printlang(){
- int count = 0;
- for (Node tmp : language) {
- int[] roole = tmp.getRoole();
- System.out.print("" + ++count + " ");
- for (int ii = 0; ii < roole.length; ++ii) {
- if (ii == 1) {
- System.out.print(" -> ");
- }
- System.out.print(getLexemaText(roole[ii]) + " ");
- if (roole.length != 1) continue;
- System.out.print(" -> ");
- }
- System.out.println("");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement