I'm trying to debug why my variable mystring
is not known when I think it should be according to an earlier question
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)
My grammar is
%{
#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; }
%%
Then my main loop is
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);
Why is mystring
null when I expect it to be something? I get a segmentation fault:
$ ./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)
The whole project is on my github.