📄 recursion.java
字号:
package mycompiler.yufa;
import java.util.*;
import java.io.*;
import java.awt.*;
/***************************************/
class SymbTable /* 在语义分析时用到 */
{
String idName;
AttributeIR attrIR=new AttributeIR();
SymbTable next=null;
}
class AttributeIR
{
TypeIR idtype=new TypeIR();
String kind;
Var var;
Proc proc;
}
class Var
{
String access;
int level;
int off;
boolean isParam;
}
class Proc
{
int level;
ParamTable param=new ParamTable();
int mOff;
int nOff;
int procEntry;
int codeEntry;
}
class ParamTable
{
SymbTable entry=new SymbTable();
ParamTable next=null;
}
class TypeIR
{
int size;
String kind;
Array array=null;
FieldChain body=null;
}
class Array
{
TypeIR indexTy=new TypeIR();
TypeIR elementTy=new TypeIR();
int low;
int up;
}
class FieldChain
{
String id;
int off;
TypeIR unitType=new TypeIR();
FieldChain next=null;
}
/*******************************************/
class TreeNode /* 语法树结点的定义 */
{
TreeNode child[]=new TreeNode[3];
TreeNode sibling=null;
int lineno;
String nodekind;
String kind;
int idnum;
String name[]=new String[10];
SymbTable table[]=new SymbTable[10];
Attr attr=new Attr();
}
class Attr
{
ArrayAttr arrayAttr=null; /* 只用到其中一个,用到时再分配内存 */
ProcAttr procAttr=null;
ExpAttr expAttr=null;
String type_name;
}
class ArrayAttr
{
int low;
int up;
String childtype;
}
class ProcAttr
{
String paramt;
}
class ExpAttr
{
String op;
int val;
String varkind;
String type;
}
/************************************************/
class TokenType /****Token序列的定义*******/
{
int lineshow;
String Lex;
String Sem;
}
/********************************************************************/
/* 类 名 Recursion */
/* 功 能 总程序的处理 */
/* 说 明 建立一个类,处理总程序 */
/********************************************************************/
public class Recursion
{
TokenType token=new TokenType();
int MAXTOKENLEN=10;
int lineno=0;
String temp_name;
StringTokenizer fenxi;
public boolean Error=false;
public String stree;
public String serror;
public TreeNode yufaTree;
public Recursion(String s)
{
yufaTree=Program(s);
printTree(yufaTree,0);
}
/********************************************************************/
/********************************************************************/
/********************************************************************/
/* 函数名 Program */
/* 功 能 总程序的处理函数 */
/* 产生式 < Program > ::= ProgramHead DeclarePart ProgramBody . */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/* 语法树的根节点的第一个子节点指向程序头部分ProgramHead, */
/* DeclaraPart为ProgramHead的兄弟节点,程序体部分ProgramBody */
/* 为DeclarePart的兄弟节点. */
/********************************************************************/
TreeNode Program(String ss)
{
fenxi=new StringTokenizer(ss,"\n");
ReadNextToken();
TreeNode root = newNode("ProcK");
TreeNode t=ProgramHead();
TreeNode q=DeclarePart();
TreeNode s=ProgramBody();
if (t!=null)
root.child[0] = t;
else
syntaxError("a program head is expected!");
if (q!=null)
root.child[1] = q;
if (s!=null)
root.child[2] = s;
else syntaxError("a program body is expected!");
match("DOT");
if (!(token.Lex.equals("ENDFILE")))
syntaxError("Code ends before file\n");
if (Error==true)
return null;
return root;
}
/**************************函数头部分********************************/
/********************************************************************/
/********************************************************************/
/* 函数名 ProgramHead */
/* 功 能 程序头的处理函数 */
/* 产生式 < ProgramHead > ::= PROGRAM ProgramName */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode ProgramHead()
{
TreeNode t = newNode("PheadK");
match("PROGRAM");
if (token.Lex.equals("ID"))
t.name[0]=token.Sem;
match("ID");
return t;
}
/**************************声明部分**********************************/
/********************************************************************/
/********************************************************************/
/* 函数名 DeclarePart */
/* 功 能 声明部分的处理 */
/* 产生式 < DeclarePart > ::= TypeDec VarDec ProcDec */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode DeclarePart()
{
/*类型*/
TreeNode typeP = newNode("TypeK");
TreeNode tp1 = TypeDec();
if (tp1!=null)
typeP.child[0] = tp1;
else
typeP=null;
/*变量*/
TreeNode varP = newNode("VarK");
TreeNode tp2 = VarDec();
if (tp2 != null)
varP.child[0] = tp2;
else
varP=null;
/*函数*/
TreeNode procP = ProcDec();
if (procP==null) {}
if (varP==null) { varP=procP; }
if (typeP==null) { typeP=varP; }
if (typeP!=varP)
typeP.sibling = varP;
if (varP!=procP)
varP.sibling = procP;
return typeP;
}
/**************************类型声明部分******************************/
/********************************************************************/
/* 函数名 TypeDec */
/* 功 能 类型声明部分的处理 */
/* 产生式 < TypeDec > ::= ε | TypeDeclaration */
/* 说 明 根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode TypeDec()
{
TreeNode t = null;
if (token.Lex.equals("TYPE"))
t = TypeDeclaration();
else if ((token.Lex.equals("VAR"))||(token.Lex.equals("PROCEDURE"))
||(token.Lex.equals("BEGIN"))) {}
else
ReadNextToken();
return t;
}
/********************************************************************/
/* 函数名 TypeDeclaration */
/* 功 能 类型声明部分的处理函数 */
/* 产生式 < TypeDeclaration > ::= TYPE TypeDecList */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode TypeDeclaration()
{
match("TYPE");
TreeNode t = TypeDecList();
if (t==null)
syntaxError("a type declaration is expected!");
return t;
}
/********************************************************************/
/* 函数名 TypeDecList */
/* 功 能 类型声明部分的处理函数 */
/* 产生式 < TypeDecList > ::= TypeId = TypeName ; TypeDecMore */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode TypeDecList()
{
TreeNode t = newNode("DecK");
if (t != null)
{
TypeId(t);
match("EQ");
TypeName(t);
match("SEMI");
TreeNode p = TypeDecMore();
if (p!=null)
t.sibling = p;
}
return t;
}
/********************************************************************/
/* 函数名 TypeDecMore */
/* 功 能 类型声明部分的处理函数 */
/* 产生式 < TypeDecMore > ::= ε | TypeDecList */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode TypeDecMore()
{
TreeNode t=null;
if (token.Lex.equals("ID"))
t = TypeDecList();
else if ((token.Lex.equals("VAR"))||(token.Lex.equals("PROCEDURE")) ||(token.Lex.equals("BEGIN"))) {}
else
ReadNextToken();
return t;
}
/********************************************************************/
/* 函数名 TypeId */
/* 功 能 类型声明部分的处理函数 */
/* 产生式 < TypeId > ::= id */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
void TypeId(TreeNode t)
{
if ((token.Lex.equals("ID"))&&(t!=null))
{
t.name[(t.idnum)]=token.Sem;
t.idnum = t.idnum+1;
}
match("ID");
}
/********************************************************************/
/* 函数名 TypeName */
/* 功 能 类型声明部分的处理 */
/* 产生式 < TypeName > ::= BaseType | StructureType | id */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
void TypeName(TreeNode t)
{
if (t !=null)
{
if ((token.Lex.equals("INTEGER"))||(token.Lex.equals("CHAR")))
BaseType(t);
else if ((token.Lex.equals("ARRAY"))||(token.Lex.equals("RECORD")))
StructureType(t);
else if (token.Lex.equals("ID"))
{
t.kind = "IdK";
t.attr.type_name = token.Sem;
match("ID");
}
else
ReadNextToken();
}
}
/********************************************************************/
/* 函数名 BaseType */
/* 功 能 类型声明部分的处理函数 */
/* 产生式 < BaseType > ::= INTEGER | CHAR */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
void BaseType(TreeNode t)
{
if (token.Lex.equals("INTEGER"))
{
match("INTEGER");
t.kind = "IntegerK";
}
else if (token.Lex.equals("CHAR"))
{
match("CHAR");
t.kind = "CharK";
}
else
ReadNextToken();
}
/********************************************************************/
/* 函数名 StructureType */
/* 功 能 类型声明部分的处理函数 */
/* 产生式 < StructureType > ::= ArrayType | RecType */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
void StructureType(TreeNode t)
{
if (token.Lex.equals("ARRAY"))
{
ArrayType(t);
}
else if (token.Lex.equals("RECORD"))
{
t.kind = "RecordK";
RecType(t);
}
else
ReadNextToken();
}
/********************************************************************/
/* 函数名 ArrayType */
/* 功 能 类型声明部分的处理函数 */
/* 产生式 < ArrayType > ::= ARRAY [low..top] OF BaseType */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
void ArrayType(TreeNode t)
{
t.attr.arrayAttr = new ArrayAttr();
match("ARRAY");
match("LMIDPAREN");
if (token.Lex.equals("INTC"))
t.attr.arrayAttr.low = Integer.parseInt(token.Sem);
match("INTC");
match("UNDERANGE");
if (token.Lex.equals("INTC"))
t.attr.arrayAttr.up = Integer.parseInt(token.Sem);
match("INTC");
match("RMIDPAREN");
match("OF");
BaseType(t);
t.attr.arrayAttr.childtype = t.kind;
t.kind = "ArrayK";
}
/********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -