繁体   English   中英

LEX:分段错误:11

[英]LEX: segmentation fault : 11

我有一个独立的(意味着尚未与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;
  }

如建议的那样,编译器警告是开始的地方:

$ 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]

地址不正确的strings.h使用标准string.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中提到gcc-normal 不适用于一个参数

暂无
暂无

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

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