gpt4 book ai didi

c++ - LEX:段错误:11

转载 作者:太空宇宙 更新时间:2023-11-04 04:34:17 24 4
gpt4 key购买 nike

我有一个独立的(意味着还没有与 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]

使用标准的 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 是(再次)一个起点。我通常使用脚本来应用更多选项,例如,我提到的 gcc-normal getopt isn't working for one argument

关于c++ - LEX:段错误:11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32835185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com