📄 parsea.java
字号:
// /
// 函数名 varIdList /
// 功 能 变量声明部分的处理函数 /
// 产生式 < varIdList > ::= id varIdMore /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
void varIdList(TreeNode t) {
if (token.lex == LexType.ID) {
t.name[(t.idnum)] = token.sem;
match(LexType.ID);
t.idnum = (t.idnum ) + 1;
} else {
syntaxisError("a varid is expected here!");
ReadNextToken();
}
varIdMore(t);
}
// /
// 函数名 varIdMore /
// 功 能 变量声明部分的处理函数 /
// 产生式 < varIdMore > ::= ε | , varIdList /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
void varIdMore(TreeNode t) {
switch (token.lex) {
case SEMI:
break;
case COMMA:
match(LexType.COMMA);
varIdList(t);
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
}
// 过程声明部分 /
// /
// 函数名 procDec /
// 功 能 函数声明部分的处理函数 /
// 产生式 < procDec > ::= ε | procDeclaration /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode procDec() {
TreeNode t = null;
switch (token.lex) {
case BEGIN:
break;
case PROCEDURE:
t = procDeclaration();
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
return t;
}
// /
// 函数名 procDeclaration /
// 功 能 函数声明部分的处理函数 /
// 产生式 < procDeclaration > ::= PROCEDURE /
// ProcName(paramList); /
// procDecPart /
// procBody /
// procDec /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// 函数的根节点用于记录该函数的名字;第一个子节点指向参数节 /
// 点,第二个节点指向函数中的声明部分节点;第三个节点指向函 /
// 数体。
// /
private TreeNode procDeclaration() {
TreeNode t = newProcNode();
match(LexType.PROCEDURE);
if (t != null) {
t.lineno = lineno;
if (token.lex == LexType.ID) {
t.name[0] = token.sem;
(t.idnum)++;
match(LexType.ID);
}
match(LexType.LPAREN);
paramList(t);
match(LexType.RPAREN);
match(LexType.SEMI);
t.child[1] = procDecPart();
t.child[2] = procBody();
t.sibling = procDec();
}
return t;
}
// /
// 函数名 paramList /
// 功 能 函数声明中参数声明部分的处理函数 /
// 产生式 < paramList > ::= ε | paramDecList /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
void paramList(TreeNode t) {
TreeNode p = null;
switch (token.lex) {
case RPAREN:
break;
case INTEGER:
case CHAR:
case ARRAY:
case RECORD:
case ID:
case VAR:
p = paramDecList();
t.child[0] = p;
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
}
// /
// 函数名 paramDecList /
// 功 能 函数声明中参数声明部分的处理函数 /
// 产生式 < paramDecList > ::= param paramMore /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode paramDecList() {
TreeNode t = param();
TreeNode p = paramMore();
if (p != null) {
t.sibling = p;
}
return t;
}
// /
// 函数名 paramMore /
// 功 能 函数声明中参数声明部分的处理函数 /
// 产生式 < paramMore > ::= ε | ; paramDecList /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode paramMore() {
TreeNode t = null;
switch (token.lex) {
case RPAREN:
break;
case SEMI:
match(LexType.SEMI);
t = paramDecList();
if (t == null)
syntaxisError("a param declaration is request!");
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
return t;
}
// /
// 函数名 param /
// 功 能 函数声明中参数声明部分的处理函数 /
// 产生式 < param > ::= typeName formList | VAR typeName formList /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode param() {
TreeNode t = newDecNode();
if (t != null) {
t.lineno = lineno;
switch (token.lex) {
case INTEGER:
case CHAR:
case ARRAY:
case RECORD:
case ID:
t.attr.procAttr.paramt = ParamType.valparamType;
typeName(t);
formList(t);
break;
case VAR:
match(LexType.VAR);
t.attr.procAttr.paramt = ParamType.varparamType;
typeName(t);
formList(t);
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
}
return t;
}
// /
// 函数名 formList /
// 功 能 函数声明中参数声明部分的处理函数 /
// 产生式 < formList > ::= id fidMore /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
void formList(TreeNode t) {
if (token.lex == LexType.ID) {
t.name[(t.idnum)] = token.sem;
t.idnum = (t.idnum) + 1;
match(LexType.ID);
}
fidMore(t);
}
// /
// 函数名 fidMore /
// 功 能 函数声明中参数声明部分的处理函数 /
// 产生式 < fidMore > ::= ε | , formList /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
void fidMore(TreeNode t) {
switch (token.lex) {
case SEMI:
case RPAREN:
break;
case COMMA:
match(LexType.COMMA);
formList(t);
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
}
// /
// 函数名 procDecPart /
// 功 能 函数中的声明部分的处理函数 /
// 产生式 < procDecPart > ::= declarePart /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode procDecPart() {
TreeNode t = declarePart();
return t;
}
// /
// 函数名 procBody /
// 功 能 函数体部分的处理函数 /
// 产生式 < procBody > ::= programBody /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode procBody() {
TreeNode t = programBody();
if (t == null)
syntaxisError("a program body is requested!");
return t;
}
// /
// 函数名 programBody /
// 功 能 程序体部分的处理函数 /
// 产生式 < programBody > ::= BEGIN stmList END /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode programBody() {
TreeNode t = newStmlNode();
match(LexType.BEGIN);
if (t != null) {
t.lineno = 0;
t.child[0] = stmList();
}
match(LexType.END);
return t;
}
// /
// 函数名 stmList /
// 功 能 语句部分的处理函数 /
// 产生式 < stmList > ::= stm stmMore /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode stmList() {
TreeNode t = stm();
TreeNode p = stmMore();
if (t != null)
if (p != null)
t.sibling = p;
return t;
}
// /
// 函数名 stmMore /
// 功 能 语句部分的处理函数 /
// 产生式 < stmMore > ::= ε | ; stmList /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode stmMore() {
TreeNode t = null;
switch (token.lex) {
case ELSE:
case FI:
case END:
case ENDWH:
break;
case SEMI:
match(LexType.SEMI);
t = stmList();
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
return t;
}
// /
// 函数名 stm /
// 功 能 语句部分的处理函数 /
// 产生式 < stm > ::= conditionalStm {IF} /
// | loopStm {WHILE} /
// | inputStm {READ} /
// | outputStm {WRITE} /
// | returnStm {RETURN} /
// | id assCall {id} /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode stm() {
TreeNode t = null;
switch (token.lex) {
case IF:
t = conditionalStm();
break;
case WHILE:
t = loopStm();
break;
case READ:
t = inputStm();
break;
case WRITE:
t = outputStm();
break;
case RETURN:
t = returnStm();
break;
case ID:
temp_name = token.sem;
match(LexType.ID);
t = assCall();
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
return t;
}
// /
// 函数名 assCall /
// 功 能 语句部分的处理函数 /
// 产生式 < assCall > ::= assignmentRest {:=,LMIDPAREN,DOT} /
// | callStmRest {(} /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode assCall() {
TreeNode t = null;
switch (token.lex) {
case ASSIGN:
case LMIDPAREN:
case DOT:
t = assignmentRest();
break;
case LPAREN:
t = callStmRest();
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
return t;
}
// /
// 函数名 assignmentRest /
// 功 能 赋值语句部分的处理函数 /
// 产生式 < assignmentRest > ::= variMore : = exp /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode assignmentRest() {
TreeNode t = newStmtNode(StmtKind.AssignK);
// 赋值语句节点的第一个儿子节点记录赋值语句的左侧变量名,
// 第二个儿子结点记录赋值语句的右侧表达式 /
if (t != null) {
t.lineno = lineno;
// 处理第一个儿子结点,为变量表达式类型节点 /
TreeNode child1 = newExpNode(ExpKind.VariK);
if (child1 != null) {
child1.lineno = lineno;
child1.name[0] = temp_name;
(child1.idnum)++;
variMore(child1);
t.child[0] = child1;
}
// 赋值号匹配 /
match(LexType.ASSIGN);
// 处理第二个儿子节点 /
t.child[1] = exp();
}
return t;
}
// /
// 函数名 conditionalStm /
// 功 能 条件语句部分的处理函数 /
// 产生式 < conditionalStm > ::= IF exp THEN stmList ELSE stmList FI /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode conditionalStm() {
TreeNode t = newStmtNode(StmtKind.IfK);
match(LexType.IF);
if (t != null) {
t.lineno = lineno;
t.child[0] = exp();
}
match(LexType.THEN);
if (t != null)
t.child[1] = stmList();
if (token.lex == LexType.ELSE) {
match(LexType.ELSE);
if (t != null)
t.child[2] = stmList();
}
match(LexType.FI);
return t;
}
// /
// 函数名 loopStm /
// 功 能 循环语句部分的处理函数 /
// 产生式 < loopStm > ::= WHILE exp DO stmList ENDWH /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode loopStm() {
TreeNode t = newStmtNode(StmtKind.WhileK);
match(LexType.WHILE);
if (t != null) {
t.lineno = lineno;
t.child[0] = exp();
match(LexType.DO);
t.child[1] = stmList();
match(LexType.ENDWH);
}
return t;
}
// /
// 函数名 inputStm /
// 功 能 输入语句部分的处理函数 /
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -