使用以下语法,我收到这种输入的语法错误:
ls /home > foo #Runs and works okay, but raises error token
ls /home /foo /bar /etc #works okay
我认为这可能与前瞻的工作方式有关,但这是我的第一个语法,我有点困惑为什么它不能这样工作:external_cmd GT WORD 是重定向,redirect 是命令,命令是命令,因此输入命令 NEWLINE 应该可以工作。
语法的主要规则:
input:
error NEWLINE {
printf("Error Triggered\n");
yyclearin;
yyerrok;
prompt();
} |
input NEWLINE {
prompt();
} |
input commands NEWLINE {
prompt ();
} |
/* empty */
;
commands:
command |
command SEMI |
command SEMI commands
;
command:
builtin_cmd |
redirect |
external_cmd {
execute_command($1, 0, NULL);
}
;
redirect:
external_cmd GT WORD {
printf("Redirecting stdout of %s to %s\n", $1->cmd, $3);
//printf("DEBUG: GT\n");
execute_command($1, STDOUT_FILENO, $3);
}
external_cmd LT WORD {
printf("Redirecting stin of %s to %s\n", $1->cmd, $3);
//printf("DEBUG: GT\n");
execute_command($1, STDIN_FILENO, $3);
}
;
引发错误标记时的调试/详细输入:
Next token is token WORD ()
Shifting token WORD ()
Entering state 6
Reading a token: Next token is token WORD ()
Shifting token WORD ()
Entering state 24
Reading a token: Next token is token GT ()
Reducing stack by rule 22 (line 115):
$1 = token WORD ()
-> $$ = nterm arg_list ()
Stack now 0 2 6
Entering state 26
Reducing stack by rule 19 (line 91):
$1 = token WORD ()
$2 = nterm arg_list ()
-> $$ = nterm external_cmd ()
Stack now 0 2
Entering state 16
Next token is token GT ()
Shifting token GT ()
Entering state 29
Reading a token: Next token is token WORD ()
Shifting token WORD ()
Entering state 33
Reducing stack by rule 11 (line 68):
Redirecting stdout of ls to foo
DEBUG: redirect mode is 1
DEBUG: Command to run is ls
DEBUG: Adding Argument /home
admin kbrandt tempuser
-> $$ = nterm @1 ()
Stack now 0 2 16 29 33
Entering state 34
Reading a token: Next token is token NEWLINE ()
syntax error
Error: popping nterm @1 ()
Stack now 0 2 16 29 33
Error: popping token WORD ()
Stack now 0 2 16 29
Error: popping token GT ()
Stack now 0 2 16
Error: popping nterm external_cmd ()
Stack now 0 2
Error: popping nterm input ()
Stack now 0
Shifting token error ()
Entering state 1
Next token is token NEWLINE ()
Shifting token NEWLINE ()
Entering state 3
Reducing stack by rule 1 (line 38):
$1 = token error ()
$2 = token NEWLINE ()
Error Triggered
-> $$ = nterm input ()
Stack now 0
Entering state 2
更新:
external_cmd 是:
external_cmd:
WORD arg_list {
$$ = malloc( sizeof(struct ext_cmd) );
if ( $$ == NULL)
printf("Memory Allocation Error\n");
$$->cmd = $1;
$$->args_pp = $2;
} |
WORD {
$$ = malloc( sizeof(struct ext_cmd) );
if ( $$ == NULL)
printf("Memory Allocation Error\n");
$$->cmd = $<string>1;
$$->args_pp = NULL;
}