繁体   English   中英

转到标签上的“预期表达”错误(非本地转到)

[英]“expected expression” error on goto label (non-local goto)

是的,我知道Esdger Dijkstra说你不应该使用goto ,但他不是上帝。 我认为只要不过度使用无条件分支就可以了。 通过过度使用分支语句过度使用继承,您就有可能创建不可读的代码。

无论如何,既然我已经完成了先发制人的反驳,那么这就是我写的节目。 它应该从文件中读取代码(最终我希望它读取HTML代码,但我现在使用自己的简化标记语言,因为它更容易)并将其转换为可用作enscript程序输入的格式。

#include <stdio.h>
#include <stdlib.h>

#define INPUT_ERROR 1

void writecolor( FILE *, float, float, float );
unsigned short hextonum( char );

char escape = '\0'; // Default for enscript

int main( int argc, char **argv ){
    FILE *in;
    FILE *out;
    if( argv[1] ){
        in = fopen( argv[1], "r" );
    }
    else{
        in = stdin;
    }
    if( argv[2] ){
        out = fopen( argv[2], "w" );
    }
    else{
        out = stdout;
    }
    char c;       // Input character
    float red;    // Red value from hex code
    float green;  // Green value from hex code
    float blue;   // Blue value from hex code
    int line = 1; // Current line number, used for error reporting
    while( (c = fgetc( in )) != EOF ){
        if( c == '\\' ){
            if( fgetc( in ) == '#' ){
                red = (float) hextonum( fgetc( in ) ) * 10 / 16 * 0.1 + (float) hextonum( fgetc( in ) ) * 10 / 16 * 0.01;
                green = (float) hextonum( fgetc( in ) ) * 10 / 16 * 0.1 + (float) hextonum( fgetc( in ) ) * 10 / 16 * 0.01;
                blue = (float) hextonum( fgetc( in ) ) * 10 / 16 * 0.1 + (float) hextonum( fgetc( in ) ) * 10 / 16 * 0.01;
                writecolor( out, red, green, blue );
            }
        }
        else{
            fputc( c, out );
        }
        if( c == '\n' ){
            line++;
        }
    }
    fclose( in );
    fclose( out );
    return 0;
    :infile_error // XXX goto in hextonum branches here
    fprintf( stderr, "%s: Error on line %d of input file.\n", argv[0], line );
    return INPUT_ERROR;
}

// Converts a color code in the markup to a color code
// recognized by enscript
void writecolor( FILE *fp, float red, float green, float blue ){
    fwrite( &escape, 1, 1, fp );
    fprintf( fp, "color{%f %f %f}", red, green, blue );
}

// Converts a hex digit to its corresponding value
unsigned short hextonum( char hex ){
    if( hex >= '0' && hex <= '9' ){
        return atoi( hex );
    }
    switch( hex ){
        case( 'a' ) : case( 'A' ) : return 0xa;
        case( 'b' ) : case( 'B' ) : return 0xb;
        case( 'c' ) : case( 'C' ) : return 0xc;
        case( 'd' ) : case( 'D' ) : return 0xd;
        case( 'e' ) : case( 'E' ) : return 0xe;
        case( 'f' ) : case( 'F' ) : return 0xf;
    }
    // Okay, I think rather than putting an if statement
    // around every piece of code that uses this function,
    // I'm just going to jump to an error code in the
    // main function.
    goto infile_error;
}

这是一项非常重要的工作,目前还远未达到完美甚至功能。 我只是想知道为什么我一直收到以下错误:

html2enscript.c:50:2: error: expected expression
        :infile_error // XXX goto in hextonum branches here
        ^

这是某种编译器强制执行的良好实践规则,还是我实际上做错了什么(错误的是我的意思是语法错误,而不仅仅是糟糕的编程风格)?

附加信息:

这是我的gcc版本信息:

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.3.0 (clang-703.0.31)
Target: x86_64-apple-darwin15.0.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

这里有两个问题。 首先是您的goto标签未正确指定。

你有这个:

:infile_error

它应该在哪里:

infile_error:

第二个问题,即较大的问题,是你试图使用goto跳转到另一个函数。 这是不允许的。

看来你正试图实现某种异常机制。 但是,C不支持此功能。

执行此操作的正确方法是让函数返回一些指定错误的值,然后在函数退出时检查该值。

如果你真的想做一个非本地的goto ,你可以用setjmplongjmp来做。 这与你在C中的异常处理一样接近。

jmp_buf hextonum_err;

int main( int argc, char **argv ){
    ...
    if (setjmp(hextonum_err) != 0) {
        goto infile_error;
    }
    while( (c = fgetc( in )) != EOF ){
    ...
}

unsigned short hextonum( char hex ){
    if( hex >= '0' && hex <= '9' ){
        // don't use atoi here as that expects a string
        return hex - '0';
    }
    switch( hex ){
        case( 'a' ) : case( 'A' ) : return 0xa;
        case( 'b' ) : case( 'B' ) : return 0xb;
        case( 'c' ) : case( 'C' ) : return 0xc;
        case( 'd' ) : case( 'D' ) : return 0xd;
        case( 'e' ) : case( 'E' ) : return 0xe;
        case( 'f' ) : case( 'F' ) : return 0xf;
    }
    longjmp(hextonum_err, 1);
    // never reached, but put here because compiler will warn on non returning a value
    return 0;
}

一般来说,我不建议像这样编写代码。

正如我在评论中提到的,你不能使用goto跨功能边界分支,所以这不起作用。

就个人而言,我将红色/绿色/蓝色计算抽象为自己的函数并检查结果:

int readcolor( FILE *stream, float *red, float *green, float *blue )
{
  float *rgb[3];
  rgb[0] = red;
  rgb[1] = green;
  rgb[2] = blue;

  for ( size_t i = 0; i < 3; i++ )
  {
    int b0 = fgetc( stream );
    int b1 = fgetc( stream );

    if ( b0 == EOF || b1 == EOF )
      return 0;

    *rgb[i] = hextonum( b0 ) * 10.0f / 16.0f * 0.1f + 
              hextonum( b1 ) * 10.0f / 16.0f * 0.1f;
  }
  return 1;
}

然后在你的main功能:

if( fgetc( in ) == '#' )
{
  if ( readcolor( in, &red, &green, &blue ) )
    writecolor( out, red, green, blue );
}
else
{
  ...
}

无论如何,使main代码扫描更好一些。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM