【问题标题】:How to fix Expected Identifiers errors C Language如何修复预期标识符错误 C 语言
【发布时间】:2018-12-24 15:12:40
【问题描述】:

我在编译代码时遇到两个“预期标识符”错误,即使它是根据 ANSI-C 标准编写的。 我使用带有标志 -Wall -ansi -pedantic 的 gcc 编译器进行编译。 我知道编译器很旧,但我的大学要求我使用它。

复制粘贴错误:

mycomp.c:202:5: error: expected identifier or ‘(’ before ‘return’
     return SUCCESS;
     ^
mycomp.c:203:1: error: expected identifier or ‘(’ before ‘}’ token
     }
     ^
makefile:10: recipe for target 'mycomp.o' failed

我尝试查看我的代码,但找不到任何语法问题。我尝试按照错误指南进行操作,例如,在“return”之前添加“(”或在“}”标记之前添加“(”,但这并没有改变任何内容,我仍然会收到这些错误。

代码又长又乱,但我只在最后得到一个错误,或者它:最后一个返回和最后一个 } 数字。

int main() {  
    char command[SIZE_OF_LINE];
    int commandIndex; int i;
    int numbers[2]; char var[2];

    /* As requested, initializing all complex values to 0 + 0i */
    read_comp(A, NONE, 0 ,0);
    read_comp(B, NONE, 0 ,0);
    read_comp(C, NONE, 0 ,0);
    read_comp(D, NONE, 0 ,0);
    read_comp(E, NONE, 0 ,0);
    read_comp(F, NONE, 0 ,0);

    commandIndex = 0;

    numbers[0] = commandIndex;
    numbers[1] = commandIndex;
    var[0] = '0';
    var[1] = '0';

    for(i = 0; ; i = 0)
        if(!fgets(command, SIZE_OF_LINE, stdin)) {
            i = undefinedCharSkipper(command, i);
            commandIndex = commandIdentifier(command, i);

            if(cmd[commandIndex].func == NULL) {
                fprintf(stderr, "Command does not exist:%s\n", command);
            }

            if(strcmp(cmd[commandIndex].name, "read_comp") == 0) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                i = undefinedCharSkipper(command, i);
                if(numberReceiver(command, i) != FAIL) {
                    numbers[0] = numberReceiver(command, i);
                }

                i = undefinedCharSkipper(command, i);
                if(numberReceiver(command, i) != FAIL) {
                    numbers[1] = numberReceiver(command, i);
                }

                /* Running command */
                read_comp(getComplex(var[0]), NONE, numbers[0], numbers[1]);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "print_comp") == 0 ) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                /* Running command */
                print_comp(getComplex(var[0]), NONE, 0, 0);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "add_comp") == 0 ) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[1] = letterReceiver(i);
                }

                /* Running command */
                add_comp(getComplex(var[0]), getComplex(var[1]), 0, 0);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "sub_comp") == 0) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[1] = letterReceiver(i);
                }

                /* Running command */
                sub_comp(getComplex(var[0]), getComplex(var[1]), 0, 0);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "mult_comp_real") == 0) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                i = undefinedCharSkipper(command, i);
                if(numberReceiver(command, i) != FAIL) {
                    numbers[0] = numberReceiver(command, i);
                }

                /* Running command */
                mult_comp_real(getComplex(var[0]), NONE, numbers[0], 0);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "mult_comp_img") == 0) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                i = undefinedCharSkipper(command, i);
                if(numberReceiver(command, i) != FAIL) {
                    numbers[0] = numberReceiver(command, i);
                }

                /* Running command */
                mult_comp_img(getComplex(var[0]), NONE, 0, numbers[0]);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "mult_comp_comp") == 0) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[1] = letterReceiver(i);
                }

                /* Running command */
                mult_comp_comp(getComplex(var[0]), getComplex(var[1]), 0, 0);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "abs_comp") == 0) {
                /* Recieving parameters and setting in place */
                i = undefinedCharSkipper(command, i);
                if(letterReceiver(i) != FAIL) {
                    var[0] = letterReceiver(i);
                }

                /* Running command */
                abs_comp(getComplex(var[0]), NONE, 0, 0);
                end(command, i);
            }

            if(strcmp(cmd[commandIndex].name, "stop") == 0) {
                stop(NONE, NONE, 0, 0);
                end(command, i);
            }
        }
        else { /* Failed scanning the user's input */
            printf("\n");
            printf("Scanning had failed");
            printf("\n");
        }
    }
    return SUCCESS;
}

预期结果:编译没有错误,因为这是我得到的唯一 2 个错误。 实际结果:出现 2 个错误。

【问题讨论】:

  • 您应该避免在编译器告诉您其他情况时声称您的代码是正确的。奥卡姆剃刀原则适用于这些情况。
  • 建议:一旦你匹配了一个字符串到cmd[commandIndex].name,所有后续的测试都可以省略。您可以使用else if 而不是if 来做到这一点。
  • 请提供一个最少个可验证的例子。你可以这样做。 G。一个接一个地删除if 块。向我们展示大量不相关的代码并不能真正帮助任何人。这样做可能会使问题变得明显。
  • @Gerhardh 问题已经解决,但非常感谢您尝试帮助我!

标签: c unix ansi-c


【解决方案1】:

仔细查看并正确缩进您的代码。

在部分

    }                     //----------------------- (1)
    return SUCCESS;        // -----------------------(2)
}                     

大括号不匹配,(1) 表示main() 函数的右大括号,所以标记为(2) 的部分;您的 return SUCCESS; } 位于文件范围内,这是不允许的。

纠正你的缩进并照顾好大括号。

【讨论】:

  • @ShadowOverLoad :您可能错过了这一点,因为您使用的是“line-saver”缩进样式,其中左大括号与复合语句之前的语句位于同一行。 (IMO)更明显的是,将左大括号放在它们自己的行上,彼此位于同一列中,并缩进 开头{。然后牙套脱颖而出,可以很容易地在视觉上匹配。在这里,我认为您在for 上省略了开头的{ - 正如Sourav 所说,无论是那个还是您的缩进都是误导性的。
  • @Clifford 这些年来,我一直在努力寻找支持将左大括号放在下一行进行类比的人。现在我知道我并不孤单。 :)
  • @SouravGhosh 我认为 line-saver 方法在编程文本中很常见,因为您可以在难以理解跨页代码拆分的页面上获得更多代码。在文本编辑器中这不是问题。史蒂夫·麦康奈尔(Steve McConnell)在 Code Compete 2nd 中。 ed 不同意我对牙套不缩进的看法,但 30 年的经验告诉我他错了。然而,关于这一点的所有意见都只是——意见。但是,如果您在团队中工作,则一致性很有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-12
  • 2022-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多