【发布时间】:2016-04-20 18:22:08
【问题描述】:
我正在尝试调试为什么我的变量 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。
【问题讨论】:
-
"
mystringis not known" 似乎对问题的描述不佳。该变量肯定是已知的,因为声明在引用该变量的任何地方都在范围内,否则您的代码将无法编译。如果问题完全出在mystring上,那么肯定是它的价值不是你所期望的。 -
但是为什么这个语法没有执行呢?
<SINGLE_QUOTED>[^']+ { printf("test");mystring = strdup(yytext); }我以为应该写出测试信息给mystring。 -
@PaulOgilvie 我写信给解释器
echo 'a b',我得到一个段错误。我希望它打印test' and write to themystring` 变量。 -
如果它达到了那个规则,那么至少你会看到“test”出现在
printf("test");如果你没有看到那个,它没有达到那个规则。当然,它首先看到的是echo,对此我没有看到任何规则。 -
echo导致 lexcode 3。看起来echo被正确解释。问题是echo foo有效,但echo 'foo bar'无效
标签: c posix flex-lexer yacc lemon