gpt4 book ai didi

c - 为什么未知变量 "mystring"?

转载 作者:太空宇宙 更新时间:2023-11-04 01:54:33 25 4
gpt4 key购买 nike

我正在尝试调试为什么我的变量 mystring 未知,而我认为它应该根据之前的问题

Is the bug in the grammar or in the code?

(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/dac/ClionProjects/openshell/openshell
'PATH' is set to /home/dac/proj/google-cloud-sdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games.
> echo 'a b'
lexcode 3 Text echo mystring (null)
Becho
testlexcode 4 Text ' mystring (null)
lexcode 1 Text
mystring (null)
argument ::= ARGUMENT .
argumentList ::= argument .
command ::= FILENAME argumentList .
commandList ::= command .
{(null)} {echo} {(null)}

Program received signal SIGSEGV, Segmentation fault.
0x0000000000402308 in main ()
(gdb)

我的语法是

%{
#include "shellparser.h"
#include <string.h>
char *mystring;
%}

%option reentrant
%option noyywrap

%x SINGLE_QUOTED
%x DOUBLE_QUOTED

%%

"|" { return PIPE; }

[ \t\r] { }
[\n] { return EOL; }

[a-zA-Z0-9_\.\-]+ { return FILENAME; }

['] { BEGIN(SINGLE_QUOTED); }
<SINGLE_QUOTED>[^']+ { printf("test");mystring = strdup(yytext); }

<SINGLE_QUOTED>['] { BEGIN(INITIAL);
/* mystring contains the whole string now,
yytext contains only "'" */
return ARGUMENT; }
<SINGLE_QUOTED><<EOF>> { return -1; }

["] { BEGIN(DOUBLE_QUOTED); }
<DOUBLE_QUOTED>[^"]+ { }
<DOUBLE_QUOTED>["] { BEGIN(INITIAL); return ARGUMENT; }
<DOUBLE_QUOTED><<EOF>> { return -1; }

[^ \t\r\n|'"]+ { return ARGUMENT; }

%%

然后我的主循环是

yylex_init(&scanner);
yyset_in(stdin, scanner);

shellParser = ParseAlloc(malloc);

params[0] = NULL;
printf("> ");
i=1;
do {
lexCode = yylex(scanner);
text = strdup(yyget_text(scanner));
printf("lexcode %i Text %s mystring %s\n", lexCode, text, mystring);
if (lexCode == 4) {
params[i++] = mystring;
if (strcmp(text, "\'\0")) {
params[i++] = mystring;
}
} else
if (lexCode != EOL) {
params[i++] = text;
printf("B%s\n", text);
}
Parse(shellParser, lexCode, text);
if (lexCode == EOL) {
dump_argv("Before exec_arguments", i, params);
exec_arguments(i, params);
corpse_collector();
Parse(shellParser, 0, NULL);
i=1;
}
} while (lexCode > 0);
if (-1 == lexCode) {
fprintf(stderr, "The scanner encountered an error.\n");
}
yylex_destroy(scanner);
ParseFree(shellParser, free);

为什么 mystring 是空的,而我期望它是什么?我遇到段错误:

$ ./openshell 
'PATH' is set to /home/dac/proj/google-cloud-sdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games.
> echo 'a b'
lexcode 3 Text echo mystring (null)
Becho
testlexcode 4 Text ' mystring (null)
lexcode 1 Text
mystring (null)
argument ::= ARGUMENT .
argumentList ::= argument .
command ::= FILENAME argumentList .
commandList ::= command .
{(null)} {echo} {(null)}
Segmentation fault (core dumped)

整个项目在my github上.

最佳答案

因为在

    lexCode = yylex(scanner);
text = strdup(yyget_text(scanner));
printf("lexcode %i Text %s mystring %s\n", lexCode, text, mystring);

mystring 不一定由 yylex 设置。只有一个设置它的规则,所以通常它会(仍然)为 NULL,导致段错误。

关于c - 为什么未知变量 "mystring"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36752066/

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