【问题标题】:LEX: segmentation fault : 11LEX:分段错误:11
【发布时间】:2015-09-29 03:25:16
【问题描述】:

我有一个独立的(意味着尚未与 YACC 关联)LEX 程序。并且可以编译。但是当我运行它时,我收到如下错误消息:

分段错误:11

当成功读取多个链接时会出现此错误消息。

你能帮我解决这个问题吗?

谢谢!

代码:

  %{
  #include <strings.h>
  typedef int YYSTYPE;
  extern YYSTYPE yylval;
  int check; 
  int dummy;
  int val1;
  int val2;
  char *net_name; 
  int fanouts;
  int fanins;
  char *net_name;
  char *GATE_ASCI;
  char *GATE_TYPE;
  char *from;
  int fan_net;
  %}

  %start A B C D E F
  NET_NAME  [0-9]
  ASCI_GATE [0-9a-zA-Z]+
  SOURCE_GATE_TYPE  [a-zA-Z]+
  NUM_FANOUTS [0-9]
  NUM_FANINS [0-9]
  INPUT_LIST_1 [0-9]
  INPUT_LIST_2 [0-9]
  SPACE  [ \t\n]+
  FAN_NET [0-9a-zA-Z]+
  DIGITS [0-9]
  character [a-zA-Z]+  
  %%

  "*".*\n               {
                           BEGIN A;
                           //yylval.string = strdup(yytext);
                           /*printf("I am here in comments \n");*/
                           //return(COMMENT);
                        }

  <A>{NET_NAME}            {
                             BEGIN B;
                             net_name = strdup(yytext);       
                           /*  yylval.number = atoi(yytext);*/
                             printf("%s",net_name);

                          }


  <B>{ASCI_GATE}             {
                                BEGIN C; 
                                 GATE_ASCI = strdup(yytext);
                               printf("%s", GATE_ASCI);
                              }


  <C>{SOURCE_GATE_TYPE}       {
                                check = find_gate_type(yytext);
                                if (check != 0) {
                                  BEGIN D;
                                    GATE_TYPE = strdup(yytext);
                                  printf("%s", GATE_TYPE);
                              }

                               else {
                                     /* printf("I am here in From \n"); */
                                    BEGIN E;

                                     from  = strdup(yytext);
                                    printf("%s", from);
                              }
                          }


  <D>{NUM_FANOUTS}{SPACE}{NUM_FANINS}       {
                                            /* printf("NUM_FANOUTS NUM_FANINS\t");*/

                                              fanouts =   atoi(&yytext[0]);
                                              fanins =   atoi(&yytext[2]);

                                              printf("%d %d",fanouts, fanins); 

                                            /*BEGIN F;*/
                                            /*yylval.number = atoi(yytext);*/
                                           /* printf("I am here in Fanout \n");*/
                                              BEGIN A;
                                            }

  <E>{FAN_NET}                             {
                                            BEGIN A; 
                                            fan_net  = strdup(yytext);
                                            printf("%s", fan_net);
                                           }

  <F>{DIGITS}                         {
                                          BEGIN A;  
                                          val1 =   atoi(&yytext[0]); 
                                          val2 =   atoi(&yytext[1]); 
                                          printf("%d %d",val1, val2); 
                                         }

  ">sa"[0-1]                            {
                                        printf("%s", yytext);

                                        }

  %%

  find_gate_type(char *string_pass){
    char *string_cmp = "from";

    if(strcmp(string_pass, string_cmp) == 0) {
     /* printf("I am here  in FROM FUNCTION \n"); */
     return 0;
    }
     else{
      return 1;
     }
  }

  int main(int argc, char *argv[]){
  //int argc;
  //char **argv;

           if (argc > 1) {
                  FILE *file;
                  file = fopen(argv[1], "r");
                  if (!file) {
                          fprintf(stderr,"could not open %s\n",argv[1]);
                  } else {

                   printf("reading\n");
                   yyin = file;
                  }   
      yylex();  
      return 0;   

                /*  fclose(file);*/
        }

   }


  int yywrap(void)
  {
    return 1;
  }

【问题讨论】:

  • 您是否尝试过在调试器中运行您的程序以准确找出它何时出现段错误?
  • 使用 -Wall 编译并修复它警告您的内容。
  • 您需要提供导致此崩溃的示例输入。
  • 你好,我使用命令 $ cc -o parser lex.yy.c 来编译它,这是输入文件:*c17 iscas example(仅用于测试转换程序)1 1gat inpt 1 0 >sa1 2 2gat inpt 1 0 >sa1 3 3gat inpt 2 0 >sa0 >sa1 8 8fan 从 3gat >sa1 9 9fan 从 3gat >sa1 6 6gat inpt 1 0 >sa1 7 7gat inpt 1 0 >sa1

标签: c++ c segmentation-fault lex


【解决方案1】:

按照建议,编译器警告是开始的地方:

$ gcc -Wall -c lex.yy.c 2>&1 |sed -e 's/^/    /'
foo.l: In function ‘yylex’:
foo.l:42:3: warning: implicit declaration of function ‘strdup’ [-Wimplicit-function-declaration]
foo.l:42:14: warning: incompatible implicit declaration of built-in function ‘strdup’ [enabled by default]
foo.l:51:15: warning: incompatible implicit declaration of built-in function ‘strdup’ [enabled by default]
foo.l:57:3: warning: implicit declaration of function ‘find_gate_type’ [-Wimplicit-function-declaration]
foo.l:60:19: warning: incompatible implicit declaration of built-in function ‘strdup’ [enabled by default]
foo.l:68:15: warning: incompatible implicit declaration of built-in function ‘strdup’ [enabled by default]
foo.l:90:14: warning: incompatible implicit declaration of built-in function ‘strdup’ [enabled by default]
foo.l:90:12: warning: assignment makes integer from pointer without a cast [enabled by default]
foo.l:91:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
lex.yy.c: In function ‘yy_init_buffer’:
lex.yy.c:1253:5: warning: implicit declaration of function ‘isatty’ [-Wimplicit-function-declaration]
foo.l: At top level:
foo.l:108:3: warning: return type defaults to ‘int’ [-Wreturn-type]
foo.l: In function ‘find_gate_type’:
foo.l:111:5: warning: implicit declaration of function ‘strcmp’ [-Wimplicit-function-declaration]
foo.l: At top level:
lex.yy.c:1048:1: warning: ‘yyunput’ defined but not used [-Wunused-function]
lex.yy.c:1089:1: warning: ‘input’ defined but not used [-Wunused-function]
foo.l: In function ‘main’:
foo.l:140:4: warning: control reaches end of non-void function [-Wreturn-type]

使用标准string.h 处理不正确的strings.h 可以减少问题:

$ gcc -Wall -c lex.yy.c 2>&1 |sed -e 's/^/    /'
foo.l: In function ‘yylex’:
foo.l:57:3: warning: implicit declaration of function ‘find_gate_type’ [-Wimplicit-function-declaration]
foo.l:90:12: warning: assignment makes integer from pointer without a cast [enabled by default]
foo.l:91:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
lex.yy.c: In function ‘yy_init_buffer’:
lex.yy.c:1253:5: warning: implicit declaration of function ‘isatty’ [-Wimplicit-function-declaration]
foo.l: At top level:
foo.l:108:3: warning: return type defaults to ‘int’ [-Wreturn-type]
lex.yy.c:1048:1: warning: ‘yyunput’ defined but not used [-Wunused-function]
lex.yy.c:1089:1: warning: ‘input’ defined but not used [-Wunused-function]
foo.l: In function ‘main’:
foo.l:140:4: warning: control reaches end of non-void function [-Wreturn-type]

编译器指出fan_type 被分配了strdup 的结果(但它被声明为整数),然后在printf 语句中用作字符串。

使用调试器(我更喜欢valgrind),您可能会发现需要修复的其他内容,但编译器警告是首选的起点。

顺便说一句,-Wall (再次)是一个起点。我通常使用脚本来应用更多选项,例如,我在 getopt isn't working for one argument

中提到了gcc-normal

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 2017-01-07
    相关资源
    最近更新 更多