📄 analyseframe.java
字号:
sb.append(c);
outTextArea.append("( " + sb.toString()
+ "\t," + "SEPARATOR" + "\t)\n");
c = buffer[++i];
break;
case '[':
sb.append(c);
outTextArea.append("( " + sb.toString()
+ "\t," + "SEPARATOR" + "\t)\n");
c = buffer[++i];
break;
case ']':
sb.append(c);
outTextArea.append("( " + sb.toString()
+ "\t," + "SEPARATOR" + "\t)\n");
c = buffer[++i];
break;
case '\'':
sb.append(c);
c = buffer[++i];
while (c != '\'') {
sb.append(c);
c = buffer[++i];
}
sb.append(c);
outTextArea.append("( " + sb.toString()
+ "\t," + "STRING" + "\t)\n");
c = buffer[++i];
break;
case '\"':
sb.append(c);
c = buffer[++i];
while (c != '\"') {
sb.append(c);
c = buffer[++i];
}
sb.append(c);
outTextArea.append("( " + sb.toString()
+ "\t," + "STRING" + "\t)\n");
c = buffer[++i];
break;
case '.':
sb.append(c);
outTextArea.append("( " + sb.toString()
+ "\t," + "SEPARATOR" + "\t)\n");
c = buffer[++i];
break;
case ':':
sb.append(c);
c = buffer[++i];
if (c == '=') {
sb.append(c);
RQ[j++] = sb.toString();
CT[m++] = sb.toString();
outTextArea.append("( " + sb.toString()
+ "\t," + "OPERATOR" + "\t)\n");
c = buffer[++i];
} else {
outTextArea.append("( " + sb.toString()
+ "\t," + "ERROR" + "\t)\n");
}
break;
case '#':
sb.append(c);
outTextArea.append("( " + sb.toString()
+ "\t," + "SEPARATOR" + "\t)\n");
c = buffer[++i];
break;
case ' ':
case '\n':
case '\r':
case '\t':
c = buffer[++i];
break;
default:
sb.append(c);
outTextArea.append("( " + sb.toString()
+ "\t," + "ERROR" + "\t)\n");
c = buffer[++i];
break;
}// end switch
}// end else of no /
}// end else of no number
}// end else of no key or id
}// end while
} catch (ArrayIndexOutOfBoundsException e) {
outTextArea.append("( " + sb.toString() + "\t,"
+ checkWord(sb.toString()) + "\t)\n");
}
/**********************************************/
/*do the copy from two arrays to store the tokens
* in order to do the syntax analysis*/
l = j;
n = m;
tong = new String[l];
bucket = new String[n];
for (int y = 0; y < l; y++) {
tong[y] = RQ[y];
}
for (int x = 0; x < n; x++) {
bucket[x] = CT[x];
}
outTextArea.append("##Lexical analysis is over##\n\n\n");
/*********************************************/
/*output the result into a file,if you haven't created one before*/
if (objFile == null) {
sb = new StringBuffer(sourFile.getName());
int positon = sb.indexOf(".");
sb.delete(positon, sb.length());
sb.append(".mycomplier");
objFile = new File(sourFile.getParentFile(), sb.toString());
try {
outTextArea.write(new FileWriter(objFile));
} catch (IOException e) {
}
}
}//file is nulll
// outTextArea.append("##Lexical analysis is over##\n##Ayntax analysis is on##\n");
// analysisSyn();
}
/* syntax analysis*/
private void analysisSyn() {
outTextArea.append("##Ayntax analysis is on##\n");
k = 0;
line = 1;
while (k < l-1) { //look through all the tokens
current_word = tong[k++];
if (current_word.equals(";")) {
line++;
}
//check declaration if current word is int
if (current_word.equals("int"))
check_declaration();
//check if condition if current word is if
if (current_word.equalsIgnoreCase("if")) {
check_if_condition();
}
//check if condition if current word is while
if (current_word.equals("while")) {
check_while_condition();
}
}
outTextArea.append("##Ayntax analysis is over##\n");
}
/* code generation */
private void generate_code() {
/* the format-if-then-else */
outTextArea.append("\n\n### code generation####\n");
int bz = 0;
String pd = "";
int rs = 0;
int b = 0;
int dd = 0;// the first of dd
int bzindex = 0;
int flag[] = new int[n];
int biaozhi[] = new int[n];
for (int i = 0; i < n; i++) {
flag[i] = 0;
biaozhi[i] = 0;
}
/*make a record of the position of key word*/
for (int i = 0; i < n; i++) {
if (bucket[i].equals("if") || bucket[i].equalsIgnoreCase("then")
|| bucket[i].equals("else") || bucket[i].equals("while")
|| bucket[i].equals("do") || bucket[i].equals(";"))
flag[i] = 1;
}
/*make a record of the first key word(if and whiel) in the array*/
for (int i = 0; i < n; i++) {
if (bucket[i].equals("if") || bucket[i].equals("while")) {
dd = i;
break;
}
}
/*make a record the precise position of the key word*/
for (int i = dd; i < n; i++) {
if (flag[i] == 1)
biaozhi[b++] = i;
}
/*code generation*/
while (bzindex < n-1) {
pd = "";
/*first considering the codition of if-then-else*/
if (bucket[biaozhi[bzindex]].equals("if")) {
for (int i = biaozhi[bzindex++] + 1; i < biaozhi[bzindex]; i++) {
pd = pd + " " + bucket[i];
}
outTextArea.append("L" + (++bz) + " : if" + pd + " goto L"
+ (++bz) + "\n goto L" + (++bz) + "\n");
pd = "t" + (++rs);
int s = biaozhi[bzindex] + 1;
for (int i = biaozhi[bzindex++] + 2; i < biaozhi[bzindex]; i++) {
pd = pd + " " + bucket[i];
}
outTextArea.append("L" + (bz - 1) + " : " + pd + "\n"
+ " " + bucket[s] + ":= t" + (rs)
+ "\n goto L" + (++bz) + "\n");
pd = "t" + (++rs);
int g = biaozhi[bzindex] + 1;
for (int i = biaozhi[bzindex++] + 2; i < biaozhi[bzindex]; i++) {
pd = pd + " " + bucket[i];
}
bz--;
outTextArea.append("L" + (bz) + " : " + pd + "\n"
+ " " + bucket[g] + ":= t" + rs + "\n");
}
/*and then the situation of the while control*/
if (bucket[biaozhi[bzindex]].equals("while")) {
pd = "";
for (int i = biaozhi[bzindex++] + 1; i < biaozhi[bzindex]; i++) {
pd = pd + " " + bucket[i];
}
outTextArea.append("L" + (++bz) + " : if" + pd + " goto L"
+ (++bz) + "\n goto L" + (++bz) + "\n");
pd = "t" + (++rs);
int h = biaozhi[bzindex] + 1;
for (int i = biaozhi[bzindex++] + 2; i < biaozhi[bzindex]; i++) {
pd = pd + " " + bucket[i];
}
outTextArea.append("L" + (bz - 1) + " : " + pd + "\n"
+ " " + bucket[h] + ":= t" + rs + " \n goto L"
+ (bz - 2) + "\n" + "L" + (bz) + ":\n");
}
bzindex++;
}
outTextArea.append("### code generation over####\n");
}
/* type checking */
private void check_type() {
outTextArea.append("\n\n## type checking now##\n");
p = 0;
hang = 1;
while (p < n) {
if (bucket[p].equals(";"))
hang++;
if (tong[p].equals("ID")) {
if (!check_definite()) {
outTextArea.append(hang + " : " + bucket[p]
+ ": has not been defined before\n");
}
}
p++;
}
outTextArea.append("## type checking ends##\n");
}
/*check the current ID is definited before or not*/
private boolean check_definite() {
for (int i = 0; i <= p; i++) {
if (bucket[i].equals(bucket[p]) && bucket[i - 1].equals("int"))
return true;
}
return false;
}
/*checking the while condition*/
private void check_while_condition() {
current_word = tong[k];
if (current_word.equals("ID")) {
k++;
check_condition();
current_word = tong[++k];
if (current_word.equals("do")) {
current_word = tong[++k];
if (current_word.equals("ID")) {
k++;
check_assignment();
}
outTextArea.append(line + " :while-control--> while C do A\n");
}
}
}
/*checking the if condition*/
private void check_if_condition() {
current_word = tong[k];
if (current_word.equals("ID")) {
k++;
check_condition();
current_word = tong[++k];
if (current_word.equals("then")) {
current_word = tong[++k];
if (current_word.equals("ID")) {
k++;
check_assignment();
}
current_word = tong[k + 1];
if (current_word.equalsIgnoreCase("else")) { //modul of if-then-else
k += 2;
current_word = tong[k];
if (current_word.equals("ID")) {
k++;
check_assignment();
}
outTextArea.append(line + " :if_control-->if then else\n");
} else //modul of if-then
outTextArea.append(line + " :if_control-->if then\n");
}
}
}
//checking assignment
private void check_assignment() {
current_word = tong[k];
if (current_word.equals(":=")) {
current_word = tong[++k];
if (current_word.equals("ID")) {
current_word = tong[k + 1];
if (current_word.equals("OP")) {//modul of a:=c
k++;
check_exp();//modul like c:=c+(d*f)
} else
outTextArea.append(line + " :<Assignment--id op id\n");
}
}
}
/*checking the expression, just considering the modul like c:=a+(r*g)*/
private void check_exp() {
current_word = tong[++k];
if (current_word.equals("(")) {
current_word = tong[++k];
if (current_word.equals("ID")) {
if (tong[++k].equals("OP"))
outTextArea.append(line
+ " :<Expression- id op (id op id)\n");
if (!tong[++k].equals("ID") || !tong[++k].equals(")"))
outTextArea.append("##" + line + " : Expression Wrong##\n");
}
}
}
//checking condition > or <=
private void check_condition() {
current_word = tong[k];
if (current_word.equals(">") || current_word.equals("<=")) {
current_word = tong[++k];
if (current_word.equals("ID"))
outTextArea.append(line + " :<Condition>-- > or <=\n");
} else
outTextArea.append("##" + line + ": Condition Wrong##\n");
}
//checking the declaration like " int a;"
private void check_declaration() {
current_word = tong[k];
if (current_word.equals("ID")) {
outTextArea.append(line + " :<Declaration-- type id\n");
}
}
/**
* This method initializes checkWord
*
* @return int
*/
private String checkWord(String word) {
String key[] = { "auto", "break", "case", "char", "const", "continue",
"default", "do", "else", "enum", "extern", "for", "goto", "if",
"register", "return", "signed", "sizeof", "static", "struct",
"switch", "typedef", "union", "unsigned", "void", "volatile",
"while", "then", "else" };
String type[] = { "int", "long", "double", "float", "short" };
/*String pretreatment[] = { "define", "endif", "elif", "error", "line",
"include", "ifdef", "ifndef", "pragma", "undef", "if", "else" };*/
for (int i = 0; i < 29; i++) {
if (word.equals(key[i]))
return "KEY";
}
/*for (int i = 0; i < 12; i++) {
if (word.equals(pretreatment[i]))
return "PRETREATMENT";
}*/
for (int i = 0; i < 5; i++) {
if (word.equals(type[i]))
return "TYPE";
}
return "ID";
}
/*
* DocumentListener to handle the changes of inTextArea when the current
* content of inTextArea, it will show '*' on the title to inform the user
* of the changing
*
* @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent)
*/
public void changedUpdate(DocumentEvent e) {
}
public void insertUpdate(DocumentEvent e) {
if (!edited) {
edited = true;
setTitle(getTitle() + " *");
}
}
public void removeUpdate(DocumentEvent e) {
if (!edited) {
edited = true;
setTitle(getTitle() + " *");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -