package grammar.recursive;
import grammar.recursive.Type.DecKind;
import grammar.recursive.Type.ExpKind;
import grammar.recursive.Type.ExpType;
import grammar.recursive.Type.NodeKind;
import grammar.recursive.Type.ParamType;
import grammar.recursive.Type.StmtKind;
import grammar.recursive.Type.VarKind;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import scaner.TokenType;
import scaner.TokenType.LexType;
public class Recursive implements RecursiveInterface {
private int lineno = 0; // 为全局变量lineno
private int indentno = 0; // 打印空格数量
private boolean Error = false;// 语法分析是否有错误
private int MAXCHILDREN = 3;
private TokenType token = null;
private int line0; //
private int fp_num;
private String temp_name;
private List<TokenType> tlist = null;
private List<String> grammarlist = null;
private String reservestr = "";
private String tempStr = "";
private TreeNode root = null;
private static int TABNUM = 6;
public Recursive() {
fp_num = 0; /* 输出时记录token个数的变量 */
Error = false;
}
public boolean getError() {
return Error;
}
public void setError(boolean error) {
Error = error;
}
public List<String> getGrammarlist() {
return grammarlist;
}
public List<TokenType> getTlist() {
return tlist;
}
public void setTlist(List<TokenType> tlist) {
this.tlist = tlist;
}
public String getReservestr() {
return reservestr;
}
public void setReservestr(String reservestr) {
this.reservestr = reservestr;
}
/* 功 能 创建语法树根节点函数 */
public TreeNode newRootNode() {
TreeNode t = new TreeNode();
t.setNodekind(NodeKind.ProK);
t.setLineno(lineno);// ######
return t;
}
public TreeNode newPheadNode() {/* 功 能 创建程序头类型语法树节点函数 */
TreeNode t = new TreeNode();
t.setNodekind(NodeKind.PheadK);
/* 相同类型的变量个数 */
t.setIdnum(0);
t.setLineno(lineno);// ######
return t;
}
/* 功 能 创建声明语法树节点函数,没有指明具体的节点声明 */
/* 类型,在语法树的第二层 */
public TreeNode newDecANode(NodeKind kind) {
TreeNode t = new TreeNode();
t.setNodekind(kind);
t.setLineno(lineno);
return t;
}
/* 功 能 类型标志语法树节点创建函数 */
public TreeNode newTypeNode() {
TreeNode t = new TreeNode();
t.setNodekind(NodeKind.TypeK);
t.setLineno(lineno);
return t;
}
/* 功 能 变量声明标志语法树节点创建函数 */
public TreeNode newVarNode() {
TreeNode t = new TreeNode();
t.setNodekind(NodeKind.VarK);
t.setLineno(lineno);
return t;
}
/* 功 能 创建声明类型语法树节点函数 */
public TreeNode newDecNode() {
TreeNode t = new TreeNode();
t.setNodekind(NodeKind.DecK);
t.setLineno(lineno);
return t;
}
/* 功 能 创建函数类型语法树节点函数 */
public TreeNode newProcNode() {
TreeNode t = new TreeNode();
t.setNodekind(NodeKind.ProcDecK);
t.setLineno(lineno);
return t;
}
/* 功 能 创建语句标志类型语法树节点函数 */
public TreeNode newStmlNode() {
TreeNode t = new TreeNode();
t.setNodekind(NodeKind.StmLK);
t.setLineno(lineno);
return t;
}
/* 功 能 创建语句类型语法树节点函数 */
public TreeNode newStmtNode(StmtKind kind) {
TreeNode t = new TreeNode();
t.setNodekind(NodeKind.StmLK);
/* 指定新语法树节点t成员:语句类型kind.stmt为函数给定参数kind */
t.kind.stmt = kind;
t.setLineno(lineno);
return t;
}
/* 功 能 表达式类型语法树节点创建函数 */
public TreeNode newExpNode(ExpKind kind) {
TreeNode t = new TreeNode();
t.setNodekind(NodeKind.ExpK);
/* 指定新语法树节点t成员:语句类型kind.stmt为函数给定参数kind */
t.kind.exp = kind;
t.setLineno(lineno);
t.attr.expattr.varkind = VarKind.IdV;
/* 指定新语法树节点t成员: 类型检查类型type为Void */
t.attr.expattr.type = ExpType.Void;
return t;
}
/* 功 能 字符串复制函数 */
public String copyString(String s) {
String t = null;
t = s;
return t;
}
/* 功 能 将文件tokenlist中的信息作为返回值 */
public void ReadNextToken() {// ///////##########################
/*
* if (fp_num >= tlist.size()) { //
* System.out.println("read next token error !!!!!!!"); token = null;
* Error = true; try { throw new Exception(); } catch (Exception e) { }
* // System.exit(0); return; }
*/
if (fp_num < tlist.size()) {
token = tlist.get(fp_num);
fp_num++;
} else {
Error = true;
}
}
/* 说 明 在输出文件中打印个数为参数tabnum的空格 */
public void printTab(int tabnum) {
for (int i = 0; i < tabnum; i++) {
tempStr += " ";
}
}
/* 说 明 该函数打印指定数量空格,用于表示子树缩进 */
public void printSpaces() {
/* 按给定缩进量indentno打印空格进行缩进 * 其中缩进量indentno总能保持非负 */
for (int i = 0; i < indentno; i++) {
tempStr += " ";
}
}
public void printTree(TreeNode tree) {
int i;
/* 增量缩进宏,每次进入语法树节点都进行增量缩进 */
indentno += TABNUM;
/* 函数参数给定语法树节点指针tree非null(空) */
while (tree != null) {
tempStr = "";
/* 打印行号 */
if (tree.getLineno() == 0)
printTab(9);
else
switch ((int) (tree.getLineno() / 10)) {
case 0:
tempStr += "line: " + tree.getLineno();
printTab(3);
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
tempStr += "line: " + tree.getLineno();
printTab(2);
break;
default:
tempStr += "line: " + tree.getLineno();
printTab(1);
}
/* 调用函数printSpaces,打印相应的空格,进行缩进 */
printSpaces();
switch (tree.getNodekind()) {
case ProK:
tempStr += "ProK" + " ";
break;
case PheadK: {
tempStr += "PheadK" + " ";
tempStr += tree.name[0] + " ";
}
break;
case DecK: {
tempStr += "DecK" + " ";
if (tree.attr.procattr.paramt == ParamType.varparamType)
tempStr += "var param:" + " ";
if (tree.attr.procattr.paramt == ParamType.valparamType)
tempStr += "value param:" + " ";
switch (tree.kind.dec) {
case ArrayK: {
tempStr += "ArrayK" + " ";
tempStr += tree.attr.arrayattr.up + " ";
tempStr += tree.attr.arrayattr.low + " ";
if (tree.attr.arrayattr.childtype == DecKind.CharK)
tempStr += "Chark" + " ";
else if (tree.attr.arrayattr.childtype == DecKind.IntegerK)
tempStr += "IntegerK" + " ";
}
break;
case CharK:
tempStr += "CharK" + " ";
break;
case IntegerK:
tempStr += "IntegerK" + " ";
break;
case RecordK:
tempStr += "RecordK" + " ";
break;
case IdK:
tempStr += "IdK" + " ";
tempStr += tree.attr.type_name + " ";
break;
default:
tempStr += "error1!";
Error = true;
}
if (tree.getIdnum() != 0)
for (i = 0; i <= (tree.getIdnum()); i++) {
tempStr += tree.name[i] + " ";
}
else {
tempStr += "wrong!no var!\n";
Error = true;
}
}
break;
case TypeK:
tempStr += "TypeK" + " ";
break;
case VarK:
tempStr += "VarK" + " ";
if (tree.table[0] != null)
tempStr += "VarK" + tree.table[0].attrIR.more.varattr.off
+ " " + tree.table[0].attrIR.more.varattr.level;
break;
case ProcDecK:
tempStr += "ProcDecK" + " ";
tempStr += tree.name[0] + " ";
if (tree.table[0] != null)
tempStr += tree.name[0]
+ tree.table[0].attrIR.more.procattr.mOff + " "
+ tree.table[0].attrIR.more.procattr.nOff + " "
+ tree.table[0].attrIR.more.procattr.level;
break;
case StmLK:
tempStr += "StmLk" + " ";
break;
case StmtK: {
tempStr += "StmtK" + " ";
switch (tree.kind.stmt) {