📄 parsea.java
字号:
if (t != null && token.lex == LexType.ID) {
t.lineno = 0;
t.name[0] = token.sem;
}
match(LexType.ID);
return t;
}
// 声明部分 /
// /
// 函数名 declarePart /
// 功 能 声明部分的处理函数 /
// 产生式 < declarePart > ::= typeDec varDec procDec /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode declarePart() {
// 类型 /
TreeNode typeP = newDecANode(NodeKind.TypeK);
TreeNode pp = typeP;
if (typeP != null) {
typeP.lineno = 0;
TreeNode tp1 = typeDec();
if (tp1 != null)
typeP.child[0] = tp1;
else {
typeP = null;
}
}
// 变量 /
TreeNode varP = newDecANode(NodeKind.VarK);
if (varP != null) {
varP.lineno = 0;
TreeNode tp2 = varDec();
if (tp2 != null)
varP.child[0] = tp2;
else {
varP = null;
}
}
// 函数 /
TreeNode s = procDec();
if (s == null) {
}
if (varP == null) {
varP = s;
}
if (typeP == null) {
pp = typeP = varP;
}
if (typeP != varP) {
typeP.sibling = varP;
typeP = varP;
}
if (varP != s) {
varP.sibling = s;
varP = s;
}
return pp;
}
// 类型声明部分 /
// /
// 函数名 typeDec /
// 功 能 类型声明部分的处理函数 /
// 产生式 < typeDec > ::= ε | TypeDeclaration /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode typeDec() {
TreeNode t = null;
switch (token.lex) {
case TYPE:
t = typeDeclaration();
break;
case VAR:
case PROCEDURE:
case BEGIN:
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
return t;
}
// /
// 函数名 TypeDeclaration /
// 功 能 类型声明部分的处理函数 /
// 产生式 < TypeDeclaration > ::= TYPE TypeDecList /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode typeDeclaration() {
match(LexType.TYPE);
TreeNode t = typeDecList();
if (t == null) {
syntaxisError("a type declaration is expected!");
}
return t;
}
// /
// 函数名 TypeDecList /
// 功 能 类型声明部分的处理函数 /
// 产生式 < TypeDecList > ::= typeId = typeName ; typeDecMore /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode typeDecList() {
TreeNode t = newDecNode();
if (t != null) {
t.lineno = lineno;
typeId(t);
match(LexType.EQ);
typeName(t);
match(LexType.SEMI);
TreeNode p = typeDecMore();
if (p != null)
t.sibling = p;
}
return t;
}
// /
// 函数名 typeDecMore /
// 功 能 类型声明部分的处理函数 /
// 产生式 < typeDecMore > ::= ε | TypeDecList /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode typeDecMore() {
TreeNode t = null;
switch (token.lex) {
case VAR:
case PROCEDURE:
case BEGIN:
break;
case ID:
t = typeDecList();
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
return t;
}
// /
// 函数名 typeId /
// 功 能 类型声明部分的处理函数 /
// 产生式 < typeId > ::= id /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
void typeId(TreeNode t) {
int tnum = (t.idnum);
if ((token.lex == LexType.ID && t != null)) {
t.name[tnum] = token.sem;
tnum = tnum + 1;
}
t.idnum = tnum;
match(LexType.ID);
}
// /
// 函数名 typeName /
// 功 能 类型声明部分的处理函数 /
// 产生式 < typeName > ::= baseType | structureType | id /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
void typeName(TreeNode t) {
if (t != null)
switch (token.lex) {
case INTEGER:
case CHAR:
baseType(t);
break;
case ARRAY:
case RECORD:
structureType(t);
break;
case ID:
t.kind.dec = DecKind.IdK;
t.attr.type_name = token.sem;
match(LexType.ID);
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
}
// /
// 函数名 baseType /
// 功 能 类型声明部分的处理函数 /
// 产生式 < baseType > ::= INTEGER | CHAR /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
void baseType(TreeNode t) {
switch (token.lex) {
case INTEGER:
match(LexType.INTEGER);
t.kind.dec = DecKind.IntegerK;
break;
case CHAR:
match(LexType.CHAR);
t.kind.dec = DecKind.CharK;
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
}
// /
// 函数名 structureType /
// 功 能 类型声明部分的处理函数 /
// 产生式 < structureType > ::= arrayType | recType /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
void structureType(TreeNode t) {
switch (token.lex) {
case ARRAY:
arrayType(t);
break;
case RECORD:
t.kind.dec = DecKind.RecordK;
recType(t);
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
}
// /
// 函数名 arrayType /
// 功 能 类型声明部分的处理函数 /
// 产生式 < arrayType > ::= ARRAY [low..top] OF baseType /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
void arrayType(TreeNode t) {
match(LexType.ARRAY);
match(LexType.LMIDPAREN);
if (token.lex == LexType.INTC) {
t.attr.arrayAttr.low = Integer.parseInt(token.sem);
}
match(LexType.INTC);
match(LexType.UNDERANGE);
if (token.lex == LexType.INTC) {
t.attr.arrayAttr.up = Integer.parseInt(token.sem);
}
match(LexType.INTC);
match(LexType.RMIDPAREN);
match(LexType.OF);
baseType(t);
t.attr.arrayAttr.childtype = t.kind.dec;
t.kind.dec = DecKind.ArrayK;
}
// /
// 函数名 recType /
// 功 能 类型声明部分的处理函数 /
// 产生式 < recType > ::= RECORD fieldDecList END /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
void recType(TreeNode t) {
TreeNode p = null;
match(LexType.RECORD);
p = fieldDecList();
if (p != null)
t.child[0] = p;
else
syntaxisError("a record body is requested!");
match(LexType.END);
}
// /
// 函数名 fieldDecList /
// 功 能 类型声明部分的处理函数 /
// 产生式 < fieldDecList > ::= baseType idList ; fieldDecMore /
// | arrayType idList; fieldDecMore /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode fieldDecList() {
TreeNode t = newDecNode();
TreeNode p = null;
if (t != null) {
t.lineno = lineno;
switch (token.lex) {
case INTEGER:
case CHAR:
baseType(t);
idList(t);
match(LexType.SEMI);
p = fieldDecMore();
break;
case ARRAY:
arrayType(t);
idList(t);
match(LexType.SEMI);
p = fieldDecMore();
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
t.sibling = p;
}
return t;
}
// /
// 函数名 fieldDecMore /
// 功 能 类型声明部分的处理函数 /
// 产生式 < fieldDecMore > ::= ε | fieldDecList /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode fieldDecMore() {
TreeNode t = null;
switch (token.lex) {
case END:
break;
case INTEGER:
case CHAR:
case ARRAY:
t = fieldDecList();
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
return t;
}
// /
// 函数名 idList /
// 功 能 类型声明部分的处理函数 /
// 产生式 < idList > ::= id idMore /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
void idList(TreeNode t) {
if (token.lex == LexType.ID) {
t.name[(t.idnum)] = token.sem;
match(LexType.ID);
t.idnum = (t.idnum) + 1;
}
idMore(t);
}
// /
// 函数名 idMore /
// 功 能 类型声明部分的处理函数 /
// 产生式 < idMore > ::= ε | , idList /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
void idMore(TreeNode t) {
switch (token.lex) {
case SEMI:
break;
case COMMA:
match(LexType.COMMA);
idList(t);
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
}
// 变量声明部分 /
// /
// 函数名 varDec /
// 功 能 变量声明部分的处理函数 /
// 产生式 < varDec > ::= ε | varDeclaration /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode varDec() {
TreeNode t = null;
switch (token.lex) {
case PROCEDURE:
case BEGIN:
break;
case VAR:
t = varDeclaration();
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
return t;
}
// /
// 函数名 varDeclaration /
// 功 能 变量声明部分的处理函数 /
// 产生式 < varDeclaration > ::= VAR varDecList /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode varDeclaration() {
match(LexType.VAR);
TreeNode t = varDecList();
if (t == null)
syntaxisError("a var declaration is expected!");
return t;
}
// /
// 函数名 varDecList /
// 功 能 变量声明部分的处理函数 /
// 产生式 < varDecList > ::= typeName varIdList; varDecMore /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode varDecList() {
TreeNode t = newDecNode();
TreeNode p = null;
if (t != null) {
t.lineno = lineno;
typeName(t);
varIdList(t);
match(LexType.SEMI);
p = varDecMore();
t.sibling = p;
}
return t;
}
// /
// 函数名 varDecMore /
// 功 能 变量声明部分的处理函数 /
// 产生式 < varDecMore > ::= ε | varDecList /
// 说 明 函数根据文法产生式,调用相应的递归处理函数,生成语法树节点 /
// /
private TreeNode varDecMore() {
TreeNode t = null;
switch (token.lex) {
case PROCEDURE:
case BEGIN:
break;
case INTEGER:
case CHAR:
case ARRAY:
case RECORD:
case ID:
t = varDecList();
break;
default:
ReadNextToken();
syntaxisError("unexpected token is here!");
break;
}
return t;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -