문제
다음과 같은 문법,저는 구문 오류가 이 류의 입력:
ls /home > foo #Runs and works okay, but raises error token
ls /home /foo /bar /etc #works okay
나도 모른다는 생각을 함께 할 수있는 뭔가가있는 방법 예측,작동하지만 이번이 처음 문법이고 나는 비트에 대한 혼란이 작동하지 않는 이유가 이 방법:external_cmd GT 단어가 리다이렉션을 리디렉션은 명령 명령 명령,그래서 입력된 명령을 줄 바꿈 작업해야 합니다.
최고 규칙의 문법:
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;
}
해결책 3
내 리디렉션 규칙에는 누락 된 파이프가 있으므로 두 가지 구성 요소 대신 중간 규칙 동작이있는 것이 있습니다.
다른 팁
구문 오류는 두 번째 호출에서 Yyparse로 발생합니다. 리디렉션이 있으면 문법이 yyaccept를 수행하여 더 이상 읽지 않고 구식 구식이 즉시 돌아 오게합니다. 두 번째 호출에서 첫 번째 토큰 읽기는 Newline입니다.이 경우 오류가 발생합니다 (문법은 빈 줄을 허용하지 않습니다.)
리디렉션이 없으면 yyaccept가 없으므로 문법은 계속 실행되어 Newline을 읽고 입력의 끝에 도달하면 돌아갑니다.
당신이 정말로 말 를 사용해야 왼쪽 재귀로 LALR(1)파서 생성기.오른 재귀를 필요로하는 모든 요소를 이동할 수 있에 파서 주택하기 전에도 하나의 감소가 발생할 수 있습니다.당신이 상상할 수 있는 이것이 무엇을 오류 복구합니다.
정확히 무엇입
external_cmd
?그것은 종류의 것 같은 감소되는 초기 그러나 그것은 말하기 어렵기 때문에 당신은 포함하지 않습니다.왜
YYACCEPT
호출한 후에는 리다이렉션?하려는 경우에는 다시 시작하는 파서 각 라인에 다음 필요가 없습을 재귀적 입니다.할 만큼 그것을 가하지 않는다,YYACCEPT.