/*
* Analyse.java
*
* Created on 2007年10月10日, 上午10:02
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package cmm;
import java.util.LinkedList;
/**
*
* @author lx
*/
public class Analyse {
private LinkedList tokens;
private LinkedList ID;
private LinkedList sen;
private LinkedList table;
private LinkedList code;
private Cmm cmm;
private int lineNum;
String[] word = new String[2];
private boolean flag;
VarType type;
private boolean flag_val=false;
// 设置一个变量adr1存放下一条目标代码的地址
//int adr1;
/** Creates a new instance of Analyse */
public Analyse() {
}
public Analyse(LinkedList a,LinkedList b,Cmm c) {
this();
lineNum = 1;
cmm=c;
tokens = a;
ID = b;
flag = false;
sen = new LinkedList();
table = new LinkedList();
code = new LinkedList();
nextToken();
}
public String[] nextToken(){
if(!tokens.isEmpty ()){
word[1] = (String)ID.poll();
word[0] = (String)(tokens.poll().toString());
if(word[1].equals("NL")){
lineNum++;
return nextToken();
}else
return word;
}
return null;
}
// 程序退出时判断是否遇到#正常退出
public String getTo(){
String temp;
if(!(word[0].equals(null)||word[1].equals(null))){
temp = word[0];
return temp;
}
return null;
}
// 打印语法树
public void printTree(){
String temp;
String space = "";
int length = 0;
while(!sen.isEmpty()){
temp = sen.poll().toString();
if(temp.equals("$")){
space = space + " ";
}else if(temp.equals("%")){
length = space.length();
space = space.substring(0,length -10);
}else if(temp.startsWith("第")){
//cmm.output1.append(space);
cmm.output1.setText("");
cmm.output1.append(temp + "\n\n");
sen.clear();
}
else{
if(!temp.equals(";")){
cmm.output1.append(space);
cmm.output1.append(temp + "\n\n");
}else{
temp= "";
cmm.output1.append(temp +"\n");
}
}
}
}
// S`-->SS1
public void an(){
if(word[0].equalsIgnoreCase("int")||word[0].equalsIgnoreCase("real")||word[1].equals("ID")||word[0].equalsIgnoreCase("read")||word[0].equalsIgnoreCase("write")||word.equals("{")||word[0].equalsIgnoreCase("if")||word[0].equalsIgnoreCase("while")||word[0].equals(";")){
statement();
st_1();
}else if(!(word[0].equals("}")||word[0].equals("#"))){
sen.offer("第"+ lineNum + "行:非法的开始符号!");
}
}
// S-->I|E|R|W|C|B|H|null
public void statement(){
if(word[0].equalsIgnoreCase("int")||word[0].equalsIgnoreCase("real")){
sen.offer("声明语句:");
sen.offer("$");
identification();
sen.offer("%");
}else if(word[1].equals("ID")){
flag_val = true;
sen.offer("赋值语句:");
sen.offer("$");
evaluation();
sen.offer("%");
flag_val = false;
}else if(word[0].equalsIgnoreCase("read")){
sen.offer("read语句:");
sen.offer("$");
readIn();
sen.offer("%");
}else if(word[0].equalsIgnoreCase("write")){
sen.offer("write语句:");
sen.offer("$");
writeOut();
sen.offer("%");
}else if(word[0].equals("{")){
composition();
}else if(word[0].equalsIgnoreCase("if")){
sen.offer("if语句:");
sen.offer("$");
if_statement();
}else if(word[0].equalsIgnoreCase("while")){
sen.offer("while语句:");
sen.offer("$");
while_statement();
}else if(!(word[0].equals(";")||word[0].equalsIgnoreCase("else")||word[0].equals("}")||word[0].equals("#"))){
sen.offer("第"+lineNum +"行:语句调用出错!");
}
}
// S1 --> ;SS1
public void st_1(){
if(word[0].equals(";")){
sen.offer(word[0]);
nextToken();
statement();
st_1();
}else if(!(word[0].equals("#")||word[0].equals("}"))){
sen.offer("第" + lineNum + "行: 错误的语句串 !");
}
}
//I-->int|real D <声明语句>--> int|real <定义>
public void identification(){
if(word[0].equalsIgnoreCase("int")){
//名字表添加一项
enter(null,VarType.INT,0,false);
sen.offer("数据类型:");
sen.offer("$");
sen.offer(word[0]);
sen.offer("%");
sen.offer("声明:");
sen.offer("$");
nextToken();
definition();
sen.offer("%");
}else if(word[0].equalsIgnoreCase("real")){
//名字表添加一项
enter(null,VarType.REAL,0.0,false);
sen.offer("数据类型:");
sen.offer("$");
sen.offer(word[0]);
sen.offer("%");
sen.offer("声明:");
sen.offer("$");
nextToken();
definition();
sen.offer("%");
}
else{
sen.offer("第"+ lineNum + "行:请输入正确的数据类型!");
}
}
// D-->iD2
public void definition(){
if(word[1].equals("ID")){
Variable temp;
temp = (Variable)table.getLast();
temp.setName(word[0]);
sen.offer("标识符:");
sen.offer("$");
sen.offer(word[0]);
sen.offer("%");
nextToken();
def_2();
}else{
sen.offer("第"+ lineNum + "行:请输入正确的标识符!");
}
}
//D2 --> [x] | =D1 | null
public void def_2(){
if(word[0].equals("[")){
Variable temp;
temp = (Variable)table.getLast();
temp.setIsArray(true);
sen.offer(word[0]);
nextToken();
if(word[1].equals("INT")){
if(((Variable)table.getLast()).getType()==VarType.INT){
for(int i=1;i<Integer.parseInt(word[0]);i++){
//为每个数组元素在名字表中添加一项
Variable temp1;
temp1 = (Variable)table.getLast();
enter(temp1.getName(),temp1.getType(),0,true);
}
}else{
for(int i=1;i<Integer.parseInt(word[0]);i++){
//为每个数组元素在名字表中添加一项
Variable temp1;
temp1 = (Variable)table.getLast();
enter(temp1.getName(),temp1.getType(),0.0,tr