compilers - lab4

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package javaapplication1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Class {
private final static String[] namesofgroups = {"KEY_W", "IDT"};
//private final static String COMMENT = "^(/\\*)([^\\*]*\\*[^/]?)+/";
private final static String COMMENTBEG = "^(/\\*)";
private final static String COMMENTEND = "(\\*/[\\s\\t])|(\\*/$)";
private final static String KEY_W = "^(for|if|m1)[\\s|\\t|$]";
private final static String IDT = "^((([a-zA-Z][0-9])+[a-zA-Z]?)|(([0-9][a-zA-Z])+[0-9]?))([\\s\\t]|$)";
private final static String REG_EX = "(" + COMMENTBEG + ")|(" + KEY_W + ")|(" + IDT + ")";
private final static int REGEXP_POS_COMMENT = 1;
private final static int REGEXP_POS_KEYWORD = 3;
private final static int REGEXP_POS_IDENT = 5;
private final static int KEYWORD_INDEX = 0;
private final static int IDENET_INDEX = 1;
static void print(boolean comment, int indexoftype, int line, int pos, String m) {
if (comment) {
System.out.println("COMMMENT "
+ "(" + Integer.toString(line)
+ "," + Integer.toString(pos)
+ ")"
);
} else {
System.out.println(namesofgroups[indexoftype] + " "
+ "(" + Integer.toString(line)
+ "," + Integer.toString(pos)
+ ") " + m //matcher.group(indexofmatcher)
);
}
}
public static void main(String[] args) {
int lineofcomment = -1;
int posofcomment = -1;
BufferedReader mReader = null;
try {
mReader = new BufferedReader(
new FileReader(
new File(
"C:\\Users\\Марина\\Documents\\NetBeansProjects\\JavaApplication1\\test\\f")));
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
try {
Pattern pattern = Pattern.compile(REG_EX);
Matcher matcher;
Pattern pattern1 = Pattern.compile(COMMENTEND);
Matcher matcher1;
int numofline = 1;
while (mReader.ready()) {
String line = mReader.readLine();
int str = line.length();
while (line.length() != 0) {
if (posofcomment != -1) {
matcher1 = pattern1.matcher(line);
if (matcher1.find()) {
print(true, 1, lineofcomment, posofcomment, null);
line = line.substring(matcher1.end());
lineofcomment = -1;
posofcomment = -1;
} else {
line = "";
}
} else {
matcher = pattern.matcher(line);
if (matcher.find()) {
int indexofmatcher = 0;
int indexoftype = 0;
if (matcher.group(REGEXP_POS_COMMENT) != null) {
lineofcomment = numofline;
posofcomment = str - line.length() + 1;
indexofmatcher = REGEXP_POS_COMMENT;
} else if (matcher.group(REGEXP_POS_IDENT) != null) {
if (!matcher.group(REGEXP_POS_IDENT).equals("m1")) {
indexofmatcher = REGEXP_POS_IDENT;
indexoftype = IDENET_INDEX;
} else {
indexofmatcher = REGEXP_POS_IDENT;
indexoftype = KEYWORD_INDEX;
}
} else if (matcher.group(REGEXP_POS_KEYWORD) != null) {
indexofmatcher = REGEXP_POS_KEYWORD;
indexoftype = KEYWORD_INDEX;
}
if (indexofmatcher != REGEXP_POS_COMMENT) {
print(false, indexoftype, numofline, str - line.length() + 1, matcher.group(indexofmatcher));
}
line = line.substring(matcher.group(indexofmatcher).length());
} else if (line.length() != 0 && line.charAt(0) == ' ') {
line = line.substring(1);
} else {
System.out.println("syntax error\t("
+ Integer.toString(numofline) + ", "
+ Integer.toString(str - line.length() + 1) + ")"
);
while (line.length() != 0 && line.charAt(0) != ' ') {
line = line.substring(1);
}
}
}
}
numofline++;
}
if (posofcomment != -1) {
System.out.println("syntax error\t("
+ Integer.toString(lineofcomment) + ", "
+ Integer.toString(posofcomment) + ")"
);
}
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}