📄 yacc.java.svn-base
字号:
package cn.edu.buaa.scse.liyi.compile.tools;
import java.util.LinkedList;
import java.util.Vector;
import cn.edu.buaa.scse.liyi.compile.types.*;
/**
*
* @author liyi
*/
public class Yacc
{
//四元式操作符
public static final int ADD=1;
public static final int SUB=2;
public static final int MUL=3;
public static final int DIV=4;
public static final int MOV=5;
public static final int STAMP=6;
public static final int J=7;
public static final int JFALSE=8;
public static final int CONDITION=9;
public static final int CMP=10;
public static final int PUSHPARA=11;
public static final int CALL=12;
public static final int PRINTF=13;
public static final int SCANF=14;
public static final int RETURN=15;
public static final int BREAK=16;
private LinkedList<Output> lexList; //词法输出表
private Output lop=null; //当前词法输出元素
private int lxpos=0; //词法输出表当前位置
private Output flag; //标志寄存
private int layer=0; //当前层数
private Vector<Constant> global_const=null; //全局常量表
private Vector<String> global_var=null; //全局变量表
private LinkedList<Function> funcList=null; //函数表
private Function curfunc=null; //当前函数
private Quaternion quater=null; //当前四元式
private int funclab=0; //当前函数标签个数
private int ti=1; //四元式中间结果符号角标
private String register=null; //暂存器
private boolean error=false; //是否错误
private boolean checkret=true; //是否return标志
public LinkedList<String> errorList=null; //错误信息列表
/**
* 语法分析器构造方法
* @param lexList
*/
public Yacc(LinkedList<Output> lexList)
{
this.lexList=lexList;
this.global_const=new Vector<Constant>();
this.global_var=new Vector<String>();
this.funcList=new LinkedList<Function>();
this.errorList=new LinkedList<String>();
}
/********************************************************************************************/
public void setGlobal_const(Vector<Constant> global_const)
{
this.global_const=global_const;
}
public Vector<Constant> getGlobal_const()
{
return global_const;
}
public void setGlobal_var(Vector<String> global_var)
{
this.global_var=global_var;
}
public Vector<String> getGlobal_var()
{
return global_var;
}
public void setError(boolean error)
{
this.error=error;
}
public boolean isError()
{
return error;
}
public void setFuncList(LinkedList<Function> funcList)
{
this.funcList=funcList;
}
public LinkedList<Function> getFuncList()
{
return funcList;
}
/********************************************************************************************/
/**
* 取词法输出链中的下一个元素的方法
*/
private boolean next()
{
if(++lxpos<lexList.size())
{
lop=new Output(lexList.get(lxpos));
return true;
}
else
{
lop=new Output();
//error=true;
//errorList.add(Error.report(Error.ERR_OVERFLOW,0,null));
return false;
}
}
/**
* 构造新四元式并把它加入到四元式链中去
* @param operator
* @param operend1
* @param operend2
* @param result
*/
private void addQuater(int operator,String operend1,String operend2,String result)
{
quater=new Quaternion(operator,operend1,operend2,result);
curfunc.getQuaterList().add(quater);
}
/**
* 检查函数是否同名
* @param funcname
* @param line
* @return
*/
private boolean checkFuncName(String funcname,int line)
{
for(Function func:this.funcList)
{
if(funcname!=null&&funcname.equals(func.getName()))
{
error=true;
errorList.add(Error.report(Error.ERR_SAMEFUNC,line,funcname));
return false;
}
}
return true;
}
/**
* 子函数调用时检查此函数是否被定义过
* @param funcname
* @param line
* @return
*/
private boolean checkFuncDefined(String funcname,int line)
{
for(Function func:this.funcList)
{
if(funcname!=null&&funcname.equals(func.getName()))
{
return true;
}
}
error=true;
errorList.add(Error.report(Error.ERR_NOTDEFINE,line,funcname));
return false;
}
/**
* 检查全局常量及变量是否同名
* @param global_name
* @param line
* @return
*/
private boolean checkGlobal(String global_name,int line)
{
for(Constant con:this.global_const)
{
if(global_name!=null&&global_name.equals(con.getName()))
{
error=true;
errorList.add(Error.report(Error.ERR_SAMEGLOBAL,line,global_name));
return false;
}
}
for(String var:this.global_var)
{
if(var.equals(global_name))
{
error=true;
errorList.add(Error.report(Error.ERR_SAMEGLOBAL,line,global_name));
return false;
}
}
return true;
}
/**
* 检查局部常量及变量是否同名
* @param local_name
* @param line
* @return
*/
private boolean checkLocal(String local_name,int line)
{
for(Constant con:curfunc.getConst())
{
if(local_name.equals(con.getName()))
{
error=true;
errorList.add(Error.report(Error.ERR_SAMELOCAL,line,local_name));
return false;
}
}
for(String para:curfunc.getPara())
{
if(para.equals(local_name))
{
error=true;
errorList.add(Error.report(Error.ERR_SAMELOCAL,line,local_name));
return false;
}
}
for(String var:curfunc.getVar())
{
if(var.equals(local_name))
{
error=true;
errorList.add(Error.report(Error.ERR_SAMELOCAL,line,local_name));
return false;
}
}
return true;
}
/********************************************************************************************/
/**
* <常量定义>
* @return
*/
private boolean parseConstDef()
{
if(lop.getType()==Lex.IDSY)
{
String globalName=new String();
String localName=new String();
if(layer==0)
{
if(!checkGlobal(lop.getWord(),lop.getLine()))
{
return false;
}
globalName=lop.getWord(); //把全局常量名存入全局常量表
}
else
{
//局部常量处理
if(!checkLocal(lop.getWord(),lop.getLine()))
{
return false;
}
localName=lop.getWord();
}
if(next()&&lop.getType()==Lex.EQUAL)
{
if(next()&&lop.getType()==Lex.INTEGER)
{
if(layer==0)
{
global_const.add(new Constant(globalName,Integer.parseInt(lop.getWord()))); //把全局常量值存入全局常量表
}
else
{
//局部常量处理
curfunc.getConst().add(new Constant(localName,Integer.parseInt(lop.getWord())));
}
next();
return true;
}
}
}
error=true;
errorList.add(Error.report(Error.ERR_NOTBE,lop.getLine(),"常量定义"));
return false;
}
/**
* <常量说明部分>
* @return
*/
private boolean parseConst()
{
if(next()&&parseConstDef())
{ //const 后面不是<常量定义>,错误
while(lop.getType()!=Lex.SEMICOLON)
{
if(lop.getType()==Lex.COMMA)
{
if(!next()||!parseConstDef())
{ //逗号后面不是<常量定义>,错误
return false;
}
}
else
{
error=true;
errorList.add(Error.report(Error.ERR_SEMILACK,lop.getLine(),null));
return false;
}
}
//遇到分号前一直没有错误,可以接受
next();
return true;
}
return false;
}
/**
* <变量说明部分>
* @return
*/
private boolean parseVar()
{
if(layer==0)
{ //第零层为全局变量
if(!checkGlobal(flag.getWord(),flag.getLine()))
{
return false;
}
global_var.add(flag.getWord());
while(lop.getType()!=Lex.SEMICOLON)
{
if(!next()||lop.getType()!=Lex.IDSY)
{
error=true;
errorList.add(Error.report(Error.ERR_NOTBE,lop.getLine(),"变量说明"));
return false;
}
if(!checkGlobal(lop.getWord(),lop.getLine()))
{
return false;
}
global_var.add(lop.getWord());
register=lop.getWord();
if(next()&&lop.getType()==Lex.EQUAL)
{
if(next()&&lop.getType()==Lex.INTEGER)
{
addQuater(MOV,lop.getWord(),null,register);
next();
}
else
{
error=true;
errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
return false;
}
}
if(lop.getType()!=Lex.COMMA&&lop.getType()!=Lex.SEMICOLON)
{
error=true;
errorList.add(Error.report(Error.ERR_SEMILACK,lop.getLine(),null));
return false;
}
}
next();
return true;
}
//第一层为局部变量
while(lop.getType()!=Lex.SEMICOLON)
{
if(!next()||lop.getType()!=Lex.IDSY)
{
error=true;
errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
return false;
}
//局部变量处理
if(!checkLocal(lop.getWord(),lop.getLine()))
{
return false;
}
curfunc.getVar().add(lop.getWord());
register=lop.getWord();
if(next()&&lop.getType()==Lex.EQUAL)
{
if(next()&&lop.getType()==Lex.INTEGER)
{
addQuater(MOV,lop.getWord(),null,register);
next();
}
else
{
error=true;
errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
return false;
}
}
if(lop.getType()!=Lex.COMMA&&lop.getType()!=Lex.SEMICOLON)
{
error=true;
errorList.add(Error.report(Error.ERR_SEMILACK,lop.getLine(),null));
return false;
}
}
next();
return true;
}
/**
* <主函数>
* @param type
* @return
*/
private boolean parseMain(int type)
{
curfunc=new Function(type,"main");
layer++;
funcList.add(curfunc);
if(lop.getType()!=Lex.Lpar)
{
error=true;
errorList.add(Error.report(Error.ERR_PARALACK,lop.getLine(),"main"));
return false;
}
if(!next()||lop.getType()!=Lex.Rpar)
{
error=true;
errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
return false;
}
if(next()&&parseComplex())
{
//四元式成功生成后填map
int i=1;
for(String var:curfunc.getVar())
{
curfunc.getVmap().put(var,i*4);
i++;
}
for(Quaternion quater:curfunc.getQuaterList())
{
if(quater.getResult()!=null&&quater.getResult().startsWith("@"))
{
curfunc.getVmap().put(quater.getResult(),i*4);
i++;
}
}
funclab=0;
return true;
}
return false;
}
/**
* <子函数>
* @return
*/
private boolean parseFunc()
{
if(flag.getType()==Lex.IDSY)
{
if(!checkFuncName(flag.getWord(),flag.getLine()))
{
return false;
}
curfunc=new Function(Function.R_INT,flag.getWord());
layer++;
}
else
{
if(!checkFuncName(lop.getWord(),lop.getLine()))
{
return false;
}
curfunc=new Function(Function.R_VOID,lop.getWord());
layer++;
next();
}
funcList.add(curfunc);
if(!parsePara())
{
return false;
}
//有参数时填map
int i=0;
for(String para:curfunc.getPara())
{
curfunc.getPmap().put(para,(i+2)*4);
i++;
}
if(lop.getType()!=Lex.Lbra)
{
error=true;
errorList.add(Error.report(Error.ERR_NOTBE,lop.getLine(),"子函数定义"));
return false;
}
if(!parseComplex())
{
return false;
}
//四元式成功生成后填map
i=1;
for(String v:curfunc.getVar())
{
curfunc.getVmap().put(v,i*4);
i++;
}
for(Quaternion quater:curfunc.getQuaterList())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -