📄 recursion.java
字号:
/* 产生式 < ProcBody > ::= ProgramBody */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode ProcBody()
{
TreeNode t = ProgramBody();
if (t==null)
syntaxError("a program body is requested!");
return t;
}
/****************************函数体部分******************************/
/********************************************************************/
/********************************************************************/
/* 函数名 ProgramBody */
/* 功 能 程序体部分的处理 */
/* 产生式 < ProgramBody > ::= BEGIN StmList END */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode ProgramBody()
{
TreeNode t = newNode("StmLK");
match("BEGIN");
t.child[0] = StmList();
match("END");
return t;
}
/********************************************************************/
/* 函数名 StmList */
/* 功 能 语句部分的处理函数 */
/* 产生式 < StmList > ::= Stm StmMore */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode StmList()
{
TreeNode t = Stm();
TreeNode p = StmMore();
if (t!=null)
{
if (p!=null)
t.sibling = p;
}
return t;
}
/********************************************************************/
/* 函数名 StmMore */
/* 功 能 语句部分的处理函数 */
/* 产生式 < StmMore > ::= ε | ; StmList */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode StmMore()
{
TreeNode t = null;
if ((token.Lex.equals("ELSE"))||(token.Lex.equals("FI"))|| (token.Lex.equals("END"))||(token.Lex.equals("ENDWH"))) {}
else if (token.Lex.equals("SEMI"))
{
match("SEMI");
t = StmList();
}
else
ReadNextToken();
return t;
}
/********************************************************************/
/* 函数名 Stm */
/* 功 能 语句部分的处理函数 */
/* 产生式 < Stm > ::= ConditionalStm {IF} */
/* | LoopStm {WHILE} */
/* | InputStm {READ} */
/* | OutputStm {WRITE} */
/* | ReturnStm {RETURN} */
/* | id AssCall {id} */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode Stm()
{
TreeNode t = null;
if (token.Lex.equals("IF"))
t = ConditionalStm();
else if (token.Lex.equals("WHILE"))
t = LoopStm();
else if (token.Lex.equals("READ"))
t = InputStm();
else if (token.Lex.equals("WRITE"))
t = OutputStm();
else if (token.Lex.equals("RETURN"))
t = ReturnStm();
else if (token.Lex.equals("ID"))
{
temp_name = token.Sem;
match("ID");
t = AssCall();
}
else
ReadNextToken();
return t;
}
/********************************************************************/
/* 函数名 AssCall */
/* 功 能 语句部分的处理函数 */
/* 产生式 < AssCall > ::= AssignmentRest {:=,LMIDPAREN,DOT} */
/* | CallStmRest {(} */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode AssCall()
{
TreeNode t = null;
if ((token.Lex.equals("ASSIGN"))||(token.Lex.equals("LMIDPAREN"))|| (token.Lex.equals("DOT")))
t = AssignmentRest();
else if (token.Lex.equals("LPAREN"))
t = CallStmRest();
else
ReadNextToken();
return t;
}
/********************************************************************/
/* 函数名 AssignmentRest */
/* 功 能 赋值语句部分的处理函数 */
/* 产生式 < AssignmentRest > ::= VariMore : = Exp */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode AssignmentRest()
{
TreeNode t = newStmtNode("AssignK");
/* 赋值语句节点的第一个儿子节点记录赋值语句的左侧变量名,
/* 第二个儿子结点记录赋值语句的右侧表达式*/
/*处理第一个儿子结点,为变量表达式类型节点*/
TreeNode c = newExpNode("VariK");
c.name[0] = temp_name;
c.idnum = c.idnum+1;
VariMore(c);
t.child[0] = c;
match("ASSIGN");
/*处理第二个儿子节点*/
t.child[1] = Exp();
return t;
}
/********************************************************************/
/* 函数名 ConditionalStm */
/* 功 能 条件语句部分的处理函数 */
/* 产生式 <ConditionalStm>::=IF RelExp THEN StmList ELSE StmList FI */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode ConditionalStm()
{
TreeNode t = newStmtNode("IfK");
match("IF");
t.child[0] = Exp();
match("THEN");
if (t!=null)
t.child[1] = StmList();
if(token.Lex.equals("ELSE"))
{
match("ELSE");
t.child[2] = StmList();
}
match("FI");
return t;
}
/********************************************************************/
/* 函数名 LoopStm */
/* 功 能 循环语句部分的处理函数 */
/* 产生式 < LoopStm > ::= WHILE RelExp DO StmList ENDWH */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode LoopStm()
{
TreeNode t = newStmtNode("WhileK");
match("WHILE");
t.child[0] = Exp();
match("DO");
t.child[1] = StmList();
match("ENDWH");
return t;
}
/********************************************************************/
/* 函数名 InputStm */
/* 功 能 输入语句部分的处理函数 */
/* 产生式 < InputStm > ::= READ(id) */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode InputStm()
{
TreeNode t = newStmtNode("ReadK");
match("READ");
match("LPAREN");
if (token.Lex.equals("ID"))
{
t.name[0] = token.Sem;
t.idnum = t.idnum+1;
}
match("ID");
match("RPAREN");
return t;
}
/********************************************************************/
/* 函数名 OutputStm */
/* 功 能 输出语句部分的处理函数 */
/* 产生式 < OutputStm > ::= WRITE(Exp) */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode OutputStm()
{
TreeNode t = newStmtNode("WriteK");
match("WRITE");
match("LPAREN");
t.child[0] = Exp();
match("RPAREN");
return t;
}
/********************************************************************/
/* 函数名 ReturnStm */
/* 功 能 返回语句部分的处理函数 */
/* 产生式 < ReturnStm > ::= RETURN(Exp) */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode ReturnStm()
{
TreeNode t = newStmtNode("ReturnK");
match("RETURN");
return t;
}
/********************************************************************/
/* 函数名 CallStmRest */
/* 功 能 函数调用语句部分的处理函数 */
/* 产生式 < CallStmRest > ::= (ActParamList) */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode CallStmRest()
{
TreeNode t=newStmtNode("CallK");
match("LPAREN");
/*函数调用时,其子节点指向实参*/
/*函数名的结点也用表达式类型结点*/
TreeNode c = newExpNode("VariK");
c.name[0] = temp_name;
c.idnum = c.idnum+1;
t.child[0] = c;
t.child[1] = ActParamList();
match("RPAREN");
return t;
}
/********************************************************************/
/* 函数名 ActParamList */
/* 功 能 函数调用实参部分的处理函数 */
/* 产生式 < ActParamList > ::= ε | Exp ActParamMore */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode ActParamList()
{
TreeNode t = null;
if (token.Lex.equals("RPAREN")) {}
else if ((token.Lex.equals("ID"))||(token.Lex.equals("INTC")))
{
t = Exp();
if (t!=null)
t.sibling = ActParamMore();
}
else
ReadNextToken();
return t;
}
/********************************************************************/
/* 函数名 ActParamMore */
/* 功 能 函数调用实参部分的处理函数 */
/* 产生式 < ActParamMore > ::= ε | , ActParamList */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode ActParamMore()
{
TreeNode t = null;
if (token.Lex.equals("RPAREN")) {}
else if (token.Lex.equals("COMMA"))
{
match("COMMA");
t = ActParamList();
}
else
ReadNextToken();
return t;
}
/*************************表达式部分********************************/
/*******************************************************************/
/* 函数名 Exp */
/* 功 能 表达式处理函数 */
/* 产生式 Exp ::= simple_exp | 关系运算符 simple_exp */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/*******************************************************************/
TreeNode Exp()
{
TreeNode t = simple_exp();
/* 当前单词token为逻辑运算单词LT或者EQ */
if ((token.Lex.equals("LT"))||(token.Lex.equals("EQ")))
{
TreeNode p = newExpNode("OpK");
/* 将当前单词token(为EQ或者LT)赋给语法树节点p的运算符成员attr.op*/
p.child[0] = t;
p.attr.expAttr.op = token.Lex;
t = p;
/* 当前单词token与指定逻辑运算符单词(为EQ或者LT)匹配 */
match(token.Lex);
/* 语法树节点t非空,调用简单表达式处理函数simple_exp()
函数返回语法树节点给t的第二子节点成员child[1] */
if (t!=null)
t.child[1] = simple_exp();
}
return t;
}
/*******************************************************************/
/* 函数名 simple_exp */
/* 功 能 表达式处理 */
/* 产生式 simple_exp ::= term | 加法运算符 term */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/*******************************************************************/
TreeNode simple_exp()
{
TreeNode t = term();
/* 当前单词token为加法运算符单词PLUS或MINUS */
while ((token.Lex.equals("PLUS"))||(token.Lex.equals("MINUS")))
{
TreeNode p = newExpNode("OpK");
p.child[0] = t;
p.attr.expAttr.op = token.Lex;
t = p;
match(token.Lex);
/* 调用元处理函数term(),函数返回语法树节点给t的第二子节点成员child[1] */
t.child[1] = term();
}
return t;
}
/********************************************************************/
/* 函数名 term */
/* 功 能 项处理函数 */
/* 产生式 < 项 > ::= factor | 乘法运算符 factor */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/********************************************************************/
TreeNode term()
{
TreeNode t = factor();
/* 当前单词token为乘法运算符单词TIMES或OVER */
while ((token.Lex.equals("TIMES"))||(token.Lex.equals("OVER")))
{
TreeNode p = newExpNode("OpK");
p.child[0] = t;
p.attr.expAttr.op = token.Lex;
t = p;
match(token.Lex);
p.child[1] = factor();
}
return t;
}
/*********************************************************************/
/* 函数名 factor */
/* 功 能 因子处理函数 */
/* 产生式 factor ::= INTC | Variable | ( Exp ) */
/* 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 */
/*********************************************************************/
TreeNode factor()
{
TreeNode t = null;
if (token.Lex.equals("INTC"))
{
t = newExpNode("ConstK");
/* 将当前单词名tokenString转换为整数赋给t的数值成员attr.val */
t.attr.expAttr.val = Integer.parseInt(token.Sem);
match("INTC");
}
else if (token.Lex.equals("ID"))
t = Variable();
else if (token.Lex.equals("LPAREN"))
{
match("LPAREN");
t = Exp();
match("RPAREN");
}
else
ReadNextToken();
return t;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -