匹配卷曲括号
我在这段代码中遇到了很难匹配大括号的时间。它始终指示没有“if”错误的“else”,但不确定语法应如何阅读。如果任何人都可以帮助它,将不胜感激。这里是代码:匹配卷曲括号
private void compileFactor() {
boolean its_a_variable = theInfo.isVar();
if (isIdent(theToken)) {
String ident = theToken;
theToken = t.token(); // handles var and const cases!
IdentInfo theInfo = symTable.lookup(ident);
}
boolean its_a_variable = theInfo.isVar();
int theAddr = theInfo.getAddr();
boolean isGlobal = theInfo.getIsGlobal();
int constValue = theInfo.getValue();
if (its_a_variable) { // pld12: CHANGE THIS!!
int theAddr = theInfo.getAddr();
boolean isGlobal = theInfo.getIsGlobal();
if (theAddr == -1) t.error("undeclared identifier used in expr: "+ident);
if (isGlobal) cs.emit(Machine.LOAD, theAddr);
else cs.emit(Machine.LOADF, theAddr);
} else {
int constValue = theInfo.getValue();
if (constValue = null) t.error("undeclared identifier used in expr: "+ident);
else {
cs.emitLOADI(theNumber);
}
else if (isNumber(theToken)) {
int theNumber = new Integer(theToken).intValue();
cs.emitLOADINT(theNumber);
theToken = t.token();
}
else if (equals(theToken, "(")) { // nothing to do to generate code!
accept("(");
compileExpr();
accept(")");
}
}
这else if
一个else
后发生的情况:
else if (isNumber(theToken)) {
int theNumber = new Integer(theToken).intValue();
cs.emitLOADINT(theNumber);
theToken = t.token();
}
编辑:固定和reindented
private void compileFactor() {
if (isIdent(theToken)) {
String ident = theToken;
theToken = t.token(); // handles var and const cases!
IdentInfo theInfo = symTable.lookup(ident);
boolean its_a_variable = theInfo.isVar();
int theAddr = theInfo.getAddr();
boolean isGlobal = theInfo.getIsGlobal();
int constValue = theInfo.getValue();
if (its_a_variable) { // pld12: CHANGE THIS!!
int theAddr = theInfo.getAddr();
boolean isGlobal = theInfo.getIsGlobal();
if (theAddr == -1) t.error("undeclared identifier used in expr: "+ident);
if (isGlobal) cs.emit(Machine.LOAD, theAddr);
else cs.emit(Machine.LOADF, theAddr);
} else {
int constValue = theInfo.getValue();
if (constValue = null) {
t.error("undeclared identifier used in expr: "+ident);
} else {
cs.emitLOADI(theNumber);
}
}
} else if (isNumber(theToken)) {
int theNumber = new Integer(theToken).intValue();
cs.emitLOADINT(theNumber);
theToken = t.token();
} else if (equals(theToken, "(")) { // nothing to do to generate code!
accept("(");
compileExpr();
accept(")");
}
}
我怀疑这里是你的问题。
if (constValue = null) t.error("undeclared identifier used in expr: "+ident);
else {
cs.emitLOADI(theNumber);
}
编辑应该是这样?
if (constValue = null) {
t.error("undeclared identifier used in expr: "+ident);
} else {
cs.emitLOADI(theNumber);
}
即使将if部分用作内联语句,也可能在if-else子句之间有多行。大部分空白并不重要。 – 2009-12-07 02:43:48
是的,但基于缩进,它看起来很像,如果是要加入其他。 – 2009-12-08 07:59:49
几个检查站
我想你会需要ISNUMBER(theToken)之前关闭括号
}} else if (isNumber(theToken)) {
,并可你不需要这行后支架?
IdentInfo theInfo = symTable.lookup(ident);
}
可能是整个代码是这样的吗?
private void compileFactor() {
boolean its_a_variable = theInfo.isVar();
if (isIdent(theToken)) {
String ident = theToken;
theToken = t.token(); // handles var and const cases!
IdentInfo theInfo = symTable.lookup(ident);
boolean its_a_variable = theInfo.isVar();
int theAddr = theInfo.getAddr();
boolean isGlobal = theInfo.getIsGlobal();
int constValue = theInfo.getValue();
if (its_a_variable) { // pld12: CHANGE THIS!!
int theAddr = theInfo.getAddr();
boolean isGlobal = theInfo.getIsGlobal();
if (theAddr == -1) t.error("undeclared identifier used in expr: "+ident);
if (isGlobal) cs.emit(Machine.LOAD, theAddr);
else cs.emit(Machine.LOADF, theAddr);
} else {
int constValue = theInfo.getValue();
if (constValue = null)
t.error("undeclared identifier used in expr: "+ident);
else {
cs.emitLOADI(theNumber);
}
}
} else if (isNumber(theToken)) {
int theNumber = new Integer(theToken).intValue();
cs.emitLOADINT(theNumber);
theToken = t.token();
} else if (equals(theToken, "(")) { // nothing to do to generate code!
accept("(");
compileExpr();
accept(")");
}
}
我不想开始支架放置神圣的战争,但这就是为什么我更喜欢“开放大括号下一行”成语。即使我没有IDE来帮助我,我也可以直观地排列开始和结束大括号。
就这样说,IDE是你最好的朋友。
我也喜欢的立即添加开口和填充它之前右括号的块。
成功避开一个行块,而不括号将帮助的建议。添加并不是很费劲。
+! “我不想开始支架放置圣战”哦,你是做... – 2009-12-07 02:41:41
只是说明我的理由,就这样。 – duffymo 2009-12-07 02:45:06
这是结构的样子:
private void compileFactor() {
if (...) {
...
}
if (...) {
...
if (...) ...
if (...) ...
else ...
} else {
if (...) ...
else {
...
* } else if (...) {
...
} else if (...) {
...
}
}
在哪里我已经把*
你有一个else以下的人。在这一点之前简单地添加另一个括号并不能解决它,因为这会在别的之后放入其他的。
所以,你的情况有一些根本性的错误,你几乎不得不从一开始就重新考虑它。
在所有if语句中添加括号,以确保它们确实在您期望的位置开始和结束。
如果您使用的是Visual Studio,您可以使用CTRL + “]” 键相匹配的括号
尝试评论这样的代码:
function blah(){ //start function
if(...){ //open if 1
for(...){ //open for
//do something
}//close for
} //close if 1
}//close function
我在一些langueages做在Flash中,虽然它让事情陷入困境。
祝你好运!
发布的代码有6'}'和7'{'。
最后的“{”以下摘录的语句,似乎没有匹配的一个“}”
else cs.emit(Machine.LOADF, theAddr);
} else {
为什么你不能使用文本编辑器,简单的语法的理解来跟踪所有相应的打开和关闭括号? – artdanil 2009-12-07 02:21:11
请说这是哪种语言。 – 2009-12-07 04:14:24