JavaCC语义分析实验
一、实验目的:
- 掌握自顶向下语义分析中语义子程序的添加过程;
- 掌握“拉链”、“回填”操作的原理及实现;
- 根据 MiniC 的上下文无关文法,对赋值语句、算术表达式、关系表达式、
if-else 语句、while 语句、布尔表达式等语法结构添加语义子程序; - 针对测试代码,输出四元式序列。
二、MiniC 语法 (文法 1、2,二者选择其一实现)
文法1:无布尔表达式
〈程序〉
→ int main() {〈语句块〉* }
〈语句块〉 →〈语句〉| {〈语句块〉* }
〈语句〉
→〈顺序语句〉|〈条件语句〉|〈循环语句〉
〈顺序语句〉→ (〈声明语句〉| 〈赋值语句〉) ”;”
〈声明语句〉→ int ID,ID,…,ID //思考如何表示
〈赋值语句〉→ ID =〈表达式〉
〈条件语句〉→ if (〈条件〉)〈语句块〉 else 〈语句块〉
〈循环语句〉→ while (〈条件〉)〈语句块〉
〈条件〉
→〈表达式〉(〈关系符〉〈表达式〉)?
〈表达式〉 → // 可以使用javacc自动生成的 表达式(Expression)的文法
〈关系符〉 → <| <=| >| >= | ==| !=
文法2:有布尔表达式
〈程序〉
→ int main() {〈语句块〉* }
〈语句块〉 →〈语句〉| {〈语句块〉* }
〈语句〉
→〈顺序语句〉|〈条件语句〉|〈循环语句〉
〈顺序语句〉→ (〈声明语句〉| 〈赋值语句〉) ”;”
〈声明语句〉→ int ID,ID,…,ID //思考如何表示
〈赋值语句〉→ ID =〈表达式〉
〈条件语句〉→ if (〈逻辑〉)〈语句块〉 else 〈语句块〉
〈循环语句〉→ while (〈逻辑〉)〈语句块〉
〈逻辑〉 →〈条件〉(〈逻辑运算符〉〈条件〉)*
〈条件〉
→〈表达式〉(〈关系符〉〈表达式〉)?
〈表达式〉 → // 可以使用javacc自动生成的 表达式(Expression)的文法
〈关系符〉 → <| <=| >| >= | ==| !=
〈逻辑运算符〉→ && | || | !
注意:该文法为参考文法,可以对其适当修改。首先,文件中的文法必须不
含左递归和回溯;其次,需要考虑如何通过文法来定义逻辑运算符的优先级。
三.实验过程
Minic语法:
〈程序〉 → int main() {〈语句块〉*}
〈语句块〉 →〈语句〉| {〈语句块〉* }
源码
/* Generated By:JJTree&JavaCC: Do not edit this line. MiniC.java */
package miniC;
import util.*;
public class MiniC/*@bgen(jjtree)*/implements MiniCTreeConstants, MiniCConstants {/*@bgen(jjtree)*/
protected JJTMiniCState jjtree = new JJTMiniCState();QTList qtList = new QTList();
VariableNameGenerator variableNameGenerator = new VariableNameGenerator();
public static void main(String args [])
{
System.out.println("Reading from standard input...");
System.out.print("Enter an expression like \u005c"1+(2+3)*var;\u005c" :");
MiniC parser = new MiniC(System.in);
try
{
SimpleNode n = parser.Start();
n.dump("");
System.out.println("Thank you.");
}
catch (Exception e)
{
System.out.println("Oops.");
System.out.println(e.getMessage());
}
}
final public SimpleNode Start() throws ParseException {
/*@bgen(jjtree) Start */
SimpleNode jjtn000 = new SimpleNode(JJTSTART);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);
try {
program();
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
{if (true) return jjtn000;}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
//程序
final public void program() throws ParseException {
/*@bgen(jjtree) program */
SimpleNode jjtn000 = new SimpleNode(JJTPROGRAM);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);
try {
jj_consume_token(INT);
jj_consume_token(MAIN);
jj_consume_token(LB);
jj_consume_token(RB);
jj_consume_token(LS);
label_1:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INT:
case WHILE:
case FLOAT:
case BOOL:
case DOUBLE:
case FOR:
case IF:
case CHAR:
case DO:
case SWITCH:
case LS:
case IDENTIFIER:
;
break;
default:
jj_la1[0] = jj_gen;
break label_1;
}
StatementBlocks();
}
jj_consume_token(RETURN);
Integer();
jj_consume_token(SEMIC);
jj_consume_token(RS);
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void Statement() throws ParseException {
/*@bgen(jjtree) Statement */
SimpleNode jjtn000 = new SimpleNode(JJTSTATEMENT);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);
try {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INT:
case FLOAT:
case BOOL:
case DOUBLE:
case CHAR:
case SWITCH:
case IDENTIFIER:
SeqStatement();
break;
case IF:
CondStatement();
break;
case WHILE:
case FOR:
case DO:
LoopStatement();
break;
default:
jj_la1[1] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void StatementBlocks() throws ParseException {
/*@bgen(jjtree) StatementBlocks */
SimpleNode jjtn000 = new SimpleNode(JJTSTATEMENTBLOCKS);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);
try {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INT:
case WHILE:
case FLOAT:
case BOOL:
case DOUBLE:
case FOR:
case IF:
case CHAR:
case DO:
case SWITCH:
case IDENTIFIER:
Statement();
break;
case LS:
jj_consume_token(LS);
label_2:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INT:
case WHILE:
case FLOAT:
case BOOL:
case DOUBLE:
case FOR:
case IF:
case CHAR:
case DO:
case SWITCH:
case LS:
case IDENTIFIER:
;
break;
default:
jj_la1[2] = jj_gen;
break label_2;
}
StatementBlocks();
}
jj_consume_token(RS);
break;
default:
jj_la1[3] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void SeqStatement() throws ParseException {
/*@bgen(jjtree) SeqStatement */
SimpleNode jjtn000 = new SimpleNode(JJTSEQSTATEMENT);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);
try {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INT:
case FLOAT:
case BOOL:
case DOUBLE:
case CHAR:
case IDENTIFIER:
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INT:
case FLOAT:
case BOOL:
case DOUBLE:
case CHAR:
DeclarStatement();
break;
case IDENTIFIER:
AssignStatement();
break;
default:
jj_la1[4] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
jj_consume_token(SEMIC);
break;
case SWITCH:
SwitchCase();
break;
default:
jj_la1[5] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void DeclarStatement() throws ParseException {
/*@bgen(jjtree) DeclarStatement */
SimpleNode jjtn000 = new SimpleNode(JJTDECLARSTATEMENT);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);
try {
DeclarKey();
Identifier();
label_3:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case COMMA:
;
break;
default:
jj_la1[6] = jj_gen;
break label_3;
}
jj_consume_token(COMMA);
Identifier();
}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void AssignStatement() throws ParseException {
/*@bgen(jjtree) AssignStatement */
SimpleNode jjtn000 = new SimpleNode(JJTASSIGNSTATEMENT);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);Token op;
Token first;
String exp;
try {
first = Identifier();
op = jj_consume_token(EQ);
exp = Expression();
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
QTInfo qt = new QTInfo(op.image, exp, "_", first.image);
qtList.addQTInfo(qt);
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void CondStatement() throws ParseException {
/*@bgen(jjtree) CondStatement */
SimpleNode jjtn000 = new SimpleNode(JJTCONDSTATEMENT);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);ConditionValue chain = new ConditionValue();
ConditionValue temp = new ConditionValue();
int i;
try {
jj_consume_token(IF);
jj_consume_token(LB);
chain = Logic();
jj_consume_token(RB);
i = QTInfo.size + 1;
chain.backpatchTrueChain(i);
StatementBlocks();
QTInfo qt = new QTInfo("J", "_", "_", "N");
temp.mergeTrue(qt);
qtList.addQTInfo(qt);
i = QTInfo.size + 1;
chain.backpatchFalseChain(i);
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case ELSE:
jj_consume_token(ELSE);
StatementBlocks();
i = QTInfo.size + 1;
temp.backpatchTrueChain(i);
break;
default:
jj_la1[7] = jj_gen;
;
}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public ConditionValue Logic() throws ParseException {
/*@bgen(jjtree) Logic */
SimpleNode jjtn000 = new SimpleNode(JJTLOGIC);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);int seq = 0;
int tag = 0;
ConditionValue chain = new ConditionValue();
ConditionValue resChain = new ConditionValue();
ConditionValue chain2 = new ConditionValue();
try {
chain = LogicAnd();
label_4:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case OR:
;
break;
default:
jj_la1[8] = jj_gen;
break label_4;
}
jj_consume_token(OR);
tag = 1;
seq = QTInfo.size + 1;
chain.backpatchFalseChain(seq);
chain2 = LogicAnd();
resChain = new ConditionValue();
resChain.mergeFalse(chain2);
chain2.mergeTrue(chain);
resChain.mergeTrue(chain2);
chain = resChain;
}
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
if (tag == 0)
{if (true) return chain;}
else
{if (true) return resChain;}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
final public ConditionValue LogicAnd() throws ParseException {
/*@bgen(jjtree) LogicAnd */
SimpleNode jjtn000 = new SimpleNode(JJTLOGICAND);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);int seq = 0;
int tag = 0;
ConditionValue chain = new ConditionValue();
ConditionValue resChain = new ConditionValue();
ConditionValue chain2 = new ConditionValue();
try {
chain = LogicNot();
label_5:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case AND:
;
break;
default:
jj_la1[9] = jj_gen;
break label_5;
}
jj_consume_token(AND);
tag = 1;
seq = QTInfo.size + 1;
chain.backpatchTrueChain(seq);
chain2 = LogicNot();
resChain = new ConditionValue();
resChain.mergeTrue(chain2);
chain2.mergeFalse(chain);
resChain.mergeFalse(chain2);
chain = resChain;
}
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
if (tag == 0)
{if (true) return chain;}
else
{if (true) return resChain;}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
final public ConditionValue LogicNot() throws ParseException {
/*@bgen(jjtree) LogicNot */
SimpleNode jjtn000 = new SimpleNode(JJTLOGICNOT);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);ConditionValue chain = new ConditionValue();
ConditionValue chain1 = new ConditionValue();
try {
label_6:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INTEGER_LITERAL:
case LB:
case IDENTIFIER:
chain = Condition();
break;
case UN:
jj_consume_token(UN);
chain1 = Condition();
chain.FalseMergeTrue(chain1);
chain.TrueMergeFalse(chain1);
break;
default:
jj_la1[10] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INTEGER_LITERAL:
case LB:
case UN:
case IDENTIFIER:
;
break;
default:
jj_la1[11] = jj_gen;
break label_6;
}
}
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
{if (true) return chain;}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
final public ConditionValue Condition() throws ParseException {
/*@bgen(jjtree) Condition */
SimpleNode jjtn000 = new SimpleNode(JJTCONDITION);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);int tag = 0;
String first;
String middle;
String rop;
ConditionValue chain = new ConditionValue();
try {
first = Expression();
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case LESS:
case LESSN:
case GREAT:
case GREATN:
case EQU:
case NOTEQU:
rop = RelationChar();
middle = Expression();
QTInfo qt1 = new QTInfo("J" + rop, first, middle, "N");
QTInfo qt2 = new QTInfo("J", "_", "_", "N");
chain.mergeTrue(qt1);
chain.mergeFalse(qt2);
qtList.addQTInfo(qt1);
qtList.addQTInfo(qt2);
tag = 1;
break;
default:
jj_la1[12] = jj_gen;
;
}
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
if(tag == 0)
{
QTInfo qt1 = new QTInfo("JNZ", first, "_", "N");
QTInfo qt2 = new QTInfo("J", "_", "_", "N");
chain.mergeTrue(qt1);
chain.mergeFalse(qt2);
qtList.addQTInfo(qt1);
qtList.addQTInfo(qt2);
}
{if (true) return chain;}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
final public void LoopStatement() throws ParseException {
/*@bgen(jjtree) LoopStatement */
SimpleNode jjtn000 = new SimpleNode(JJTLOOPSTATEMENT);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);
try {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case WHILE:
While();
break;
case DO:
Do_While();
break;
case FOR:
For_Loop();
break;
default:
jj_la1[13] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void While() throws ParseException {
/*@bgen(jjtree) While */
SimpleNode jjtn000 = new SimpleNode(JJTWHILE);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);int seq = 0, tag;
ConditionValue chain;
ConditionValue temp = new ConditionValue();
try {
jj_consume_token(WHILE);
jj_consume_token(LB);
tag = qtList.QTList.size() + 1;
chain = Logic();
jj_consume_token(RB);
jj_consume_token(LS);
seq = QTInfo.size + 1;
chain.backpatchTrueChain(seq);
label_7:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INT:
case WHILE:
case FLOAT:
case BOOL:
case DOUBLE:
case FOR:
case IF:
case CHAR:
case DO:
case SWITCH:
case LS:
case IDENTIFIER:
;
break;
default:
jj_la1[14] = jj_gen;
break label_7;
}
StatementBlocks();
}
jj_consume_token(RS);
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
QTInfo qt = new QTInfo("J", "_", "_", Integer.toString(tag));
qtList.addQTInfo(qt);
seq = QTInfo.size + 1;
chain.backpatchFalseChain(seq);
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void Do_While() throws ParseException {
/*@bgen(jjtree) Do_While */
SimpleNode jjtn000 = new SimpleNode(JJTDO_WHILE);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);int tag,now;
ConditionValue chain;
try {
jj_consume_token(DO);
jj_consume_token(LS);
tag = qtList.QTList.size() + 1;
label_8:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INT:
case WHILE:
case FLOAT:
case BOOL:
case DOUBLE:
case FOR:
case IF:
case CHAR:
case DO:
case SWITCH:
case LS:
case IDENTIFIER:
;
break;
default:
jj_la1[15] = jj_gen;
break label_8;
}
StatementBlocks();
}
jj_consume_token(RS);
jj_consume_token(WHILE);
jj_consume_token(LB);
chain = Logic();
now = QTInfo.size + 1;
chain.backpatchTrueChain(tag);
chain.backpatchFalseChain(now);
jj_consume_token(RB);
jj_consume_token(SEMIC);
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void For_Loop() throws ParseException {
/*@bgen(jjtree) For_Loop */
SimpleNode jjtn000 = new SimpleNode(JJTFOR_LOOP);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);int id1=-1,id2=-1,id3=-1,now=-1,add=-1,flag1=0,flag2=0;
ConditionValue chain = new ConditionValue();
try {
jj_consume_token(FOR);
jj_consume_token(LB);
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case IDENTIFIER:
AssignStatement();
break;
default:
jj_la1[16] = jj_gen;
;
}
jj_consume_token(SEMIC);
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INTEGER_LITERAL:
case LB:
case UN:
case IDENTIFIER:
id1 = qtList.QTList.size() + 1;
flag1 = 1;
chain = Logic();
break;
default:
jj_la1[17] = jj_gen;
;
}
if(flag1==0)
{
add = qtList.QTList.size();
QTInfo qt = new QTInfo("J", "_", "_", "N");
qtList.addQTInfo(qt);
}
jj_consume_token(SEMIC);
id2 = qtList.QTList.size() + 1;
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case IDENTIFIER:
flag2 = 1;
AssignStatement();
if(flag1==1)
{
QTInfo qt = new QTInfo("J", "_", "_", Integer.toString(id1));
qtList.addQTInfo(qt);
}
break;
default:
jj_la1[18] = jj_gen;
;
}
if(flag1==1)
{
now = QTInfo.size + 1;
chain.backpatchTrueChain(now);
}
id3 = qtList.QTList.size() + 1;
jj_consume_token(RB);
now = QTInfo.size + 1;
if(flag1==0&&flag2==1)
{
QTInfo temp = qtList.get(add);
temp.setResult(now);
qtList.set(add,temp);
}
StatementBlocks();
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
if(flag2==1)
{
QTInfo qt = new QTInfo("J", "_", "_", Integer.toString(id2));
qtList.addQTInfo(qt);
now = QTInfo.size + 1;
chain.backpatchFalseChain(now);
}
else if(flag1==1)
{
QTInfo qt = new QTInfo("J", "_", "_", Integer.toString(id1));
qtList.addQTInfo(qt);
now = QTInfo.size + 1;
chain.backpatchFalseChain(now);
}
else
{
QTInfo qt = new QTInfo("J", "_", "_", Integer.toString(id3));
qtList.addQTInfo(qt);
now = QTInfo.size + 1;
chain.backpatchFalseChain(now);
}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void SwitchCase() throws ParseException {
/*@bgen(jjtree) SwitchCase */
SimpleNode jjtn000 = new SimpleNode(JJTSWITCHCASE);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);Token t,t1;
ConditionValue chain = new ConditionValue();
int flag=0,tag=0,now=0;
try {
jj_consume_token(SWITCH);
jj_consume_token(LB);
t = Identifier();
jj_consume_token(RB);
jj_consume_token(LS);
label_9:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case CASE:
;
break;
default:
jj_la1[19] = jj_gen;
break label_9;
}
jj_consume_token(CASE);
t1 = jj_consume_token(INTEGER_LITERAL);
now = QTInfo.size + 3;
QTInfo qt1 = new QTInfo("J==", t.image, t1.image, now);
QTInfo qt2 = new QTInfo("J", "_", "_", "N");
//chain.mergeTrue(qt1);
chain.mergeFalse(qt2);
qtList.addQTInfo(qt1);
qtList.addQTInfo(qt2);
jj_consume_token(COLON);
now = QTInfo.size + 1;
chain.backpatchTrueChain(now);
StatementBlocks();
}
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case DEFAULTT:
jj_consume_token(DEFAULTT);
jj_consume_token(COLON);
flag = 1;
tag = qtList.QTList.size() + 1;
StatementBlocks();
break;
default:
jj_la1[20] = jj_gen;
;
}
if(flag==0)
tag = qtList.QTList.size() + 1;
chain.backpatchFalseChain(tag);
jj_consume_token(RS);
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public String Expression() throws ParseException {
/*@bgen(jjtree) Expression */
SimpleNode jjtn000 = new SimpleNode(JJTEXPRESSION);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);String exp;
try {
exp = AdditiveExpression();
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
{if (true) return exp;}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
final public String AdditiveExpression() throws ParseException {
/*@bgen(jjtree) AdditiveExpression */
SimpleNode jjtn000 = new SimpleNode(JJTADDITIVEEXPRESSION);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);String first;
String middle;
String newTemp;
Token op;
try {
first = MultiplicativeExpression();
newTemp = first;
label_10:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case ADD:
case SUB:
;
break;
default:
jj_la1[21] = jj_gen;
break label_10;
}
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case ADD:
op = jj_consume_token(ADD);
break;
case SUB:
op = jj_consume_token(SUB);
break;
default:
jj_la1[22] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
middle = MultiplicativeExpression();
newTemp = variableNameGenerator.genVariableName();
QTInfo qt = new QTInfo(op.image, first, middle, newTemp);
qtList.addQTInfo(qt);
first = newTemp;
}
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
{if (true) return newTemp;}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
final public String MultiplicativeExpression() throws ParseException {
/*@bgen(jjtree) MultiplicativeExpression */
SimpleNode jjtn000 = new SimpleNode(JJTMULTIPLICATIVEEXPRESSION);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);String first;
String middle;
String newTemp;
Token op;
try {
first = UnaryExpression();
newTemp = first;
label_11:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case MULT:
case DIV:
case MOD:
;
break;
default:
jj_la1[23] = jj_gen;
break label_11;
}
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case MULT:
op = jj_consume_token(MULT);
break;
case DIV:
op = jj_consume_token(DIV);
break;
case MOD:
op = jj_consume_token(MOD);
break;
default:
jj_la1[24] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
middle = UnaryExpression();
newTemp = variableNameGenerator.genVariableName();
QTInfo qt = new QTInfo(op.image, first, middle, newTemp);
qtList.addQTInfo(qt);
first = newTemp;
}
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
{if (true) return newTemp;}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
final public String UnaryExpression() throws ParseException {
/*@bgen(jjtree) UnaryExpression */
SimpleNode jjtn000 = new SimpleNode(JJTUNARYEXPRESSION);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);String exp;
Token t;
try {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case LB:
jj_consume_token(LB);
exp = Expression();
jj_consume_token(RB);
break;
case IDENTIFIER:
t = Identifier();
exp = t.image;
break;
case INTEGER_LITERAL:
t = Integer();
exp = t.image;
break;
default:
jj_la1[25] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
{if (true) return exp;}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
final public Token Identifier() throws ParseException {
/*@bgen(jjtree) Identifier */
SimpleNode jjtn000 = new SimpleNode(JJTIDENTIFIER);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);Token t;
try {
t = jj_consume_token(IDENTIFIER);
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
{if (true) return t;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
final public Token Integer() throws ParseException {
/*@bgen(jjtree) Integer */
SimpleNode jjtn000 = new SimpleNode(JJTINTEGER);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);Token t;
try {
t = jj_consume_token(INTEGER_LITERAL);
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
{if (true) return t;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
final public Token Float() throws ParseException {
/*@bgen(jjtree) Float */
SimpleNode jjtn000 = new SimpleNode(JJTFLOAT);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);Token t;
try {
t = jj_consume_token(FLOAT_LITERAL);
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
{if (true) return t;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
final public Token StartKey() throws ParseException {
/*@bgen(jjtree) StartKey */
SimpleNode jjtn000 = new SimpleNode(JJTSTARTKEY);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);Token t;
try {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case VOID:
t = jj_consume_token(VOID);
break;
case INT:
t = jj_consume_token(INT);
break;
default:
jj_la1[26] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
{if (true) return t;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
final public Token DeclarKey() throws ParseException {
/*@bgen(jjtree) DeclarKey */
SimpleNode jjtn000 = new SimpleNode(JJTDECLARKEY);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);Token t;
try {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INT:
t = jj_consume_token(INT);
break;
case FLOAT:
t = jj_consume_token(FLOAT);
break;
case CHAR:
t = jj_consume_token(CHAR);
break;
case DOUBLE:
t = jj_consume_token(DOUBLE);
break;
case BOOL:
t = jj_consume_token(BOOL);
break;
default:
jj_la1[27] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
{if (true) return t;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
final public String RelationChar() throws ParseException {
/*@bgen(jjtree) RelationChar */
SimpleNode jjtn000 = new SimpleNode(JJTRELATIONCHAR);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);Token t;
try {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case LESS:
t = jj_consume_token(LESS);
break;
case LESSN:
t = jj_consume_token(LESSN);
break;
case GREAT:
t = jj_consume_token(GREAT);
break;
case GREATN:
t = jj_consume_token(GREATN);
break;
case EQU:
t = jj_consume_token(EQU);
break;
case NOTEQU:
t = jj_consume_token(NOTEQU);
break;
default:
jj_la1[28] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
{if (true) return t.image;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
/** Generated Token Manager. */
public MiniCTokenManager token_source;
SimpleCharStream jj_input_stream;
/** Current token. */
public Token token;
/** Next token. */
public Token jj_nt;
private int jj_ntk;
private int jj_gen;
final private int[] jj_la1 = new int[29];
static private int[] jj_la1_0;
static private int[] jj_la1_1;
static {
jj_la1_init_0();
jj_la1_init_1();
}
private static void jj_la1_init_0() {
jj_la1_0 = new int[] {0xc8fb000,0xc8fb000,0xc8fb000,0xc8fb000,0x839000,0x8839000,0x0,0x100000,0x0,0x0,0x80000080,0x80000080,0x0,0x4042000,0xc8fb000,0xc8fb000,0x0,0x80000080,0x0,0x10000000,0x20000000,0x0,0x0,0x0,0x0,0x80000080,0x5000,0x839000,0x0,};
}
private static void jj_la1_init_1() {
jj_la1_1 = new int[] {0x200002,0x200000,0x200002,0x200002,0x200000,0x200000,0x40000,0x0,0x400,0x200,0x200800,0x200800,0x3f000,0x0,0x200002,0x200002,0x200000,0x200800,0x200000,0x0,0x0,0x18,0x18,0xe0,0xe0,0x200000,0x0,0x0,0x3f000,};
}
/** Constructor with InputStream. */
public MiniC(java.io.InputStream stream) {
this(stream, null);
}
/** Constructor with InputStream and supplied encoding */
public MiniC(java.io.InputStream stream, String encoding) {
try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source = new MiniCTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 29; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
public void ReInit(java.io.InputStream stream) {
ReInit(stream, null);
}
/** Reinitialise. */
public void ReInit(java.io.InputStream stream, String encoding) {
try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source.ReInit(jj_input_stream);
token = new Token();
jj_ntk = -1;
jjtree.reset();
jj_gen = 0;
for (int i = 0; i < 29; i++) jj_la1[i] = -1;
}
/** Constructor. */
public MiniC(java.io.Reader stream) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
token_source = new MiniCTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 29; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
public void ReInit(java.io.Reader stream) {
jj_input_stream.ReInit(stream, 1, 1);
token_source.ReInit(jj_input_stream);
token = new Token();
jj_ntk = -1;
jjtree.reset();
jj_gen = 0;
for (int i = 0; i < 29; i++) jj_la1[i] = -1;
}
/** Constructor with generated Token Manager. */
public MiniC(MiniCTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 29; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
public void ReInit(MiniCTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jjtree.reset();
jj_gen = 0;
for (int i = 0; i < 29; i++) jj_la1[i] = -1;
}
private Token jj_consume_token(int kind) throws ParseException {
Token oldToken;
if ((oldToken = token).next != null) token = token.next;
else token = token.next = token_source.getNextToken();
jj_ntk = -1;
if (token.kind == kind) {
jj_gen++;
return token;
}
token = oldToken;
jj_kind = kind;
throw generateParseException();
}
/** Get the next Token. */
final public Token getNextToken() {
if (token.next != null) token = token.next;
else token = token.next = token_source.getNextToken();
jj_ntk = -1;
jj_gen++;
return token;
}
/** Get the specific Token. */
final public Token getToken(int index) {
Token t = token;
for (int i = 0; i < index; i++) {
if (t.next != null) t = t.next;
else t = t.next = token_source.getNextToken();
}
return t;
}
private int jj_ntk() {
if ((jj_nt=token.next) == null)
return (jj_ntk = (token.next=token_source.getNextToken()).kind);
else
return (jj_ntk = jj_nt.kind);
}
private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
private int[] jj_expentry;
private int jj_kind = -1;
/** Generate ParseException. */
public ParseException generateParseException() {
jj_expentries.clear();
boolean[] la1tokens = new boolean[56];
if (jj_kind >= 0) {
la1tokens[jj_kind] = true;
jj_kind = -1;
}
for (int i = 0; i < 29; i++) {
if (jj_la1[i] == jj_gen) {
for (int j = 0; j < 32; j++) {
if ((jj_la1_0[i] & (1<<j)) != 0) {
la1tokens[j] = true;
}
if ((jj_la1_1[i] & (1<<j)) != 0) {
la1tokens[32+j] = true;
}
}
}
}
for (int i = 0; i < 56; i++) {
if (la1tokens[i]) {
jj_expentry = new int[1];
jj_expentry[0] = i;
jj_expentries.add(jj_expentry);
}
}
int[][] exptokseq = new int[jj_expentries.size()][];
for (int i = 0; i < jj_expentries.size(); i++) {
exptokseq[i] = jj_expentries.get(i);
}
return new ParseException(token, exptokseq, tokenImage);
}
/** Enable tracing. */
final public void enable_tracing() {
}
/** Disable tracing. */
final public void disable_tracing() {
}
}
四、实验结果