If statement 匹配花括号
我很难在这段代码中匹配花括号。它一直指示一个else,没有if错误,但不确定语法应该如何读取。如果有人能帮忙,我将不胜感激。代码如下:If statement 匹配花括号,if-statement,If Statement,我很难在这段代码中匹配花括号。它一直指示一个else,没有if错误,但不确定语法应该如何读取。如果有人能帮忙,我将不胜感激。代码如下: private void compileFactor() { boolean its_a_variable = theInfo.isVar(); if (isIdent(theToken)) { String ident = theToken; theToken = t.token(
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(")");
}
}
我想这是你的问题
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 (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);
}
此else if发生在else之后:
编辑:已修复并重新凹入
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(")");
}
}
此else if发生在else之后:
编辑:已修复并重新凹入
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(")");
}
}
如果您在匹配大括号时遇到问题,建议您启动一个新的函数定义,只需添加控制流和分支ifs、else…,并在每个if/else if/else块上使用大括号,即使是一行程序 如果正确,请添加函数的其余部分。
当大括号变得不需要动脑筋时,就开始跳过一行程序。会建议如果在匹配大括号时遇到问题,开始一个新的函数定义,只需添加控制流和分支if、else等,并在每个if/else if/else块上使用大括号,即使是一行程序 如果正确,请添加函数的其余部分。 当大括号变得不需要动脑筋时,就开始跳过它们一行。几个检查点 我想你需要先把括号合上,然后再把数字记下来
}} 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(")");
}
}
很少有检查站
我想你需要先把括号合上,然后再把数字记下来
}} 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是您最好的朋友 我还喜欢这样一个建议,即在填充块之前立即向块中添加开始和结束括号
避免使用不带大括号的单线图块将有所帮助。我不想发动一场圣战,但这就是为什么我更喜欢下一行开头括号的习惯用法。即使没有IDE帮助,我也可以在视觉上排列打开和关闭支架 话虽如此,IDE是您最好的朋友 我还喜欢这样一个建议,即在填充块之前立即向块中添加开始和结束括号
避免使用不带大括号的单线图块将有所帮助。实际上,添加的工作量并不大。这就是结构的外观:
private void compileFactor() {
if (...) {
...
}
if (...) {
...
if (...) ...
if (...) ...
else ...
} else {
if (...) ...
else {
...
* } else if (...) {
...
} else if (...) {
...
}
}
我把*放在这里,你有一个else跟在一个else后面。简单地在该点之前添加另一个括号也无法修复它,因为这会将else放在另一个else之后
所以,你的身体状况有一些根本性的问题,你必须从一开始就重新思考
在所有if语句中添加括号,这样您就可以确保它们确实在您期望的地方开始和结束。这就是结构的外观:
private void compileFactor() {
if (...) {
...
}
if (...) {
...
if (...) ...
if (...) ...
else ...
} else {
if (...) ...
else {
...
* } else if (...) {
...
} else if (...) {
...
}
}
我把*放在这里,你有一个else跟在一个else后面。简单地在该点之前添加另一个括号也无法修复它,因为这会将else放在另一个else之后
所以,你的身体状况有一些根本性的问题,你必须从一开始就重新思考
在所有if语句中添加括号,以便确保它们确实在您期望的位置开始和结束。如果您使用Visual Studio,可以使用CTRL+]键匹配括号如果您使用Visual Studio,可以使用CTRL+]键匹配括号尝试如下注释代码:
function blah(){ //start function
if(...){ //open if 1
for(...){ //open for
//do something
}//close for
} //close if 1
}//close function
我用Flash来做,虽然在某些语言中它会让事情陷入困境
祝你好运 试着这样评论代码:
function blah(){ //start function
if(...){ //open if 1
for(...){ //open for
//do something
}//close for
} //close if 1
}//close function
我用Flash来做,虽然在某些语言中它会让事情陷入困境
祝你好运 发布的代码有6'}'和7'{' 以下摘录的最后一个“{”语句似乎没有匹配的“}”
else cs.emit(Machine.LOADF, theAddr);
} else {
发布的代码有6'}'和7'{' 以下摘录的最后一个“{”语句似乎没有匹配的“}”
else cs.emit(Machine.LOADF, theAddr);
} else {
为什么您不能使用具有简单语法理解的文本编辑器来跟踪所有相应的开始括号和结束括号?请说明这是什么语言。为什么您不能使用具有简单语法理解的文本编辑器来跟踪所有相应的开始括号和结束括号?请说明这是什么语言……或者永远不要开始跳过大括号所有。如果代码块周围总是有大括号,有些人会觉得代码更清晰。是的-个人总是把它们留在里面,但不想发动一场圣战:……或者根本就不跳过大括号。有些人觉得代码更清晰
if-else子句之间可能有多行,即使if部分用作内联语句。大多数部分的空白并不重要。是的,但根据缩进,它看起来很像if是要连接到else的。即使if部分用作内联语句,if else子句之间也可能有多行。大部分空白都不重要。是的,但根据缩进,它看起来很像if是用来连接到其他的。只说明我的原因,就这样。只说明我的原因,就这样。如果if是数字,那么if可能是if Ident Token{,else if after-else没有意义。我猜它没有意义;它上面的else缺少了一个结束括号。是的,它确实需要结束括号,在那里,else if是数字,如果它是数字,那么它可能是用于if-identthetoken的{,else if after else没有意义。我猜它没有意义;它上面的else缺少一个结束括号。是的,它确实需要结束括号